summaryrefslogtreecommitdiffstats
path: root/web/attachments/169141-check_file_age.pl.patch
blob: 857e11c9652e058aaf2923fd39e98970294d27fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
diff -r -U1 nagios-plugins-HEAD-200602280052-orig/plugins-scripts/check_file_age.pl nagios-plugins-HEAD-200602280052/plugins-scripts/check_file_age.pl
--- nagios-plugins-HEAD-200602280052-orig/plugins-scripts/check_file_age.pl	2005-12-15 16:17:49.000000000 +0100
+++ nagios-plugins-HEAD-200602280052/plugins-scripts/check_file_age.pl	2006-02-28 12:10:21.000000000 +0100
@@ -35,4 +35,4 @@
 
-my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V);
-my ($result, $message, $age, $size, $st);
+my ($opt_c, $opt_f, $opt_d, $opt_n, $opt_w, $opt_r, $opt_C, $opt_W, $opt_h, $opt_V);
+my ($result, $message, $age, $size, $st, @files, @failed, $file);
 
@@ -45,2 +45,5 @@
 $opt_f = "";
+$opt_n = 1;
+$opt_d = "";
+$opt_r = "";
 
@@ -50,3 +53,6 @@
 	"h"   => \$opt_h, "help"	=> \$opt_h,
+	"d"   => \$opt_d, "dir-content"	=> \$opt_d,
 	"f=s" => \$opt_f, "file"	=> \$opt_f,
+	"n=i" => \$opt_n, "num-files=i"	=> \$opt_n,
+	"r=s" => \$opt_r, "regexp=s"	=> \$opt_r,
 	"w=f" => \$opt_w, "warning-age=f" => \$opt_w,
@@ -79,17 +85,54 @@
 
-$st = File::stat::stat($opt_f);
-$age = time - $st->mtime;
-$size = $st->size;
-
+if ($opt_d) {
+    if (!opendir(DIR, $opt_f)) {
+        print "FILE_AGE CRITICAL: $opt_f: $!";
+        exit $ERRORS{'CRITICAL'};
+    }
+    # remove . and .. and filter regular expression
+    @files = grep { !/^\.{1,2}$/ } grep { /$opt_r/ } readdir(DIR);
+    # print "@files";
+    closedir(DIR);
+} else {
+    @files = ($opt_f);
+}
+
+my $crit = 0;
+my $warn = 0;
+foreach $file (@files) {
+    if ($opt_d) {
+        $file = "$opt_f/$file";
+    }
+    $st = File::stat::stat("$file");
+    $age = time - $st->mtime;
+    $size = $st->size;
+
+    if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
+        push (@failed,$file);
+        $crit++;
+    }
+    elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
+        push (@failed,$file);
+        $warn++;
+    }
+}
+
+# return maximum RC if the sum of nOK files exceeds $opt_n
+if ($warn + $crit >= $opt_n) {
+    if ($crit > 0 ) {
+        $result = 'CRITICAL';
+    } else {
+        $result = 'WARNING';
+    }
+} else {
+    $result = 'OK';
+}
 
-$result = 'OK';
 
-if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
-	$result = 'CRITICAL';
+if ( ! $opt_d ) {
+    print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";
+} else {
+    my $failed = @failed;
+    print "FILE_AGE $result: @failed " . $failed ." files older/smaller than threshold thres: $opt_n\n";
 }
-elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
-	$result = 'WARNING';
-}
-
-print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";
+    
 exit $ERRORS{$result};
@@ -98,3 +141,3 @@
 	print "Usage:\n";
-	print "  $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] -f <file>\n";
+	print "  $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-d [-r <regexp>] [-n <num>]  -f <file>\n";
 	print "  $PROGNAME [-h | --help]\n";
@@ -110,2 +153,5 @@
 	print "  <size>  File must be at least this many bytes long (default: crit 0 bytes)\n";
+	print "  -d check all files in <file> (if <file> is a directory)\n";
+	print "  -r only check files maching regular expression in directory (only if -d is given)\n";
+	print "  -n minimum number of files that have to be crit/warn to reach non OK state\n";
 	print "\n";