I wrote to scripts with the same idea, to monitor a file age, I have written it also in vbs in the past.
Now I did it in bash and php.
BASH Version, example:
check_file_age.sh /opt/solr-3.4.0/app/solrrioshow/conf/dataimport.properties 1440 1441
UPDATED: Its now all in minutes

#!/bin/bash
# Check file age
# By Felipe Ferreira
# updated 09/2012
warn=0
crit=0
if [ ! $# == 3 ]; then
   echo "nPlease provide 3 args:    ,example: n check_file_age.sh /var/log/messages 60 30"
   exit 1
else
   filename=$1
   warn=$2
   crit=$3
fi
# GET NOW TIME IN EPOCH FORMAT
timee=`date +%s`
#echo "Now EPOCH:               " $timee
if [ -f "$filename" ]; then
 lmod=`stat --format -%Y $filename | tr -d "-"`
# echo "Epoch File Last Modified: $lmod "
else
 echo "UNKOWN - File $filename does not exists"
 exit 3
fi
diff=`expr $timee - $lmod`
diffm=`expr $diff / 60`
#echo "Diference :  $diff sec / Diference :  $diffm min"
#echo "WARN: $warn CRIT: $crit DIFF: $diff"
if [ $diffm -ge $crit ]; then
 echo "CRITICAL - The file $filename is $diffm min. old"
 exit 2
elif [ $diffm -ge $warn ]; then
 echo "WARNING - The file $filename is $diffm min. old "
 exit 1
else
 echo "OK - The file $filename is $diffm min. old"
 exit 0
fi


PHP version

#!/usr/bin/php
   ,example: n check_file_age.php /var/log/messages 60 30 n n";
   exit(1);
} else {
   $filename = $argv[1];
   $warn = $argv[2] ;
   $crit = $argv[3];
}
# GET NOW TIME IN EPOCH FORMAT
$timee = time();
//echo "Epoch Now:                   " . $timee . "n";
$filename = '/data/Documents/scripts/php/test';
if (file_exists($filename)) {
	# GET LAST MODIFIED TIME OF FILE IN EPOCH FORMAT
	$stat = stat($filename);
	$lmod = $stat['mtime'];
//	echo "Epoch File Last Modified:    " . $lmod  . "n";
}
$diff = $timee - $lmod;
$diffm = ($diff/60);
$diffm = substr($diffm, 0, 2);
//echo "Diference : " . $diff . " secnDiference : " . $diffm . " minn" ;
if ( ($diff > $warn) && ($diff < $crit)) {
	echo "WARNING - The file $filename is $diffm min. old n";
	exit(1);
} elseif ($diff > $crit) {
	echo "CRITICAL - The file $filename is $diffm min. old n";
	exit(2);
} else {
	echo "OK - The file $filename is $diffm min. old n";
	exit(0);
}
?>
/pre>



This one is to check_file_age.sh how old is the latest file inside a directory, here is the script:
#!/bin/bash
# Check file age
# By Felipe Ferreira
warn=0
crit=0
if [ ! $# == 3 ]; then
   echo -e  "\n\nPlease provide 3 args:    ,example: \ncheck_file_age.php /tmp 60 30"
   echo -e "Will get oldest file in the directory\n\nBy Felipe Ferreira\n"
   exit 1
else
   dir=$1
   warn=$2
   crit=$3
fi
# GET NOW TIME IN EPOCH FORMAT
timee=`date +%s`
#echo "Now EPOCH:               " $timee
#check if folder exists and get oldest file
if [ -d "$dir" ]; then
 filename=$dir`ls -lsht $dir |tail -n 1 |awk {'print $NF'}`
else
 echo "CRITICAL - $filename directory not found"
 exit 2
fi
if [ -f "$filename" ]; then
 lmod=`stat --format -%Y $filename | tr -d "-"`
# echo "Epoch File Last Modified: $lmod "
else
 echo "OK - Nenhum arquivo no $dir"
 exit 0
fi
diff=`expr $timee - $lmod`
diffm=`expr $diff / 60`
##diffm=substr($diffm, 0, 2)
#echo "Diference :  $diff sec / Diference :  $diffm min"
#echo "WARN: $warn CRIT: $crit DIFF: $diff"
if [ $diffm -ge $crit ]; then
 echo "CRITICAL - The file $filename is $diffm min. old"
 exit 2
elif [ $diffm -ge $warn ]; then
 echo "WARNING - The file $filename is $diffm min. old "
 exit 1
else
 echo "OK - The file $filename is $diffm min. old"
 exit 0
fi
Tags: , , , , ,

1 thought on “check_file_age

  1. Hi,
    I’ve one problem, when I use it with sing file it works perfectly but I want to monitor file ages of multiple files wich are on remote path using Nagios ., any help would be greatly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *