Thursday, January 22, 2009

Nagios Plugin to check file age on remote machine

You have to have a user account on the remote machine with ssh public and private key setup so that nagios user can login with our a password. The time is in seconds.

Nagios Command definition is as follows


define command{
command_name check_remote_fileage
command_line $USER1$/check_remote_fileage -H user@$HOSTADDRESS$ -f $ARG1$ -w $ARG2$ -c $ARG3$
}



Script


#!/bin/bash
if (( $# < 8 )); then
echo "Usage: $0 -H hostname -f remote file path -w warn -c crit"
exit 3
fi

while getopts "H:f:w:c:" OPT; do
case "$OPT" in
H) HOST=$OPTARG
;;
f) File=$OPTARG
;;
w) w=$OPTARG
;;
c) c=$OPTARG
;;
\?) echo "Error"
exit 3;
;;
esac
done

CURRTIME=$(ssh $HOST "perl -e 'print time;'")
FILETIME=$(ssh $HOST "perl -e 'printf "%d\n", ((stat(shift))[9]);' $File")
let "DIFFTIME = CURRTIME - FILETIME"
if (( $DIFFTIME >= $w && $DIFFTIME < $c)) ; then
echo "WARNING: $File is $DIFFTIME sec old (threshold w= $w) "
exit 1
elif (( $DIFFTIME >= $c)) ;then
echo "CRITICAL: $File is $DIFFTIME sec old (threshold c= $c) "
exit 2;
else
echo "OK: $File is with in threshold $w / $c "
exit 0
fi