summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorphowen <phowen@cisco.com>2017-02-14 15:19:18 (GMT)
committerphowen <phowen@cisco.com>2017-02-14 15:19:18 (GMT)
commit98a365d76c9b7e270d7b00953905d80c80c1467d (patch)
tree57c4a7a17bd138344b31de2f8f2de7e0f4ed78d1
parent0d73b499dd939924d49f955ac84550fab5f3cdde (diff)
downloadmonitoring-plugins-98a365d.tar.gz
add range checking to check_file_age
-rwxr-xr-xplugins-scripts/check_file_age.pl63
-rw-r--r--plugins-scripts/t/check_file_age.t46
2 files changed, 93 insertions, 16 deletions
diff --git a/plugins-scripts/check_file_age.pl b/plugins-scripts/check_file_age.pl
index 56b8e97..dc49ce7 100755
--- a/plugins-scripts/check_file_age.pl
+++ b/plugins-scripts/check_file_age.pl
@@ -43,8 +43,6 @@ $ENV{'ENV'}='';
43 43
44$opt_w = 240; 44$opt_w = 240;
45$opt_c = 600; 45$opt_c = 600;
46$opt_W = 0;
47$opt_C = 0;
48$opt_f = ""; 46$opt_f = "";
49 47
50Getopt::Long::Configure('bundling'); 48Getopt::Long::Configure('bundling');
@@ -53,13 +51,13 @@ GetOptions(
53 "h" => \$opt_h, "help" => \$opt_h, 51 "h" => \$opt_h, "help" => \$opt_h,
54 "i" => \$opt_i, "ignore-missing" => \$opt_i, 52 "i" => \$opt_i, "ignore-missing" => \$opt_i,
55 "f=s" => \$opt_f, "file" => \$opt_f, 53 "f=s" => \$opt_f, "file" => \$opt_f,
56 "w=f" => \$opt_w, "warning-age=f" => \$opt_w, 54 "w=s" => \$opt_w, "warning-age=s" => \$opt_w,
57 "W=f" => \$opt_W, "warning-size=f" => \$opt_W, 55 "W=s" => \$opt_W, "warning-size=s" => \$opt_W,
58 "c=f" => \$opt_c, "critical-age=f" => \$opt_c, 56 "c=s" => \$opt_c, "critical-age=s" => \$opt_c,
59 "C=f" => \$opt_C, "critical-size=f" => \$opt_C); 57 "C=s" => \$opt_C, "critical-size=s" => \$opt_C);
60 58
61if ($opt_V) { 59if ($opt_V) {
62 print_revision($PROGNAME, '@NP_VERSION@'); 60 print_revision($PROGNAME, '2.2.22.g0d73b');
63 exit $ERRORS{'UNKNOWN'}; 61 exit $ERRORS{'UNKNOWN'};
64} 62}
65 63
@@ -91,18 +89,47 @@ unless (-e $opt_f) {
91$st = File::stat::stat($opt_f); 89$st = File::stat::stat($opt_f);
92$age = time - $st->mtime; 90$age = time - $st->mtime;
93$size = $st->size; 91$size = $st->size;
94$perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0";
95
96 92
97$result = 'OK'; 93$result = 'OK';
98 94
99if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) { 95if ($opt_c !~ m/^\d+$/ or ($opt_C and $opt_C !~ m/^\d+$/)
100 $result = 'CRITICAL'; 96 or $opt_w !~ m/^\d+$/ or ($opt_W and $opt_W !~ m/^\d+$/)) {
97 # range has been specified so use M::P::R to process
98 require Monitoring::Plugin::Range;
99 # use permissive range defaults for size when none specified
100 $opt_W = "0:" unless ($opt_W);
101 $opt_C = "0:" unless ($opt_C);
102
103 if (Monitoring::Plugin::Range->parse_range_string($opt_c)
104 ->check_range($age) == 1) { # 1 means it raises an alert because it's OUTSIDE the range
105 $result = 'CRITICAL';
106 }
107 elsif (Monitoring::Plugin::Range->parse_range_string($opt_C)
108 ->check_range($size) == 1) {
109 $result = 'CRITICAL';
110 }
111 elsif (Monitoring::Plugin::Range->parse_range_string($opt_w)
112 ->check_range($age) == 1) {
113 $result = 'WARNING';
114 }
115 elsif (Monitoring::Plugin::Range->parse_range_string($opt_W)
116 ->check_range($size) == 1) {
117 $result = 'WARNING';
118 }
101} 119}
102elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) { 120else {
103 $result = 'WARNING'; 121 # use permissive defaults for size when none specified
122 $opt_W = 0 unless ($opt_W);
123 $opt_C = 0 unless ($opt_C);
124 if ($age > $opt_c or $size < $opt_C) {
125 $result = 'CRITICAL';
126 }
127 elsif ($age > $opt_w or $size < $opt_W) {
128 $result = 'WARNING';
129 }
104} 130}
105 131
132$perfdata = "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0";
106print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes | $perfdata\n"; 133print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes | $perfdata\n";
107exit $ERRORS{$result}; 134exit $ERRORS{$result};
108 135
@@ -120,7 +147,15 @@ sub print_help () {
120 print "\n"; 147 print "\n";
121 print " -i | --ignore-missing : return OK if the file does not exist\n"; 148 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"; 149 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"; 150 print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n\n";
151 print " Both <secs> and <size> can specify a range using the standard plugin syntax\n";
152 print " If any of the warning and critical arguments are in range syntax (not just bare numbers)\n";
153 print " then all warning and critical arguments will be interpreted as ranges.\n";
154 print " To use range processing the perl module Monitoring::Plugin must be installed\n";
155 print " For range syntax see https://nagios-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n";
156 print " It is strongly recommended when using range syntax that all four of -w, -W, -c and -C are specified\n";
157 print " otherwise it is unlikely that the size test will be doint wat is desired\n";
124 print "\n"; 158 print "\n";
125 support(); 159 support();
126} 160}
161
diff --git a/plugins-scripts/t/check_file_age.t b/plugins-scripts/t/check_file_age.t
index 50a2e69..ebf673f 100644
--- a/plugins-scripts/t/check_file_age.t
+++ b/plugins-scripts/t/check_file_age.t
@@ -5,14 +5,14 @@
5# 5#
6 6
7use strict; 7use strict;
8use Test::More tests => 17; 8use Test::More tests => 27;
9use NPTest; 9use NPTest;
10 10
11my $successOutput = '/^FILE_AGE OK: /'; 11my $successOutput = '/^FILE_AGE OK: /';
12my $warningOutput = '/^FILE_AGE WARNING: /'; 12my $warningOutput = '/^FILE_AGE WARNING: /';
13my $criticalOutput = '/^FILE_AGE CRITICAL: /'; 13my $criticalOutput = '/^FILE_AGE CRITICAL: /';
14my $unknownOutput = '/^FILE_AGE UNKNOWN: /'; 14my $unknownOutput = '/^FILE_AGE UNKNOWN: /';
15my $performanceOutput = '/ \| age=[0-9]+s;[0-9]+;[0-9]+ size=[0-9]+B;[0-9]+;[0-9]+;0$/'; 15my $performanceOutput = '/ \| age=[0-9]+s;[0-9:]+;[0-9:]+ size=[0-9]+B;[0-9:]+;[0-9:]+;0$/';
16 16
17my $result; 17my $result;
18my $temp_file = "/tmp/check_file_age.tmp"; 18my $temp_file = "/tmp/check_file_age.tmp";
@@ -48,22 +48,44 @@ cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old" );
48like ( $result->output, $warningOutput, "Output for warning correct" ); 48like ( $result->output, $warningOutput, "Output for warning correct" );
49 49
50$result = NPTest->testCmd( 50$result = NPTest->testCmd(
51 "./check_file_age -f $temp_file -w 0:1"
52 );
53cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old by range" );
54like ( $result->output, $warningOutput, "Output for warning by range correct" );
55
56$result = NPTest->testCmd(
51 "./check_file_age -f $temp_file -c 1" 57 "./check_file_age -f $temp_file -c 1"
52 ); 58 );
53cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old" ); 59cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old" );
54like ( $result->output, $criticalOutput, "Output for critical correct" ); 60like ( $result->output, $criticalOutput, "Output for critical correct" );
55 61
56$result = NPTest->testCmd( 62$result = NPTest->testCmd(
63 "./check_file_age -f $temp_file -c 0:1"
64 );
65cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old by range" );
66like ( $result->output, $criticalOutput, "Output for critical by range correct" );
67
68$result = NPTest->testCmd(
57 "./check_file_age -f $temp_file -c 1000 -W 100" 69 "./check_file_age -f $temp_file -c 1000 -W 100"
58 ); 70 );
59cmp_ok( $result->return_code, '==', 0, "Checking file size" ); 71cmp_ok( $result->return_code, '==', 0, "Checking file size" );
60 72
61$result = NPTest->testCmd( 73$result = NPTest->testCmd(
74 "./check_file_age -f $temp_file -c 0:1000 -W 0:100"
75 );
76cmp_ok( $result->return_code, '==', 0, "Checking file size by range" );
77
78$result = NPTest->testCmd(
62 "./check_file_age -f $temp_file -c 1000 -W 100" 79 "./check_file_age -f $temp_file -c 1000 -W 100"
63 ); 80 );
64like( $result->output, $performanceOutput, "Checking for performance Output" ); 81like( $result->output, $performanceOutput, "Checking for performance Output" );
65 82
66$result = NPTest->testCmd( 83$result = NPTest->testCmd(
84 "./check_file_age -f $temp_file -c 1000 -W 100"
85 );
86like( $result->output, $performanceOutput, "Checking for performance Output from range" );
87
88$result = NPTest->testCmd(
67 "./check_file_age -f /non/existent --ignore-missing" 89 "./check_file_age -f /non/existent --ignore-missing"
68 ); 90 );
69cmp_ok( $result->return_code, '==', 0, "Honours --ignore-missing" ); 91cmp_ok( $result->return_code, '==', 0, "Honours --ignore-missing" );
@@ -74,10 +96,30 @@ $result = NPTest->testCmd(
74cmp_ok( $result->return_code, '==', 1, "One byte too short" ); 96cmp_ok( $result->return_code, '==', 1, "One byte too short" );
75 97
76$result = NPTest->testCmd( 98$result = NPTest->testCmd(
99 "./check_file_age -f $temp_file -c 1000 -W 101:"
100 );
101cmp_ok( $result->return_code, '==', 1, "One byte too short by range" );
102
103$result = NPTest->testCmd(
77 "./check_file_age -f $temp_file -c 1000 -C 101" 104 "./check_file_age -f $temp_file -c 1000 -C 101"
78 ); 105 );
79cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" ); 106cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" );
80 107
108$result = NPTest->testCmd(
109 "./check_file_age -f $temp_file -c 1000 -C 101:"
110 );
111cmp_ok( $result->return_code, '==', 2, "One byte too short by range - critical" );
112
113$result = NPTest->testCmd(
114 "./check_file_age -f $temp_file -c 1000 -W 0:99"
115 );
116cmp_ok( $result->return_code, '==', 1, "One byte too long by range" );
117
118$result = NPTest->testCmd(
119 "./check_file_age -f $temp_file -c 1000 -C 0:99"
120 );
121cmp_ok( $result->return_code, '==', 2, "One byte too long by range - critical" );
122
81symlink $temp_file, $temp_link or die "Cannot create symlink"; 123symlink $temp_file, $temp_link or die "Cannot create symlink";
82$result = NPTest->testCmd("./check_file_age -f $temp_link -c 10"); 124$result = NPTest->testCmd("./check_file_age -f $temp_link -c 10");
83cmp_ok( $result->return_code, '==', 0, "Works for symlinks" ); 125cmp_ok( $result->return_code, '==', 0, "Works for symlinks" );