#! /usr/bin/perl
# Check if a file is too old
# anders.nordby@aftenposten.no, 2007-09-02

use Getopt::Std;

getopts('f:w:c:');

die("Use -f <file>") unless ($opt_f);
die("Use -c <critical seconds> and/or -w <warning seconds>") unless ($opt_c || $opt_w);

if ( -f $opt_f ) {
	$age = scalar(time) - (stat($opt_f))[9];
	if ($opt_c && $age > $opt_c) {
		print "File $opt_f is older than critical limit $opt_c.\n";
		exit(2);
	} elsif ($opt_w && $age > $opt_w) {
		print "File $opt_f is older than warning limit $opt_w.\n";
		exit(1);
	} else {
		print "File $opt_f OK, $age secs old.\n";
		exit(0);
	}
} else {
	print "File $opt_f does not exist, or is not a file.\n";
	exit(2);
}
