Output plugin's stderr to stderr
[monitoring-plugins.git] / plugins-scripts / check_file_age.pl
blob01b854a637fe8c74f221063265753b5087c842ed
1 #!@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 FindBin;
29 use lib "$FindBin::Bin";
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, $opt_i);
36 my ($result, $message, $age, $size, $st, $perfdata);
38 $PROGNAME="check_file_age";
40 $ENV{'PATH'}='@TRUSTED_PATH@';
41 $ENV{'BASH_ENV'}='';
42 $ENV{'ENV'}='';
44 $opt_w = 240;
45 $opt_c = 600;
46 $opt_f = "";
48 Getopt::Long::Configure('bundling');
49 GetOptions(
50 "V" => \$opt_V, "version" => \$opt_V,
51 "h" => \$opt_h, "help" => \$opt_h,
52 "i" => \$opt_i, "ignore-missing" => \$opt_i,
53 "f=s" => \$opt_f, "file" => \$opt_f,
54 "w=s" => \$opt_w, "warning-age=s" => \$opt_w,
55 "W=s" => \$opt_W, "warning-size=s" => \$opt_W,
56 "c=s" => \$opt_c, "critical-age=s" => \$opt_c,
57 "C=s" => \$opt_C, "critical-size=s" => \$opt_C);
59 if ($opt_V) {
60 print_revision($PROGNAME, '@NP_VERSION@');
61 exit $ERRORS{'UNKNOWN'};
64 if ($opt_h) {
65 print_help();
66 exit $ERRORS{'UNKNOWN'};
69 $opt_f = shift unless ($opt_f);
71 if (! $opt_f) {
72 print "FILE_AGE UNKNOWN: No file specified\n";
73 exit $ERRORS{'UNKNOWN'};
76 # Check that file exists (can be directory or link)
77 unless (-e $opt_f) {
78 if ($opt_i) {
79 $result = 'OK';
80 print "FILE_AGE $result: $opt_f doesn't exist, but ignore-missing was set\n";
81 exit $ERRORS{$result};
83 } else {
84 print "FILE_AGE CRITICAL: File not found - $opt_f\n";
85 exit $ERRORS{'CRITICAL'};
89 $st = File::stat::stat($opt_f);
90 $age = time - $st->mtime;
91 $size = $st->size;
93 $result = 'OK';
95 if ($opt_c !~ m/^\d+$/ or ($opt_C and $opt_C !~ m/^\d+$/)
96 or $opt_w !~ m/^\d+$/ or ($opt_W and $opt_W !~ m/^\d+$/)) {
97 # range has been specified so use M::P::R to process
98 require Monitoring::Plugin::Range;
99 # use permissive range defaults for size when none specified
100 $opt_W = "0:" unless ($opt_W);
101 $opt_C = "0:" unless ($opt_C);
103 if (Monitoring::Plugin::Range->parse_range_string($opt_c)
104 ->check_range($age) == 1) { # 1 means it raises an alert because it's OUTSIDE the range
105 $result = 'CRITICAL';
107 elsif (Monitoring::Plugin::Range->parse_range_string($opt_C)
108 ->check_range($size) == 1) {
109 $result = 'CRITICAL';
111 elsif (Monitoring::Plugin::Range->parse_range_string($opt_w)
112 ->check_range($age) == 1) {
113 $result = 'WARNING';
115 elsif (Monitoring::Plugin::Range->parse_range_string($opt_W)
116 ->check_range($size) == 1) {
117 $result = 'WARNING';
120 else {
121 # use permissive defaults for size when none specified
122 $opt_W = 0 unless ($opt_W);
123 $opt_C = 0 unless ($opt_C);
124 if ($age > $opt_c or $size < $opt_C) {
125 $result = 'CRITICAL';
127 elsif ($age > $opt_w or $size < $opt_W) {
128 $result = 'WARNING';
132 $perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0";
133 print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes | $perfdata\n";
134 exit $ERRORS{$result};
136 sub print_usage () {
137 print "Usage:\n";
138 print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n";
139 print " $PROGNAME [-h | --help]\n";
140 print " $PROGNAME [-V | --version]\n";
143 sub print_help () {
144 print_revision($PROGNAME, '@NP_VERSION@');
145 print "Copyright (c) 2003 Steven Grimm\n\n";
146 print_usage();
147 print "\n";
148 print " -i | --ignore-missing : return OK if the file does not exist\n";
149 print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
150 print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n\n";
151 print " Both <secs> and <size> can specify a range using the standard plugin syntax\n";
152 print " If any of the warning and critical arguments are in range syntax (not just bare numbers)\n";
153 print " then all warning and critical arguments will be interpreted as ranges.\n";
154 print " To use range processing the perl module Monitoring::Plugin must be installed\n";
155 print " For range syntax see https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n";
156 print " It is strongly recommended when using range syntax that all four of -w, -W, -c and -C are specified\n";
157 print " otherwise it is unlikely that the size test will be doing what is desired\n";
158 print "\n";
159 support();