Merge pull request #1311 from waja/check_ntp_remove_unused_variables
[monitoring-plugins.git] / plugins-scripts / check_file_age.pl
blob56b8e97c42c567a53e624905a662e49fb125f127
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_W = 0;
47 $opt_C = 0;
48 $opt_f = "";
50 Getopt::Long::Configure('bundling');
51 GetOptions(
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);
61 if ($opt_V) {
62 print_revision($PROGNAME, '@NP_VERSION@');
63 exit $ERRORS{'UNKNOWN'};
66 if ($opt_h) {
67 print_help();
68 exit $ERRORS{'UNKNOWN'};
71 $opt_f = shift unless ($opt_f);
73 if (! $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)
79 unless (-e $opt_f) {
80 if ($opt_i) {
81 $result = 'OK';
82 print "FILE_AGE $result: $opt_f doesn't exist, but ignore-missing was set\n";
83 exit $ERRORS{$result};
85 } else {
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;
93 $size = $st->size;
94 $perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0";
97 $result = 'OK';
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)) {
103 $result = 'WARNING';
106 print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes | $perfdata\n";
107 exit $ERRORS{$result};
109 sub print_usage () {
110 print "Usage:\n";
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";
116 sub print_help () {
117 print_revision($PROGNAME, '@NP_VERSION@');
118 print "Copyright (c) 2003 Steven Grimm\n\n";
119 print_usage();
120 print "\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";
124 print "\n";
125 support();