Script for parsing log file for errors

Jack Sprat jackcsprat at yahoo.com
Tue Feb 17 21:25:32 CET 2009


I have a script that I wrote that will parse through a log looking for instances of the word "error" and count them.

If there are zero instances of the word, I expect a green (STATE_OK) status. If there are 1 or more instances of the word "error", I expect to see a red (STATE_CRITICAL) status. Currently the script appears to work fine from the command line, below are the final few lines of output from the script when I have "set -x" turned on for debugging.

+ EXITMESSAGE= 20 errors found
+ EXITSTATUS=2
+ [[ 2 -eq 0 ]]
+ echo 20 errors found
20 errors found
+ exit 2

Currently, on the Nagios web interface, the server is showing "Yellow" (warning status) and the "Status Information" column has an output showing of the word "null". I feel that my issue may be a quoting issue or something simple I am overlooking. Below is my script:

#!/bin/ksh

set -x

GREPCMD=/usr/bin/grep
SSHCMD=/usr/bin/ssh
ECHOCMD=/usr/bin/echo
NAGIOSDIR=/space/nagios

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

EXITMESSAGE='ERROR'

while getopts :s:u:h: OPT
do
case $OPT in
        s|+s ) export SERVER=$OPTARG ;;
        u|+u ) export USER=$OPTARG ;;
        : ) $ECHO "$OPTARG requires an argument"; exit $STATE_UNKNOWN;;
        \? ) $ECHO "$OPTARG: bad option, use -h for help"; exit $STATE_UNKNOWN;;
        h|+h ) $ECHO "Usage: `basename $0` -w WARNSTATES -c CRITICALSTATES -s SERVER -u USER" ; exit $STATE_UNKNOWN;;
esac

done

if [ -z "$SERVER" ]; then
   EXITMESSAGE="No SERVER specified"
   echo $EXITMESSAGE
   exit $STATE_UNKNOWN
fi

PING_STATUS=`ping $SERVER | grep -c alive`
if [ $PING_STATUS -ne 1 ]; then
echo "Cannot ping $SERVER"
exit $STATE_UNKNOWN
fi

SSH_STATUS=`$NAGIOSDIR/libexec/check_tcp -H $SERVER -p 22 | grep -ic "TCP OK"`
if [ $SSH_STATUS -ne 1 ]; then
   EXITMESSAGE="Cannot ssh to $SERVER"
   echo $EXITMESSAGE
   exit $STATE_UNKNOWN
fi
export CNT="$(/usr/bin/ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no -l ${USER} ${SERVER} "cat /opt/app/my.log | \
                sed -n 's/.* Error \[.*/Found One/p' | wc -l")"

if [[ $CNT -eq 0 ]]; then
   EXITMESSAGE="No errors found"
   EXITSTATUS="${STATE_OK}"
elif [[ $CNT -gt 0 ]]; then
   EXITMESSAGE="${CNT} errors found"
   EXITSTATUS="${STATE_CRITICAL}"
else
   EXITSTATUS="${STATE_UNKNOWN}"
fi

if [[ $EXITSTATUS -eq $STATE_OK ]]; then
   EXITMESSAGE="No errors found"
fi

echo $EXITMESSAGE
exit $EXITSTATUS
 
Any input would be appreciated.


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.monitoring-lists.org/archive/users/attachments/20090217/3f296a58/attachment.html>
-------------- next part --------------
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
-------------- next part --------------
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null


More information about the Users mailing list