Help with script needed please


#1

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:

[code]#!/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[/code]

Any suggestions would be greatly appreciated.