#!/usr/bin/ksh # author: alex harvey # email: alexh19740110@gmail.com # version: 0.1 # disclaimer: your backups are probably very important! Please # understand that this code you're using isn't mature and could # doubtlessly be imprved. It's set up in my environment and I # offer the code in the hope that others find it useful. # If you find any bugs or have any other suggestions, please # email me. # Notes on how to use this plugin are found in the attached file # 'check_backup_log.cfg' which you should save in the path # shown below. CONFIGFILE=/usr/local/nagios/etc/check_backup_log.cfg # binaries AWK=/bin/awk DATE=/bin/date GREP=/bin/grep LS=/bin/ls PS=/bin/ps # function: convert mins to hh:mm format min_to_time () { minutes=$1 hr=$(( $minutes / 60 )) mn=$(( $minutes - ( $hr * 60 ) )) [[ $mn -lt 10 ]] && mn="0${mn}" [[ $hr -lt 10 ]] && hr="0${hr}" echo "$hr:$mn" } # function: convert hh:mm format to mins time_to_min () { time=$1 hr=$(echo $time | sed -e 's/:..//') mn=$(echo $time | sed -e 's/..://') echo $(( $hr * 60 + $mn )) } # exit codes OK=0 WARNING=1 CRITICAL=2 UNKNOWN=3 # other constants ISODATE=`$DATE +20%y%m%d` YISODATE=`TZ=GMT+14; $DATE +20%y%m%d` DAY=`$DATE +%a` # MAIN # read in config file . $CONFIGFILE # time of day in minutes now_mins=$(time_to_min $($DATE +%H:%M)) # abort if today isn't a backup day if [[ $STARTMINS = -1 ]] then echo "OK - no backups on $DAY" exit $OK fi # backup hasn't started if [[ $now_mins -le $STARTMINS ]] then echo "OK - backup not scheduled to run yet" exit $OK fi # backup still running $PS -ef | $GREP $BACKUPSCRIPT | $GREP -v grep >/dev/null 2>&1 if [[ $? -eq 0 ]] then echo "OK - backup running" exit $OK fi # backup didn't create a log file? if [[ ! -a $LOGFILE ]] then echo "CRITICAL - there is no log file today!" exit $CRITICAL fi # get log file's timestamp finish_time=$($LS -l $LOGFILE | $AWK '{print $8}') finish_mins=$(time_to_min $finish_time) # gunzip if necessary the logfile last_three_chars=$(echo $LOGFILE | sed 's/^.*\(...\)$/\1/') if [[ "$last_three_chars" = ".gz" ]] then $GUNZIP $LOGFILE || \ ( echo "UNKNOWN - could not gunzip logfile"; exit $UNKNOWN ) fi # backup failed for pattern in $FAILPATTERNS do $GREP $pattern ${LOGFILE%\.gz} >/dev/null 2>&1 if [[ $? -eq 0 ]] then code=$(get_fail_code ${LOGFILE%\.gz}) echo "CRITICAL - today's backup failed (error code $code)" exit_code=$CRITICAL break # otherwise we get might multiple lines of output fi done # gzip the logfile again if necessary if [[ "$last_three_chars" = ".gz" ]] then $GZIP ${LOGFILE%\.gz} || \ ( echo "UNKNOWN - couldn't gzip logfile" && exit $UNKNOWN ) fi # exit here to ensure logfile was zipped again [[ ${exit_code:-0} -eq $CRITICAL ]] && exit $CRITICAL # backup finished early backup_run_time=$(( $finish_mins - $STARTMINS )) if [[ $backup_run_time -le $LOWERTHRESHOLD ]] then echo "WARNING - backup only ran for $backup_run_time mins (finished at $finish_time)" exit $WARNING fi # finished late if [[ $WARNONLATE = 1 ]] && [[ $backup_run_time -ge $UPPERTHRESHOLD ]] then echo "WARNING - backup ran for $backup_run_time mins (threshold = $UPPERTHRESHOLD mins)" exit $WARNING fi # else all must be okay echo "OK - backup finished at $finish_time" exit $OK # end of file