diff options
Diffstat (limited to 'web/attachments/40514-check_file_age')
-rw-r--r-- | web/attachments/40514-check_file_age | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/web/attachments/40514-check_file_age b/web/attachments/40514-check_file_age new file mode 100644 index 0000000..498c650 --- /dev/null +++ b/web/attachments/40514-check_file_age | |||
@@ -0,0 +1,112 @@ | |||
1 | #!/bin/perl -w | ||
2 | # $Id$ | ||
3 | |||
4 | # check_file.pl Copyright (C) 2003 Steven Grimm <koreth-nagios@midwinter.com> | ||
5 | # | ||
6 | # Checks a file's size and modification time to make sure it's not empty | ||
7 | # and that it's sufficiently recent. | ||
8 | # | ||
9 | # | ||
10 | # This program is free software; you can redistribute it and/or | ||
11 | # modify it under the terms of the GNU General Public License | ||
12 | # as published by the Free Software Foundation; either version 2 | ||
13 | # of the License, or (at your option) any later version. | ||
14 | # | ||
15 | # This program is distributed in the hope that it will be useful, | ||
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty | ||
17 | # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | # GNU General Public License for more details. | ||
19 | # | ||
20 | # you should have received a copy of the GNU General Public License | ||
21 | # along with this program (or with Nagios); if not, write to the | ||
22 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
23 | # Boston, MA 02111-1307, USA | ||
24 | |||
25 | use strict; | ||
26 | use English; | ||
27 | use Getopt::Long; | ||
28 | use File::stat; | ||
29 | use vars qw($PROGNAME); | ||
30 | use utils qw (%ERRORS &print_revision &support); | ||
31 | |||
32 | sub print_help (); | ||
33 | sub print_usage (); | ||
34 | |||
35 | my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V); | ||
36 | my ($result, $message, $age, $size, $st); | ||
37 | |||
38 | $PROGNAME="check_file"; | ||
39 | |||
40 | $opt_w = 240; | ||
41 | $opt_c = 600; | ||
42 | $opt_W = 0; | ||
43 | $opt_C = 0; | ||
44 | $opt_f = ""; | ||
45 | |||
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); | ||
55 | |||
56 | if ($opt_V) { | ||
57 | print_revision($PROGNAME, '$Id$'); | ||
58 | exit $ERRORS{'OK'}; | ||
59 | } | ||
60 | |||
61 | if ($opt_h) { | ||
62 | print_help(); | ||
63 | exit $ERRORS{'OK'}; | ||
64 | } | ||
65 | |||
66 | $opt_f = shift unless ($opt_f); | ||
67 | |||
68 | if (! $opt_f) { | ||
69 | print "No file specified\n"; | ||
70 | exit $ERRORS{'UNKNOWN'}; | ||
71 | } | ||
72 | |||
73 | # Examine the file. | ||
74 | unless (-f $opt_f) { | ||
75 | print "$opt_f: File not found\n"; | ||
76 | exit $ERRORS{'UNKNOWN'}; | ||
77 | } | ||
78 | |||
79 | $st = File::stat::stat($opt_f); | ||
80 | $age = time - $st->mtime; | ||
81 | $size = $st->size; | ||
82 | |||
83 | |||
84 | $result = 'OK'; | ||
85 | |||
86 | if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) { | ||
87 | $result = 'CRITICAL'; | ||
88 | } | ||
89 | elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) { | ||
90 | $result = 'WARNING'; | ||
91 | } | ||
92 | |||
93 | print "$result - $opt_f is $age seconds old and $size bytes\n"; | ||
94 | exit $ERRORS{$result}; | ||
95 | |||
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"; | ||
101 | } | ||
102 | |||
103 | sub print_help () { | ||
104 | print_revision($PROGNAME, '$Id$'); | ||
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\n"; | ||
109 | print " <size> File must be at least this many bytes long\n"; | ||
110 | print "\n"; | ||
111 | support(); | ||
112 | } | ||