check crond nagios plugin II

Metode kedua dengan cara membaca status dari cron log, dengan cara ini lebih sederhana. kita dapat melihat service status dari log cron. lalu waktu dari log service tersebut di parsing menjadi detik dan dikurangi dengan waktu sekarang.
#!/bin/bash
# Standard Nagios return codes
#OK       = 0
#WARNING  = 1
#CRITICAL = 2
#UNKNOWN  = 3

LOG=/var/log/cron

usage()
{
        echo "usage: check_crond [-w warning_value (in minutes)] [-c critical_value (in minutes)] -s services"
        echo
        echo "check_crond -w 0 -c 5 -s /usr/bin/yum"
        echo "critical_value must be greater than warning_value"
        exit 3
}


while getopts w:c:s: OPTNAME; do
        case "$OPTNAME" in
        w)
                WARNVAL="$OPTARG"
                ;;
        c)
                CRITVAL="$OPTARG"
                ;;
        s)
                SERVICES="$OPTARG"
                ;;
        *)
                usage
                ;;
        esac
done


#check value
if [ "$WARNVAL" != "" -a "$CRITVAL" != "" ]; then
        if [ $CRITVAL -le $WARNVAL ] ; then
                echo "Error: critical_value must be greater than warning_value"
                echo        
                usage
                exit 3
        fi
fi

#check dir
if [ "$SERVICES" = "" ]; then
                echo "Error: services must exist do not empty"
                echo
                usage
                exit 3
fi

# Compare
LASTRUN=$(grep $SERVICES $LOG  | tail -n 1 | cut -f1 -d  '.' | sed -e 's/T/ /')
CHECK=$(((`date +%s` - `date -d "$LASTRUN" +%s`)/60))
HOURS=$(((`date +%s` - `date -d "$LASTRUN" +%s`)/3600))


if [ $CHECK != "" ] ; then

        if [ $WARNVAL -ge $CHECK ] ; then
                echo "** WARNING **"
                echo "last error at $CHECK minutes ago"
                exit 1;
        fi

        if [ $CHECK -gt $CRITVAL ] ; then
                echo "** CRITICAL **"
                echo "last error at $CHECK minutes ago,$HOURS hours"
                exit 2;
        fi
        else
                echo "** OK **";
                echo "crond run at $LASTRUN at `hostname`"
fi
exit 0

Comments