Tuesday, May 29, 2012

Nagios-Exit Codes

Here's a Nagios plug-in written in Bash.  It's a template from which other plug-ins can be written.  The final IF block contains basic Nagios exit codes which allow any service check you write to result in any  one of the four standard exit codes-

UNKNOWN
WARNING
CRITICAL
OK



#!/bin/sh
#2012May28
#
#Plugin to check if feeds are updated.  Ssh to feedserver, and run sed for value -1
#at end of each line in /etc/liveserver/feedstates.conf.  If found, the accompanying
#feed didn't update.  Send Nagios alert.

PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`

. $PROGPATH/utils.sh

print_usage() {
echo "Usage: $PROGNAME"
}

print_help() {
print_revisions $PROGNAME $REVISION
echo ""
print_usage
echo ""
echo "This plugin is a template writeen in Bash shell script."
echo ""
support
exit 0
}

case "$1" in
--help)
print_help
exit 0
;;
-h)
print_help
exit 0
;;
--version)
print_revision $PROGNAME $REVISION
exit 0
;;
-V)
print_revision $PROGNAME $REVISION
                exit 0
                ;;
*)
testdata=`/bin/grep "\=\-1" /var/log/nagios/feedstates.conf | wc -l`
#Returns total number of lines found.  If > 0, then at least one feed didn't update.

#Next line for debugging only.  Prints result of above command.
#echo $testdata

status=$?
#Next line for debugging only.  Prints os status code of testdata cmd.
#echo $status

if test "$1" = "-v" -o "$1" = "--verbose"; then
echo ${testdata}
fi

if test ${status} -eq 1; then
echo "UNKNOWN: The plug-in has failed to function."
exit 3

elif echo ${testdata} | egrep WARNING > /dev/null; then
echo "WARNINNG: The plug-ing returned $status."
exit 1

elif [ ${testdata} -gt "0" ]; then
echo "CRITICAL: Feeds not updated. Check /etc/liveserver/feedstates.conf"
exit 2

else [ ${testdata} -eq "0" ] ;
echo "OK: Feeds are up to date."
exit 0

fi
;;
esac

No comments: