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 lib "$FindBin::Bin";
30 use utils qw (%ERRORS &print_revision &support);
35 my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $opt_i);
36 my ($result, $message, $age, $size, $st, $perfdata);
38 $PROGNAME="check_file_age";
40 $ENV{'PATH'}='@TRUSTED_PATH@';
50 Getopt::Long::Configure('bundling');
52 "V" => \$opt_V, "version" => \$opt_V,
53 "h" => \$opt_h, "help" => \$opt_h,
54 "i" => \$opt_i, "ignore-missing" => \$opt_i,
55 "f=s" => \$opt_f, "file" => \$opt_f,
56 "w=f" => \$opt_w, "warning-age=f" => \$opt_w,
57 "W=f" => \$opt_W, "warning-size=f" => \$opt_W,
58 "c=f" => \$opt_c, "critical-age=f" => \$opt_c,
59 "C=f" => \$opt_C, "critical-size=f" => \$opt_C);
62 print_revision($PROGNAME, '@NP_VERSION@');
63 exit $ERRORS{'UNKNOWN'};
68 exit $ERRORS{'UNKNOWN'};
71 $opt_f = shift unless ($opt_f);
74 print "FILE_AGE UNKNOWN: No file specified\n";
75 exit $ERRORS{'UNKNOWN'};
78 # Check that file exists (can be directory or link)
82 print "FILE_AGE $result: $opt_f doesn't exist, but ignore-missing was set\n";
83 exit $ERRORS{$result};
86 print "FILE_AGE CRITICAL: File not found - $opt_f\n";
87 exit $ERRORS{'CRITICAL'};
91 $st = File::stat::stat($opt_f);
92 $age = time - $st->mtime;
94 $perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0";
99 if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
100 $result = 'CRITICAL';
102 elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
106 print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes | $perfdata\n";
107 exit $ERRORS{$result};
111 print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n";
112 print " $PROGNAME [-h | --help]\n";
113 print " $PROGNAME [-V | --version]\n";
117 print_revision($PROGNAME, '@NP_VERSION@');
118 print "Copyright (c) 2003 Steven Grimm\n\n";
121 print " -i | --ignore-missing : return OK if the file does not exist\n";
122 print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
123 print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";