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