summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Wagner <waja@cyconet.org>2013-10-01 01:38:45 +0200
committerJan Wagner <waja@cyconet.org>2014-01-24 20:43:01 +0100
commit0b9b300f856d021b56950030f4e6053d84bc77c8 (patch)
tree57191e44659a3306616788d63a9ec977d13fd0c1
parent455fe96e7dcadd433973b1709ee79cdb58ffe428 (diff)
downloadmonitoring-plugins-0b9b300f856d021b56950030f4e6053d84bc77c8.tar.gz
check_file_age: support for --ignore-missing
Patch to provide for a -i | --ignore-missing option. This allows files that don't exist to be monitored for without causing an error. The use case is: * one process periodically pushes files to an "input" queue for a second process * the second process, when running cleanly will "consume" (process and remove) these files So the conditions to meet are: 1. If the file doesn't exist, that's fine (OK) 2. If the file exists, but is less than X seconds, that's fine (OK) 3. If the file exists, but greater than X seconds, no good (CRITICAL) Since now check_file_age doesn't permit this behavior, as a non-existent file is considered CRITICAL. Test case was contributed by our lovely Holger. Closes #1181, Closes #989, Closes #862, Closes #1187, Closes #845 Closes Debian Bug #496307
-rwxr-xr-xplugins-scripts/check_file_age.pl17
-rw-r--r--plugins-scripts/t/check_file_age.t7
2 files changed, 19 insertions, 5 deletions
diff --git a/plugins-scripts/check_file_age.pl b/plugins-scripts/check_file_age.pl
index 4cdb36db..2781c2ac 100755
--- a/plugins-scripts/check_file_age.pl
+++ b/plugins-scripts/check_file_age.pl
@@ -31,7 +31,7 @@ use utils qw (%ERRORS &print_revision &support);
31sub print_help (); 31sub print_help ();
32sub print_usage (); 32sub print_usage ();
33 33
34my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V); 34my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $opt_i);
35my ($result, $message, $age, $size, $st); 35my ($result, $message, $age, $size, $st);
36 36
37$PROGNAME="check_file_age"; 37$PROGNAME="check_file_age";
@@ -46,6 +46,7 @@ Getopt::Long::Configure('bundling');
46GetOptions( 46GetOptions(
47 "V" => \$opt_V, "version" => \$opt_V, 47 "V" => \$opt_V, "version" => \$opt_V,
48 "h" => \$opt_h, "help" => \$opt_h, 48 "h" => \$opt_h, "help" => \$opt_h,
49 "i" => \$opt_i, "ignore-missing" => \$opt_i,
49 "f=s" => \$opt_f, "file" => \$opt_f, 50 "f=s" => \$opt_f, "file" => \$opt_f,
50 "w=f" => \$opt_w, "warning-age=f" => \$opt_w, 51 "w=f" => \$opt_w, "warning-age=f" => \$opt_w,
51 "W=f" => \$opt_W, "warning-size=f" => \$opt_W, 52 "W=f" => \$opt_W, "warning-size=f" => \$opt_W,
@@ -71,8 +72,15 @@ if (! $opt_f) {
71 72
72# Check that file exists (can be directory or link) 73# Check that file exists (can be directory or link)
73unless (-e $opt_f) { 74unless (-e $opt_f) {
74 print "FILE_AGE CRITICAL: File not found - $opt_f\n"; 75 if ($opt_i) {
75 exit $ERRORS{'CRITICAL'}; 76 $result = 'OK';
77 print "FILE_AGE $result: $opt_f doesn't exist, but ignore-missing was set\n";
78 exit $ERRORS{$result};
79
80 } else {
81 print "FILE_AGE CRITICAL: File not found - $opt_f\n";
82 exit $ERRORS{'CRITICAL'};
83 }
76} 84}
77 85
78$st = File::stat::stat($opt_f); 86$st = File::stat::stat($opt_f);
@@ -94,7 +102,7 @@ exit $ERRORS{$result};
94 102
95sub print_usage () { 103sub print_usage () {
96 print "Usage:\n"; 104 print "Usage:\n";
97 print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] -f <file>\n"; 105 print " $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] [-i] -f <file>\n";
98 print " $PROGNAME [-h | --help]\n"; 106 print " $PROGNAME [-h | --help]\n";
99 print " $PROGNAME [-V | --version]\n"; 107 print " $PROGNAME [-V | --version]\n";
100} 108}
@@ -104,6 +112,7 @@ sub print_help () {
104 print "Copyright (c) 2003 Steven Grimm\n\n"; 112 print "Copyright (c) 2003 Steven Grimm\n\n";
105 print_usage(); 113 print_usage();
106 print "\n"; 114 print "\n";
115 print " -i | --ignore-missing : return OK if the file does not exist\n";
107 print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n"; 116 print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
108 print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n"; 117 print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";
109 print "\n"; 118 print "\n";
diff --git a/plugins-scripts/t/check_file_age.t b/plugins-scripts/t/check_file_age.t
index 384c276b..a5156491 100644
--- a/plugins-scripts/t/check_file_age.t
+++ b/plugins-scripts/t/check_file_age.t
@@ -5,7 +5,7 @@
5# 5#
6 6
7use strict; 7use strict;
8use Test::More tests => 15; 8use Test::More tests => 16;
9use NPTest; 9use NPTest;
10 10
11my $successOutput = '/^FILE_AGE OK: /'; 11my $successOutput = '/^FILE_AGE OK: /';
@@ -58,6 +58,11 @@ $result = NPTest->testCmd(
58cmp_ok( $result->return_code, '==', 0, "Checking file size" ); 58cmp_ok( $result->return_code, '==', 0, "Checking file size" );
59 59
60$result = NPTest->testCmd( 60$result = NPTest->testCmd(
61 "./check_file_age -f /non/existent --ignore-missing"
62 );
63cmp_ok( $result->return_code, '==', 0, "Honours --ignore-missing" );
64
65$result = NPTest->testCmd(
61 "./check_file_age -f $temp_file -c 1000 -W 101" 66 "./check_file_age -f $temp_file -c 1000 -W 101"
62 ); 67 );
63cmp_ok( $result->return_code, '==', 1, "One byte too short" ); 68cmp_ok( $result->return_code, '==', 1, "One byte too short" );