Friday, April 05, 2013

basic nagios bash service check

#!/bin/bash

CMD=`some command with a numerical output`

# Sanity check
if [ $# -ne 4 ]; then
        echo "Usage: $0 -w WARNING -c CRITICAL"
        exit
fi


while getopts ":w:c:" optname
  do
    case "$optname" in
      "w")
        WARNING=$OPTARG
        ;;
      "c")
        CRITICAL=$OPTARG
        ;;
      "?")
        echo -e "Unknown option $OPTARG\nUsage: $0 -w WARNING -c CRITICAL"
        ;;
      ":")
        echo "No argument value for option $OPTARG"
        ;;
      *)
      # Should not occur
        echo "Unknown error while processing options"
        ;;
    esac
  done

E_SUCCESS="0"
E_WARNING="1"
E_CRITICAL="2"
E_UNKNOWN="3"

if [ "$CMD" -gt "$CRITICAL" ]; then
        echo -n "CRITICAL"
        RETCODE=$E_CRITICAL
elif [ "$CMD" -gt "$WARNING" ];then
        echo -n "WARNING"
        RETCODE=$E_WARNING
else
        echo -n "OK"
        RETCODE=$E_SUCCESS
fi

echo "|RESULT=$CMD"
exit $RETCODE

No comments: