23 августа 2012 г.

Shell script + nagios

sudo -u nagios /usr/local/libexec/nagios/check_dald-replication.sh -h {IP RSYNC SERV} -t {TIME REPLICK} -m {RSYNC MODULE} -w 0.5 -c 0.7

#!/usr/local/bin/bash
while getopts "w:c:h:m:t:" Option
 do
  case $Option in
    w ) NOTIF_W=$OPTARG;;
    c ) NOTIF_C=$OPTARG;;
    h ) SRV_RSYNC=$OPTARG;;
    m ) MODULE_RSYNC=$OPTARG;;
    t ) TIME=$OPTARG;;
    * ) exit
  esac
done
TMP=`ls -l /tmp/ | grep $MODULE_RSYNC | awk '{print $3}'`
LOCKFILE=/tmp/$MODULE_RSYNC/$RANDOM.lock
STATUS=("OK" "CRITICAL" "WARNING" "UNKNOWN")
NAGIOS_OK=0
NAGIOS_WARNING=1
NAGIOS_CRITICAL=2
NAGIOS_UNKNOWN=3
    if [ -e $LOCKFILE ]; then
        echo "WARNING: $MODULE_RSYNC lock file still exists"
        exit $NAGIOS_WARNING
    fi
    if [[ -z $tmp || $tmp = 'root' ]]; then
        mkdir /tmp/$MODULE_RSYNC >/dev/null 2>&1
        chown nagios:wheel /tmp/$MODULE_RSYNC
    fi
touch $LOCKFILE >/dev/null 2>&1
float_val() {
     echo | awk 'END { print '"$1"'; }'
}
REMOTE=`cat /usr/local/$MODULE_RSYNC/var/remote.state | grep offset | awk '{print $2}'`
RSYNC=`rsync -az --exclude remote.state $SRV_RSYNC::$MODULE_RSYNC /tmp/$MODULE_RSYNC >/dev/null 2>&1`
    if [ $? -ne 0 ]; then
        exit $NAGIOS_UNKNOWN
    fi
LOCAL=`cat /tmp/$MODULE_RSYNC/local.state | grep offset | awk '{print $2}'`
STATE_RESULT=`expr $LOCAL - $REMOTE`
RESULT=$(float_val "$STATE_RESULT/$TIME")
    if [ $STATE_RESULT -lt 0 ]; then
        STATUS=${STATUS[1]}
        exitstatus=$NAGIOS_CRITICAL
        RESULT="negative replick"
    fi
    if [ -z $STATE_RESULT ]; then
        STATUS=${STATUS[3]}
        exitstatus=$NAGIOS_UNKNOWN
        RESULT=${STATUS[3]}
    else
        if [[ $RESULT > $NOTIF_W ]]; then
                if [[ $RESULT > $NOTIF_C ]]; then
                        STATUS=${STATUS[1]}
                        exitstatus=$NAGIOS_CRITICAL
                else
                        STATUS=${STATUS[2]}
                        exitstatus=$NAGIOS_WARNING
                fi
        else
                        STATUS=${STATUS[0]}
                        exitstatus=$NAGIOS_OK
        fi
    fi
echo $STATUS": $MODULE_RSYNC seconds behind master: "$RESULT
rm -r /tmp/$MODULE_RSYNC
exit $exitstatus