Page 1 of 1

BASH script works differently under nagios

Posted: Wed Nov 05, 2014 3:45 pm
by jason_jackal
Folks:
I have a very simple bash script to check the fan state of my Cisco switch. I can run the script from the command prompt with './' and it returns the correct values; however, when I build my command string and enable the check it blows past if condition 1 and if condition 2, and odd part about it... Nagios has my check as showing "warning", and the text as CHECK FAILED, which is the unknown status.

Code: Select all

#!/bin/bash
yes=0
warn=1
err=2
un=3

test=$(snmpget -v2c -c trustedzone 192.168.14.34 CISCO-ENVMON-MIB::ciscoEnvMonFanState.5007  | awk '{print $4}')


if [ "$test" = "normal(1)" ]; then
        echo "OK - 1"
        return "$yes"
elif
[ "$test" = "critical(3)" ]; then
        echo "CRITICAL - 2"
        return "$err"
else
        echo "CHECK FAILED"
        return "$un"

fi
This is driving me crazy since I can get the correct error code from manually running the script. It appears to be something goofy about my IF statement; however, it runs correctly manually.


Thank you
/JJ/

Re: BASH script works differently under nagios

Posted: Wed Nov 05, 2014 3:52 pm
by abrist
Can you try running the script as user nagios from the cli? If it fails, then you have a permission issue.

Re: BASH script works differently under nagios

Posted: Wed Nov 05, 2014 4:05 pm
by jason_jackal
abrist wrote:Can you try running the script as user nagios from the cli? If it fails, then you have a permission issue.
Hi abrist,
I do not believe I ever configured the permissions or user account for 'nagios'. However, 'nagios' does show in my '/etc/passwd' file.

I am forced to login as root and

Code: Select all

[root@nagios nagios]# sudo -s -u nagios
Not sure if this is a good way to do this.

However,

Code: Select all

bash-4.1$ ./check_fan.sh
No log handling enabled - turning on stderr logging
Cannot find module (CISCO-ENVMON-MIB): At line 1 in (none)
CISCO-ENVMON-MIB::ciscoEnvMonFanState.5007: Unknown Object Identifier
./check_fan.sh: line 10: [: : unary operator expected
CHECK FAILED
./check_fan.sh: line 19: return: can only `return' from a function or sourced script
so it looks like an issue with my MIBS being available.


where should my MIBS be loaded? I have them

Code: Select all

~/.snmp  #under root's account
thanks you have helped a great deal -

Re: BASH script works differently under nagios

Posted: Wed Nov 05, 2014 4:14 pm
by jason_jackal
Odd,
I run the script as the nagios user, but I get totally different results.

Code: Select all

[root@nagios plugins]# sudo -s -u nagios
bash-4.1$ . /usr/lib64/nagios/plugins/check_fan.sh
CHECK FAILED

Re: BASH script works differently under nagios

Posted: Wed Nov 05, 2014 4:15 pm
by jason_jackal
exit code looks like it is working

Code: Select all

bash-4.1$ . /usr/lib64/nagios/plugins/check_fan.sh
CHECK FAILED
bash-4.1$ echo $?
3

Re: BASH script works differently under nagios

Posted: Wed Nov 05, 2014 4:21 pm
by abrist
Add some more verbosity to the script. Echo out each step/shell variable and the result from the snmpget. That should help diagnosis.