Merge remote-tracking branch 'awiddersheim/fix_trusted_path'
[monitoring-plugins.git] / plugins-scripts / check_file_age.pl
blobe0280381e2c0b5fa1ea00fb4c77e6c8100f282cf
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 if not, write to the Free Software Foundation,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 use strict;
24 use English;
25 use Getopt::Long;
26 use File::stat;
27 use vars qw($PROGNAME);
28 use lib ".";
29 use utils qw (%ERRORS &print_revision &support);
31 sub print_help ();
32 sub print_usage ();
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@';
40 $ENV{'BASH_ENV'}='';
41 $ENV{'ENV'}='';
43 $opt_w = 240;
44 $opt_c = 600;
45 $opt_W = 0;
46 $opt_C = 0;
47 $opt_f = "";
49 Getopt::Long::Configure('bundling');
50 GetOptions(
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);
60 if ($opt_V) {
61 print_revision($PROGNAME, '@NP_VERSION@');
62 exit $ERRORS{'OK'};
65 if ($opt_h) {
66 print_help();
67 exit $ERRORS{'OK'};
70 $opt_f = shift unless ($opt_f);
72 if (! $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)
78 unless (-e $opt_f) {
79 if ($opt_i) {
80 $result = 'OK';
81 print "FILE_AGE $result: $opt_f doesn't exist, but ignore-missing was set\n";
82 exit $ERRORS{$result};
84 } else {
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;
92 $size = $st->size;
95 $result = 'OK';
97 if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
98 $result = 'CRITICAL';
100 elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
101 $result = 'WARNING';
104 print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";
105 exit $ERRORS{$result};
107 sub print_usage () {
108 print "Usage:\n";
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";
114 sub print_help () {
115 print_revision($PROGNAME, '@NP_VERSION@');
116 print "Copyright (c) 2003 Steven Grimm\n\n";
117 print_usage();
118 print "\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";
122 print "\n";
123 support();