BASH script works differently under nagios

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
jason_jackal
Posts: 6
Joined: Wed Nov 05, 2014 3:34 pm

BASH script works differently under nagios

Post 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/
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: BASH script works differently under nagios

Post by abrist »

Can you try running the script as user nagios from the cli? If it fails, then you have a permission issue.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
jason_jackal
Posts: 6
Joined: Wed Nov 05, 2014 3:34 pm

Re: BASH script works differently under nagios

Post 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 -
jason_jackal
Posts: 6
Joined: Wed Nov 05, 2014 3:34 pm

Re: BASH script works differently under nagios

Post 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
jason_jackal
Posts: 6
Joined: Wed Nov 05, 2014 3:34 pm

Re: BASH script works differently under nagios

Post 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
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: BASH script works differently under nagios

Post 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.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
Locked