3 # check_file_age.pl Copyright (C) 2003 Steven Grimm <koreth-nagios@midwinter.com>
5 # Checks a file's size and modification time to make sure it's not empty
6 # and that it's sufficiently recent.
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty
16 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # you should have received a copy of the GNU General Public License
20 # along with this program if not, write to the Free Software Foundation,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 use vars
qw($PROGNAME);
29 use utils qw (%ERRORS &print_revision &support);
34 my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $opt_i);
35 my ($result, $message, $age, $size, $st);
37 $PROGNAME="check_file_age";
39 $ENV{'PATH'}='@trusted_path@';
49 Getopt::Long::Configure('bundling');
51 "V" => \$opt_V, "version" => \$opt_V,
52 "h" => \$opt_h, "help" => \$opt_h,
53 "i" => \$opt_i, "ignore-missing" => \$opt_i,
54 "f=s" => \$opt_f, "file" => \$opt_f,
55 "w=f" => \$opt_w, "warning-age=f" => \$opt_w,
56 "W=f" => \$opt_W, "warning-size=f" => \$opt_W,
57 "c=f" => \$opt_c, "critical-age=f" => \$opt_c,
58 "C=f" => \$opt_C, "critical-size=f" => \$opt_C);
61 print_revision($PROGNAME, '@NP_VERSION@');
70 $opt_f = shift unless ($opt_f);
73 print "FILE_AGE UNKNOWN: No file specified\n";
74 exit $ERRORS{'UNKNOWN'};
77 # Check that file exists (can be directory or link)
81 print "FILE_AGE $result: $opt_f doesn't exist, but ignore-missing was set\n";
82 exit $ERRORS{$result};
85 print "FILE_AGE CRITICAL: File not found - $opt_f\n";
86 exit $ERRORS{'CRITICAL'};
90 $st = File::stat::stat($opt_f);
91 $age = time - $st->mtime;
97 if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
100 elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
104 print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";
105 exit $ERRORS{$result};
109 print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n";
110 print " $PROGNAME [-h | --help]\n";
111 print " $PROGNAME [-V | --version]\n";
115 print_revision($PROGNAME, '@NP_VERSION@');
116 print "Copyright (c) 2003 Steven Grimm\n\n";
119 print " -i | --ignore-missing : return OK if the file does not exist\n";
120 print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
121 print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";