|
| 1 | +#!/usr/bin/perl -w |
| 2 | + |
| 3 | +# check_file_age.pl Copyright (C) 2003 Steven Grimm <koreth-nagios@midwinter.com> |
| 4 | +# |
| 5 | +# Checks a file's size and modification time to make sure it's not empty |
| 6 | +# and that it's sufficiently recent. |
| 7 | +# |
| 8 | +# |
| 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. |
| 13 | +# |
| 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. |
| 18 | +# |
| 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., 51 Franklin Street, Fifth Floor, |
| 22 | +# Boston, MA 02110-1301, USA |
| 23 | + |
| 24 | +use strict; |
| 25 | +use English; |
| 26 | +use Getopt::Long; |
| 27 | +use File::stat; |
| 28 | +use vars qw($PROGNAME); |
| 29 | +use FindBin; |
| 30 | +use lib "$FindBin::Bin"; |
| 31 | +use lib '/usr/lib64/nagios/plugins'; |
| 32 | +use utils qw (%ERRORS &print_revision &support); |
| 33 | + |
| 34 | +sub print_help (); |
| 35 | +sub print_usage (); |
| 36 | + |
| 37 | +my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $opt_i); |
| 38 | +my ($result, $message, $age, $size, $st, $perfdata, $output, @filelist, $filename); |
| 39 | + |
| 40 | +$PROGNAME="check_file_age"; |
| 41 | + |
| 42 | +$ENV{'PATH'}='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'; |
| 43 | +$ENV{'BASH_ENV'}=''; |
| 44 | +$ENV{'ENV'}=''; |
| 45 | + |
| 46 | +$opt_w = 240; |
| 47 | +$opt_c = 600; |
| 48 | +$opt_W = 0; |
| 49 | +$opt_C = 0; |
| 50 | +$opt_f = ""; |
| 51 | + |
| 52 | +Getopt::Long::Configure('bundling'); |
| 53 | +GetOptions( |
| 54 | + "V" => \$opt_V, "version" => \$opt_V, |
| 55 | + "h" => \$opt_h, "help" => \$opt_h, |
| 56 | + "i" => \$opt_i, "ignore-missing" => \$opt_i, |
| 57 | + "f=s" => \$opt_f, "file" => \$opt_f, |
| 58 | + "w=f" => \$opt_w, "warning-age=f" => \$opt_w, |
| 59 | + "W=f" => \$opt_W, "warning-size=f" => \$opt_W, |
| 60 | + "c=f" => \$opt_c, "critical-age=f" => \$opt_c, |
| 61 | + "C=f" => \$opt_C, "critical-size=f" => \$opt_C); |
| 62 | + |
| 63 | +if ($opt_V) { |
| 64 | + print_revision($PROGNAME, '2.2.1'); |
| 65 | + exit $ERRORS{'OK'}; |
| 66 | +} |
| 67 | + |
| 68 | +if ($opt_h) { |
| 69 | + print_help(); |
| 70 | + exit $ERRORS{'OK'}; |
| 71 | +} |
| 72 | + |
| 73 | +$opt_f = shift unless ($opt_f); |
| 74 | + |
| 75 | +if (! $opt_f) { |
| 76 | + print "FILE_AGE UNKNOWN: No file specified\n"; |
| 77 | + exit $ERRORS{'UNKNOWN'}; |
| 78 | +} |
| 79 | + |
| 80 | +$opt_f = '"' . $opt_f . '"' if $opt_f =~ / /; |
| 81 | + |
| 82 | +# Check that file(s) exists (can be directory or link) |
| 83 | +$perfdata = ""; |
| 84 | +$output = ""; |
| 85 | +@filelist = glob($opt_f); |
| 86 | + |
| 87 | +foreach $filename (@filelist) { |
| 88 | + unless (-e $filename) { |
| 89 | + if ($opt_i) { |
| 90 | + $result = 'OK'; |
| 91 | + print "FILE_AGE $result: $filename doesn't exist, but ignore-missing was set\n"; |
| 92 | + exit $ERRORS{$result}; |
| 93 | + |
| 94 | + } else { |
| 95 | + print "FILE_AGE CRITICAL: File not found - $filename\n"; |
| 96 | + exit $ERRORS{'CRITICAL'}; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + $st = File::stat::stat($filename); |
| 101 | + $age = time - $st->mtime; |
| 102 | + $size = $st->size; |
| 103 | + $perfdata = $perfdata . "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0 "; |
| 104 | + |
| 105 | + $result = 'OK'; |
| 106 | + |
| 107 | + if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) { |
| 108 | + $result = 'CRITICAL'; |
| 109 | + } |
| 110 | + elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) { |
| 111 | + $result = 'WARNING'; |
| 112 | + } |
| 113 | + |
| 114 | + $output = $output . "FILE_AGE $result: $filename is $age seconds old and $size bytes "; |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +print "$output | $perfdata\n"; |
| 119 | + |
| 120 | +exit $ERRORS{$result}; |
| 121 | + |
| 122 | +sub print_usage () { |
| 123 | + print "Usage:\n"; |
| 124 | + print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n"; |
| 125 | + print " $PROGNAME [-h | --help]\n"; |
| 126 | + print " $PROGNAME [-V | --version]\n"; |
| 127 | +} |
| 128 | + |
| 129 | +sub print_help () { |
| 130 | + print_revision($PROGNAME, '2.2.1'); |
| 131 | + print "Copyright (c) 2003 Steven Grimm\n\n"; |
| 132 | + print_usage(); |
| 133 | + print "\n"; |
| 134 | + print " -i | --ignore-missing : return OK if the file does not exist\n"; |
| 135 | + print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n"; |
| 136 | + print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n"; |
| 137 | + print "\n"; |
| 138 | + support(); |
| 139 | +} |
0 commit comments