1 #!/bin/perl -w
2 # $Id$
4 # check_file_age.pl Copyright (C) 2003 Steven Grimm <koreth-nagios@midwinter.com>
5 #
6 # Checks a file's size and modification time to make sure it's not empty
7 # and that it's sufficiently recent.
8 #
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty
17 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # you should have received a copy of the GNU General Public License
21 # along with this program (or with Nagios); if not, write to the
22 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 # Boston, MA 02111-1307, USA
25 use strict;
26 use English;
27 use Getopt::Long;
28 use File::stat;
29 use vars qw($PROGNAME);
30 use lib ".";
31 use utils qw (%ERRORS &print_revision &support);
33 sub print_help ();
34 sub print_usage ();
36 my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V);
37 my ($result, $message, $age, $size, $st);
39 $PROGNAME="check_file_age";
41 $opt_w = 240;
42 $opt_c = 600;
43 $opt_W = 0;
44 $opt_C = 0;
45 $opt_f = "";
47 Getopt::Long::Configure('bundling');
48 GetOptions(
49 "V" => \$opt_V, "version" => \$opt_V,
50 "h" => \$opt_h, "help" => \$opt_h,
51 "f=s" => \$opt_f, "file" => \$opt_f,
52 "w=f" => \$opt_w, "warning-age=f" => \$opt_w,
53 "W=f" => \$opt_W, "warning-size=f" => \$opt_W,
54 "c=f" => \$opt_c, "critical-age=f" => \$opt_c,
55 "C=f" => \$opt_C, "critical-size=f" => \$opt_C);
57 if ($opt_V) {
58 print_revision($PROGNAME, '$Revision$');
59 exit $ERRORS{'OK'};
60 }
62 if ($opt_h) {
63 print_help();
64 exit $ERRORS{'OK'};
65 }
67 $opt_f = shift unless ($opt_f);
69 if (! $opt_f) {
70 print "FILE_AGE UNKNOWN: No file specified\n";
71 exit $ERRORS{'UNKNOWN'};
72 }
74 # Check that file exists (can be directory or link)
75 unless (-e $opt_f) {
76 print "FILE_AGE CRITICAL: File not found - $opt_f\n";
77 exit $ERRORS{'CRITICAL'};
78 }
80 $st = File::stat::stat($opt_f);
81 $age = time - $st->mtime;
82 $size = $st->size;
85 $result = 'OK';
87 if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
88 $result = 'CRITICAL';
89 }
90 elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
91 $result = 'WARNING';
92 }
94 print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";
95 exit $ERRORS{$result};
97 sub print_usage () {
98 print "Usage:\n";
99 print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] -f <file>\n";
100 print " $PROGNAME [-h | --help]\n";
101 print " $PROGNAME [-V | --version]\n";
102 }
104 sub print_help () {
105 print_revision($PROGNAME, '$Revision$');
106 print "Copyright (c) 2003 Steven Grimm\n\n";
107 print_usage();
108 print "\n";
109 print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
110 print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";
111 print "\n";
112 support();
113 }