Make socket alarm handler customisable (like c459ca07)
[monitoring-plugins.git] / plugins-scripts / check_file_age.pl
blobdcd5efab8305983cda8c529be2f25b52cbf0d1bd
1 #!/bin/perl -w
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 (or with Nagios); if not, write to the
21 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 # Boston, MA 02111-1307, USA
24 use strict;
25 use English;
26 use Getopt::Long;
27 use File::stat;
28 use vars qw($PROGNAME);
29 use lib ".";
30 use utils qw (%ERRORS &print_revision &support);
32 sub print_help ();
33 sub print_usage ();
35 my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V);
36 my ($result, $message, $age, $size, $st);
38 $PROGNAME="check_file_age";
40 $opt_w = 240;
41 $opt_c = 600;
42 $opt_W = 0;
43 $opt_C = 0;
44 $opt_f = "";
46 Getopt::Long::Configure('bundling');
47 GetOptions(
48 "V" => \$opt_V, "version" => \$opt_V,
49 "h" => \$opt_h, "help" => \$opt_h,
50 "f=s" => \$opt_f, "file" => \$opt_f,
51 "w=f" => \$opt_w, "warning-age=f" => \$opt_w,
52 "W=f" => \$opt_W, "warning-size=f" => \$opt_W,
53 "c=f" => \$opt_c, "critical-age=f" => \$opt_c,
54 "C=f" => \$opt_C, "critical-size=f" => \$opt_C);
56 if ($opt_V) {
57 print_revision($PROGNAME, '@NP_VERSION@');
58 exit $ERRORS{'OK'};
61 if ($opt_h) {
62 print_help();
63 exit $ERRORS{'OK'};
66 $opt_f = shift unless ($opt_f);
68 if (! $opt_f) {
69 print "FILE_AGE UNKNOWN: No file specified\n";
70 exit $ERRORS{'UNKNOWN'};
73 # Check that file exists (can be directory or link)
74 unless (-e $opt_f) {
75 print "FILE_AGE CRITICAL: File not found - $opt_f\n";
76 exit $ERRORS{'CRITICAL'};
79 $st = File::stat::stat($opt_f);
80 $age = time - $st->mtime;
81 $size = $st->size;
84 $result = 'OK';
86 if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
87 $result = 'CRITICAL';
89 elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
90 $result = 'WARNING';
93 print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";
94 exit $ERRORS{$result};
96 sub print_usage () {
97 print "Usage:\n";
98 print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] -f <file>\n";
99 print " $PROGNAME [-h | --help]\n";
100 print " $PROGNAME [-V | --version]\n";
103 sub print_help () {
104 print_revision($PROGNAME, '@NP_VERSION@');
105 print "Copyright (c) 2003 Steven Grimm\n\n";
106 print_usage();
107 print "\n";
108 print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
109 print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";
110 print "\n";
111 support();