Page 1 of 2

Nagios will not return string from bash script

Posted: Sat Sep 08, 2012 2:12 am
by w14219
I have the following script that will not echo the results of a "specific" string to the Nagios dashboard. However, I am able to echo text as well as a static string.

Example:

Will echo: "test 99"
outputSSH=$(ssh $community@$Host "/interface wireless monitor 0 once"|grep tx-ccq|grep -v overall-tx-ccq|sed 's/%//')
IFS=.:.; declare -a Array=($outputSSH)
test2=${Array[1]}
echo $test2 > temp_tx-ccq.tmp
ccq=`cat temp_tx-ccq.tmp`
ccq2=" 99"
echo "test $ccq2"
exit $STATE_OK

Will not echo: "test 99"
outputSSH=$(ssh $community@$Host "/interface wireless monitor 0 once"|grep tx-ccq|grep -v overall-tx-ccq|sed 's/%//')
IFS=.:.; declare -a Array=($outputSSH)
test2=${Array[1]}
echo $test2 > temp_tx-ccq.tmp
ccq=`cat temp_tx-ccq.tmp`
ccq2="99"
echo "test $ccq"
exit $STATE_OK

Question:
What is preventing the variable $ccq from working? If I run the script from the command line, $ccq returns the actual value of " 99". However, Nagios will not display the same value in the dashboard.

thank you for your time.

Re: Nagios will not return string from bash script

Posted: Sun Sep 09, 2012 6:00 pm
by jsmurphy
Ahhhh, shell script checks, we meet again my old nemesis. Nearly every single shell script check problem I've ever encountered has been the result of insufficient permissions, either to write the temp file or to execute a linux binary. The remaining percentage of problems were SELinux doing what SELinux does best.

So lets verify, run "su - nagios" to swap the the nagios user with its environment intact and then try to run the script on the command line again. It should fail, at least I really hope it does :lol:.

Re: Nagios will not return string from bash script

Posted: Sun Sep 09, 2012 10:11 pm
by w14219
Thanks for the suggestion. However, the script runs fine as the nagios user and am still not able to get the results to return.

see below:

nagios@Nagios:/usr/local/nagios/libexec$ ls -la check_mikrotik_signal
-rwxr-xr-x 1 nagios nagios 996 2012-09-08 08:25 check_mikrotik_signal

nagios@Nagios:/usr/local/nagios/libexec$ ls -la temp_tx-ccq.tmp
-rwxrwxrwx 1 nagios nagios 5 2012-09-09 21:55 temp_tx-ccq.tmp

nagios@Nagios:/usr/local/nagios/libexec$ ./check_mikrotik_signal
test100

https://server/nagios....
test

Mike

Re: Nagios will not return string from bash script

Posted: Mon Sep 10, 2012 10:09 am
by w14219
I believe the issue is with the type of variable $ccq is. I cannot declare it as an integer which I think is causing an issue.


grr

Re: Nagios will not return string from bash script

Posted: Mon Sep 10, 2012 4:59 pm
by jsmurphy
Why script! Why did you have to work!? My concern wasn't with the permissions of the actual files involved it was more so a concern that the Nagios user doesn't have access to execute ssh or cat or something in the script. But obviously not the case here.

I don't think it should matter what the data type is, shell scripting isn't a strongly typed language so it doesn't really understand the difference between a string and an int. So this leads me to common cause number two. Do you have selinux (silent destroyer of programmer sanity) enabled? run "sestatus" to find out.

Re: Nagios will not return string from bash script

Posted: Mon Sep 10, 2012 8:27 pm
by w14219
w14219@Nagios:/usr/local/nagios/etc/objects$ sudo sestatus
SELinux status: disabled


Was Disabled.

I am running this on Ubuntu 12.04.

Re: Nagios will not return string from bash script

Posted: Mon Sep 10, 2012 11:02 pm
by w14219
$output="100"
============================================================
Results without adding text to output string. Notice the $output is missing in the web gui?
============================================================
outputSSH=$(ssh $community@$Host "/interface wireless monitor 0 once"|grep tx-ccq|grep -v overall-tx-ccq|sed 's/%//')
IFS=" "; declare -a Array=($outputSSH)
output=${Array[1]}
echo "$output"
exit $STATE_OK

nagios@Nagios:/usr/local/nagios/libexec$ ./check_mikrotik_signal
100

Results in Nagios Web: (No output returned from plugin)
============================================================
Results when adding text to output string. Notice the $output is missing a value below, but the text is there?
============================================================
outputSSH=$(ssh $community@$Host "/interface wireless monitor 0 once"|grep tx-ccq|grep -v overall-tx-ccq|sed 's/%//')
IFS=" "; declare -a Array=($outputSSH)
output=${Array[1]}
echo "yup $output"
exit $STATE_OK

nagios@Nagios:/usr/local/nagios/libexec$ ./check_mikrotik_signal
yup 100

Results in Nagios Web:yup
============================================================
I am chasing my tail here.... No matter what I do I cannot get the $output value to display in the nagios window. While text will display just fine.

Nagios does not even recognized the value is even in the $output file.

Re: Nagios will not return string from bash script

Posted: Tue Sep 11, 2012 5:32 am
by w14219
It has to be an issue with permissions, I'm not sure where.

Running the script via command line everything works.

Running the script via nagios service, the ssh does not work. The text and logic of the critical levels are working, but it does not seem to be running the ssh execution.

outputSSH=$(ssh $account@$host "/interface wireless monitor 0 once"|grep tx-ccq|grep -v overall-tx-ccq|sed $'s/%//')

Re: Nagios will not return string from bash script

Posted: Tue Sep 11, 2012 5:40 am
by w14219
#####Service file
define service{
use generic-service
hostgroup_name Mikrotik_RG411
service_description tx-ccq
check_command check_rg411
}

#####Command file
define command{
command_name check_rg411
command_line $USER1$/check_rg411 $HOSTADDRESS $USER3$
}

#####Script - I am leveraging another script I found....
#####Permissions: 755 Nagios:Nagios on the
OUTFILE="/tmp/check_https_out.$$"
ERRFILE="/tmp/check_https_err.$$"
#WGETCMD="$(which wget)"
#outputSSH=""
#IFS=""
#RETURNVALUE=""
## Do basic check on arguments passed to the script.
if [ "$1" = "" ]; then
host="10.2.1.34"
# echo "Missing required parameter"
# exit 2
fi
if [ "$2" = "" ]; then
## Assume default port.
account="admin"
else
account=$2
fi
#${WGETCMD} --no-check-certificate --output-document=${OUTFILE} -S https://$1:${SSLPORT} 2> ${ERRFILE}
outputSSH=$(ssh $account@$host "/interface wireless monitor 0 once"|grep tx-ccq|grep -v overall-tx-ccq|sed $'s/%//')
IFS=" "
set -- $outputSSH
#echo $1
#echo $2
unset IFS
#output=${Array[1]}
RETURNVALUE=${2//[^0-9]/}
#echo $RETURNVALUE
#RETURNVALUE=$?
if [ "$RETURNVALUE" -ge 90 ]; then
echo "tx-ccq OK = $RETURNVALUE "
EXITCODE=0
else
echo "tc-ccq less than 90%= $RETURNVALUE"
EXITCODE=1
fi
if [ -f ${OUTFILE} ]; then
rm ${OUTFILE}
fi
if [ -f ${ERRFILE} ]; then
rm ${ERRFILE}
fi
exit ${EXITCODE}


#######

Re: Nagios will not return string from bash script

Posted: Tue Sep 11, 2012 2:38 pm
by w14219
Looking at the Router, Nagios does not attempt to connect.

Why would I be able to execute the script via command line with a successful SSH response, while Nagios executes the script but does not attempt to use the ssh command? How can I see errors when scripts run in Nagios?

Thanks for your help