Tips and tricks

Nagios script – fileage

#!/bin/bash

# Written by Johan Kindstrand <johan@kindstrands.se> in december 2014

s=7
folder=`date ‘+%y-%W’`
cdate=`date ‘+%s’`
file=$(ls -l –time-style=+%s /mnt/share/backup/ipcam/$folder/* | tail -1 | cut -d ‘ ‘ -f6,7)
lastfile=$( cut -d “/” -f $s- <<< “$file”)
TIMESTAMP=$(cut -d ‘ ‘ -f 1 <<< “$file”)
count=$(expr $cdate – $TIMESTAMP )
count1=$(expr $count / 3600 )
function PRINT_USAGE(){
echo “This Nagios plugin checks backup file :
-t HOURS maximal age in hours for the latest file before a warning is issued
-T HOURS maximal age in hours for the latest file before a critical alert is issued
-h prints out this help
You must specify warning time and critical time in hours.”
exit 0
}

WTIME=0;CTIME=0
declare -i CTIME
declare -i WTIME
while true ; do
getopts ‘t:T:h’ OPT
if [ “$OPT” = ‘?’ ] ; then break; fi;
case “$OPT” in
“t”) WTIME=”$OPTARG”;;
“T”) CTIME=”$OPTARG”;;
“h”) PRINT_USAGE;;
esac
done

if [ “$WTIME” = ‘0’ -a “$CTIME” = ‘0’ ] ; then
PRINT_USAGE
fi

if [ $count1 -gt $CTIME ] ; then
echo “CRITICAL – $lastfile $count1 hours old”
exit 2
fi

if [ $count1 -gt $WTIME ] ; then
echo “Warning – $lastfile $count1 hours old”
exit 1
fi
echo “OK – $lastfile $count1 hours old”

Skärmavbild 2014-12-29 kl. 17.18.48