#! /usr/bin/perl -wT
# $Id: check_file_age.pl,v 1.2 2003/10/21 15:56:35 tonvoon Exp $

# check_file_age.pl Copyright (C) 2003 Steven Grimm <koreth-nagios@midwinter.com>
#
# Checks a file's size and modification time to make sure it's not empty
# and that it's sufficiently recent.
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# you should have received a copy of the GNU General Public License
# along with this program (or with Nagios);  if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA

use 5.004;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw($helpparameterlist $versionparameterlist $filenameparameter
			 $verboseparameter);

use English;

use File::stat;

use vars qw($opt_c $opt_f $opt_w $opt_C $opt_W $PROGNAME);

use utils qw (%ERRORS &usage);


$filenameparameter->name("file");
$filenameparameter->flags([ 'f', 'file' ]);
my $agewarningparameter = new Plugin::Parameter(-name => "agewarning", -flags => [ 'w', 'warning-age' ],
						-optional => "yes", -valueoptional => "no", -type => "INTEGER",
						-default => 240,
						-checker => \&Plugin::Parameter::_check_integer,
						-description => "Number of seconds beyond which the file is too old and a WARNING will be generated");
my $sizewarningparameter = new Plugin::Parameter(-name => "sizewarning", -flags => [ 'W', 'warning-size' ],
						 -optional => "yes", -valueoptional => "no", -type => "INTEGER",
						 -default => 0,
						 -checker => \&Plugin::Parameter::_check_integer,
						 -description => "Number of bytes for minimum size of file otherwise a WARNING will be generated");
my $agecriticalparameter = new Plugin::Parameter(-name => "agecritical", -flags => [ 'c', 'critical-age' ],
						-optional => "yes", -valueoptional => "no", -type => "INTEGER",
						-default => 600,
						-checker => \&Plugin::Parameter::_check_integer,
						-description => "Number of seconds beyond which the file is too old and a CRITICAL will be generated");
my $sizecriticalparameter = new Plugin::Parameter(-name => "sizecritical", -flags => [ 'C', 'critical-size' ],
						 -optional => "yes", -valueoptional => "no", -type => "INTEGER",
						 -default => 0,
						 -checker => \&Plugin::Parameter::_check_integer,
						 -description => "Number of bytes for minimum size of file otherwise a CRITICAL will be generated");
my @commandparameterlist = ( $filenameparameter,
			     $agewarningparameter,
			     $agecriticalparameter,
			     $sizewarningparameter,
			     $sizecriticalparameter,
			     $verboseparameter );
my $plugin = new Plugin(-revision => '$Revision: 1.2 $',
			-copyright => "2003 Steven Grimm, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Plugin checks the age and size of a file",
			-parameterlists => [ \@commandparameterlist, $helpparameterlist, $versionparameterlist ]);


$plugin->init();

my ($result, $message, $age, $size, $st);

$PROGNAME="check_file_age";

# Examine the file.
unless (-f $opt_f) {
  print "$PROGNAME UNKNOWN: $opt_f: File not found\n";
  exit $ERRORS{'UNKNOWN'};
}

$st = File::stat::stat($opt_f);
$age = time - $st->mtime;
$size = $st->size;

$result = 'OK';

if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
  $result = 'CRITICAL';
}
elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
  $result = 'WARNING';
}

print "$PROGNAME $result: $opt_f is $age seconds old and $size bytes\n";
exit $ERRORS{$result};
