#!/usr/bin/perl # # Check_FileAge Rel. 1.00 # # Copyleft 2004 IBCL # # Author: Christoph Lechleitner # # Please call # check_fileage --help # for usage information # use strict; # reading parameters my $doHelp=0; my $doVersion=0; my $unknownReasons=""; my $i=0; my $nrArgs=scalar(@ARGV); my $curArg=""; my $nextArg=""; my $warnAge=10; my $criticalAge=30; my $fileToTest=""; while ($i < $nrArgs) { $curArg=$ARGV[$i++]; if ( ($i) < $nrArgs ) { $nextArg=$ARGV[$i]; } else { $nextArg=""; } if ($curArg =~ m/^(-h|--help)$/) { $doHelp=1; next; } if ($curArg =~ m/^-c$/i) { if ($nextArg eq "") { $unknownReasons .= "Missing value to argument ".$curArg."\n"; } else { $criticalAge=0+$nextArg; if ($criticalAge <= 0) { $unknownReasons .= "Invalid value to argument ".$curArg."\n"; } } $i++; next; } if ($curArg =~ m/^-f$/i) { if ($nextArg eq "") { $unknownReasons .= "Missing value to argument ".$curArg."\n"; } else { $fileToTest=$nextArg; } $i++; next; } if ($curArg =~ m/^-w$/i) { if ($nextArg eq "") { $unknownReasons .= "Missing value to argument ".$curArg."\n"; } else { $warnAge=0+$nextArg; if ($warnAge <= 0) { $unknownReasons .= "Invalid value to argument ".$curArg."\n"; } } $i++; next; } if ($curArg =~ m/^(-v|--version)$/) { $doVersion=1; next; } } if ($doVersion) { print 'check_fileage 1.00 by Christoph Lechleitner '; exit 0; } if ($doHelp) { print ' FileAge check plugin for Nagios 1.x, Release 1.00, Copyleft 2004 by IBCL.at '.' Usage: check_fileage -f fileToCheck [-w warning] [-c critical] check_fileage -h|--help check_fileage -v|--version '.' Options: -c critical File age triggering CRITICAL state, given in minutes. Defaults to 30. -f fileToCheck Name of the file to check. -h, --help Prints this help text -v, --version Prints the version information -w warning File age triggering WARNING state, given in minutes. Defaults to 10. '.' Checks, if a file exists, is readable, and its age. If the file is missing or not readable, CRITICAL state is triggered. '.' If you have comments or suggestions, feel free to contact the Author: Christoph Lechleitner '; exit 0; } if ($fileToTest eq "") { $unknownReasons .= 'File to test not given (argument -f). Usage: check_fileage -f fileToCheck [-w warnMinutes] [-c criticalMinutes] check_fileage --help '; } if ($unknownReasons ne "") { print "FileAge UNKNOWN $unknownReasons"; exit 3; } if (-f $fileToTest) { my $filemdt=(lstat $fileToTest)[9]; my $now=time(); my $fileAge=($now-$filemdt)/60; if ($fileAge > $criticalAge) { print "FileAge CRITICAL $fileToTest is $fileAge minutes old.\n"; exit 2; } if ($fileAge > $warnAge) { print "FileAge WARNING $fileToTest is $fileAge minutes old.\n"; exit 1; } print "FileAge OK $fileToTest is only $fileAge minutes old.\n"; exit 0; } else { print "FileAge CRITICAL $fileToTest does not exist or is not readable.\n"; exit 2; }