Page 1 of 1

NAgios plugin works from command line but not via NRPE

Posted: Tue Jan 12, 2016 3:10 pm
by b0br0ff
Hi Everybody, thank you in advance if somebody can help with following issue.

My Nagios plugin needs to connect o MySQL server and grep some info from there. In case of unsuccessful connection attempt it needs to count connection failures and show status as WARNING in case of connection failures less then 3 and if more then 3 => CRITICAL. So, I store connection failures in the text file in /tmp/fails.count

My problem is that when I execute script from command line on a client machine it works as expected, however when I executed from Nagios server it always returns 1 as fails count.

Output at client:
[root@cent1 ~]# /usr/lib/nagios/plugins/check_DP_status.sh
DP verification failed 5 times (TCP port 3307): ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)


Output at server:
[root@cent2 20160111]# /usr/lib/nagios/plugins/check_nrpe -H 192.168.178.30 -c check_dp_status
File /tmp/fails.count exist and is accessible!
Fails count=
DP verification failed 1 times (TCP port 3307): ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)


Environment:
OS: Centos 6.5
Language: bash

Code: Select all

COUNTER_FILE="/tmp/fails.count"
#MySQL settings
MYSQL_USER=xxx
MYSQL_PASSWD=xxx
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3307
MYSQL_QUERY="SELECT count(*) FROM ...."
MYSQL_OPT="--host=${MYSQL_HOST} --port=${MYSQL_PORT} --user=${MYSQL_USER} --password=${MYSQL_PASSWD}"
#End MySQL settings


mysql ${MYSQL_OPT} --execute="${MYSQL_QUERY}" > ${LOG_DP_STATUS} 2> ${LOG_ACCESS}

if [ -s "${LOG_ACCESS}" ]; then
	#Could not connect to MySQL
	MSG=`tail -n 1 ${LOG_ACCESS}`
	COUNT=
	
	#Create a new counter file if one doesn't exist already
	if [ -r $COUNTER_FILE ] ; then
		echo "File $COUNTER_FILE exist and is accessible!"
	    #COUNT=$(<$COUNTER_FILE)
	   COUNT=$(cat $COUNTER_FILE)
	   echo "Fails count=$COUNT"
	else
		echo "File $COUNTER_FILE does not exist!"
		COUNT=0
	fi

	#Increment counter and save to file 
	COUNT=$(( COUNT += 1 ))
	echo $COUNT > $COUNTER_FILE
	#echo "Fails count=$COUNT"
	echo "DP verification failed $COUNT times (TCP port ${MYSQL_PORT}): ${MSG}"
	
	#check if fails count is 3
	if [[ $(($COUNT)) -gt 3 ]] ; then
	   #We have reached 3
	   exit 2
	fi
	
	exit 1
else
	COUNT=0
	echo $COUNT > $COUNTER_FILE
fi

Regards
Evghenii

Re: NAgios plugin works from command line but not via NRPE

Posted: Tue Jan 12, 2016 3:52 pm
by hsmith
Can you try to run the script on the client as the nagios user account?

Re: NAgios plugin works from command line but not via NRPE

Posted: Wed Jan 13, 2016 3:24 am
by b0br0ff
Hi actually nagios account has "nologin" option in /etc/passwd. Might be this a reason?

Re: NAgios plugin works from command line but not via NRPE

Posted: Wed Jan 13, 2016 11:01 am
by hsmith
That could be an issue. The /etc/passwd entry for the nagios account on my VPS is this:

Code: Select all

grep nagios /etc/passwd
nagios:x:1002:1002::/home/nagios:/bin/sh