diff options
author | Bernd Arnold <wopfel@gmail.com> | 2018-06-13 16:56:22 +0200 |
---|---|---|
committer | Bernd Arnold <wopfel@gmail.com> | 2018-06-13 16:56:22 +0200 |
commit | db499b6f5b82aff65171ac4cfa5b936ba1e5e5ad (patch) | |
tree | 4148af2ebf8c8a7c67f65a9961bbf668127c97a6 /plugins-scripts | |
parent | a784b19d6fc193e4c98ec6b2248bf4e252a9a0d7 (diff) | |
download | monitoring-plugins-db499b6f5b82aff65171ac4cfa5b936ba1e5e5ad.tar.gz |
Introducing ranges for warning and critical
Works as before:
-w 1w -c 2w
New (as before, but also warn if uptime < 5m, and crit if uptime < 2m):
-w 5m:1w -c 2m:2w
(idea by @sni)
Also refactored the time calculation, if a suffix is present:
New sub calc_as_seconds($)
Diffstat (limited to 'plugins-scripts')
-rwxr-xr-x | plugins-scripts/check_uptime.pl | 135 | ||||
-rw-r--r-- | plugins-scripts/t/check_uptime.t | 2 |
2 files changed, 95 insertions, 42 deletions
diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 2b230f5c..f2b47be5 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl | |||
@@ -26,6 +26,8 @@ use strict; | |||
26 | use Getopt::Long; | 26 | use Getopt::Long; |
27 | use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c | 27 | use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c |
28 | $opt_f $opt_s | 28 | $opt_f $opt_s |
29 | $lower_warn_threshold $upper_warn_threshold | ||
30 | $lower_crit_threshold $upper_crit_threshold | ||
29 | $status $state $msg); | 31 | $status $state $msg); |
30 | use FindBin; | 32 | use FindBin; |
31 | use lib "$FindBin::Bin"; | 33 | use lib "$FindBin::Bin"; |
@@ -118,10 +120,19 @@ $pretty_uptime .= sprintf( "%d second%s", $secs, $secs == 1 ? "" : "s" ); | |||
118 | my $state_str = "UNKNOWN"; | 120 | my $state_str = "UNKNOWN"; |
119 | 121 | ||
120 | # Check values | 122 | # Check values |
121 | if ( $uptime_seconds > $opt_c ) { | 123 | my $out_of_bounds_text = ""; |
124 | if ( $uptime_seconds > $upper_crit_threshold ) { | ||
122 | $state_str = "CRITICAL"; | 125 | $state_str = "CRITICAL"; |
123 | } elsif ( $uptime_seconds > $opt_w ) { | 126 | $out_of_bounds_text = "Exceeds upper crit threshold"; |
127 | } elsif ( $uptime_seconds < $lower_crit_threshold ) { | ||
128 | $state_str = "CRITICAL"; | ||
129 | $out_of_bounds_text = "Exceeds lower crit threshold"; | ||
130 | } elsif ( $uptime_seconds > $upper_warn_threshold ) { | ||
131 | $state_str = "WARNING"; | ||
132 | $out_of_bounds_text = "Exceeds upper warn threshold"; | ||
133 | } elsif ( $uptime_seconds < $lower_warn_threshold ) { | ||
124 | $state_str = "WARNING"; | 134 | $state_str = "WARNING"; |
135 | $out_of_bounds_text = "Exceeds lower warn threshold"; | ||
125 | } else { | 136 | } else { |
126 | $state_str = "OK"; | 137 | $state_str = "OK"; |
127 | } | 138 | } |
@@ -129,6 +140,7 @@ if ( $uptime_seconds > $opt_c ) { | |||
129 | $msg = "$state_str: "; | 140 | $msg = "$state_str: "; |
130 | 141 | ||
131 | $msg .= "uptime is $uptime_seconds seconds. "; | 142 | $msg .= "uptime is $uptime_seconds seconds. "; |
143 | $msg .= "$out_of_bounds_text. " if $out_of_bounds_text; | ||
132 | $msg .= "Running for $pretty_uptime. " if $opt_f; | 144 | $msg .= "Running for $pretty_uptime. " if $opt_f; |
133 | if ( $opt_s ) { | 145 | if ( $opt_s ) { |
134 | chomp( my $up_since = `uptime -s` ); | 146 | chomp( my $up_since = `uptime -s` ); |
@@ -138,7 +150,7 @@ if ( $opt_s ) { | |||
138 | $state = $ERRORS{$state_str}; | 150 | $state = $ERRORS{$state_str}; |
139 | 151 | ||
140 | # Perfdata support | 152 | # Perfdata support |
141 | print "$msg|uptime=${uptime_seconds}s;$opt_w;$opt_c;0\n"; | 153 | print "$msg|uptime=${uptime_seconds}s;$upper_warn_threshold;$upper_crit_threshold;0\n"; |
142 | exit $state; | 154 | exit $state; |
143 | 155 | ||
144 | 156 | ||
@@ -176,51 +188,56 @@ sub process_arguments(){ | |||
176 | exit $ERRORS{'UNKNOWN'}; | 188 | exit $ERRORS{'UNKNOWN'}; |
177 | } | 189 | } |
178 | 190 | ||
179 | # Check if suffix is present | 191 | # Check if a range was supplied ("lowvalue:highvalue") for warning and critical |
180 | # Calculate parameter to seconds (to get an integer value finally) | 192 | # Otherwise, set 0 as the lower threshold and the parameter value as upper threshold |
181 | # s = seconds | 193 | # (the uptime should always be positive, so there should be no issue) |
182 | # m = minutes | 194 | if ( $opt_w =~ /^(.+):(.+)$/ ) { |
183 | # h = hours | 195 | $lower_warn_threshold = $1; |
184 | # d = days | 196 | $upper_warn_threshold = $2; |
185 | # w = weeks | 197 | } else { |
186 | my %factor = ( "s" => 1, | 198 | $lower_warn_threshold = 0; |
187 | "m" => 60, | 199 | $upper_warn_threshold = $opt_w; |
188 | "h" => 60 * 60, | 200 | } |
189 | "d" => 60 * 60 * 24, | 201 | if ( $opt_c =~ /^(.+):(.+)$/ ) { |
190 | "w" => 60 * 60 * 24 * 7, | 202 | $lower_crit_threshold = $1; |
191 | ); | 203 | $upper_crit_threshold = $2; |
192 | if ( $opt_w =~ /^(\d+)([a-z])$/ ) { | 204 | } else { |
193 | my $value = $1; | 205 | $lower_crit_threshold = 0; |
194 | my $suffix = $2; | 206 | $upper_crit_threshold = $opt_c; |
195 | print "warning: value=$value, suffix=$suffix\n" if $verbose; | 207 | } |
196 | if ( ! defined $factor{$suffix} ) { | 208 | |
197 | print "Error: wrong suffix ($suffix) for warning"; | 209 | # Set as seconds (calculate if suffix present) |
198 | exit $ERRORS{'UNKNOWN'}; | 210 | $lower_warn_threshold = calc_as_seconds( $lower_warn_threshold ); |
199 | } | 211 | $lower_crit_threshold = calc_as_seconds( $lower_crit_threshold ); |
200 | $opt_w = $value * $factor{$suffix}; | 212 | $upper_warn_threshold = calc_as_seconds( $upper_warn_threshold ); |
213 | $upper_crit_threshold = calc_as_seconds( $upper_crit_threshold ); | ||
214 | |||
215 | # Check for numeric value of warning parameter | ||
216 | if ( $lower_warn_threshold !~ /^\d+$/ ) { | ||
217 | print "Lower warning (-w) is not numeric\n"; | ||
218 | exit $ERRORS{'UNKNOWN'}; | ||
201 | } | 219 | } |
202 | if ( $opt_c =~ /^(\d+)([a-z])$/ ) { | 220 | if ( $upper_warn_threshold !~ /^\d+$/ ) { |
203 | my $value = $1; | 221 | print "Upper warning (-w) is not numeric\n"; |
204 | my $suffix = $2; | 222 | exit $ERRORS{'UNKNOWN'}; |
205 | print "critical: value=$value, suffix=$suffix\n" if $verbose; | ||
206 | if ( ! defined $factor{$suffix} ) { | ||
207 | print "Error: wrong suffix ($suffix) for critical"; | ||
208 | exit $ERRORS{'UNKNOWN'}; | ||
209 | } | ||
210 | $opt_c = $value * $factor{$suffix}; | ||
211 | } | 223 | } |
212 | 224 | # Check for numeric value of critical parameter | |
213 | if ( $opt_w !~ /^\d+$/ ) { | 225 | if ( $lower_crit_threshold !~ /^\d+$/ ) { |
214 | print "Warning (-w) is not numeric\n"; | 226 | print "Lower critical (-c) is not numeric\n"; |
215 | exit $ERRORS{'UNKNOWN'}; | 227 | exit $ERRORS{'UNKNOWN'}; |
216 | } | 228 | } |
217 | if ( $opt_c !~ /^\d+$/ ) { | 229 | if ( $upper_crit_threshold !~ /^\d+$/ ) { |
218 | print "Critical (-c) is not numeric\n"; | 230 | print "Upper critical (-c) is not numeric\n"; |
219 | exit $ERRORS{'UNKNOWN'}; | 231 | exit $ERRORS{'UNKNOWN'}; |
220 | } | 232 | } |
221 | 233 | ||
222 | if ( $opt_w >= $opt_c) { | 234 | # Check boundaries |
223 | print "Warning (-w) cannot be greater than Critical (-c)!\n"; | 235 | if ( $upper_warn_threshold >= $upper_crit_threshold ) { |
236 | print "Upper Warning (-w) cannot be greater than Critical (-c)!\n"; | ||
237 | exit $ERRORS{'UNKNOWN'}; | ||
238 | } | ||
239 | if ( $lower_warn_threshold < $lower_crit_threshold ) { | ||
240 | print "Lower Warning (-w) cannot be greater than Critical (-c)!\n"; | ||
224 | exit $ERRORS{'UNKNOWN'}; | 241 | exit $ERRORS{'UNKNOWN'}; |
225 | } | 242 | } |
226 | 243 | ||
@@ -254,3 +271,39 @@ sub print_help () { | |||
254 | print "\n\n"; | 271 | print "\n\n"; |
255 | support(); | 272 | support(); |
256 | } | 273 | } |
274 | |||
275 | sub calc_as_seconds () { | ||
276 | |||
277 | my $parameter = shift; | ||
278 | |||
279 | # Check if suffix is present | ||
280 | # Calculate parameter to seconds (to get an integer value finally) | ||
281 | # If no suffix is present, just return the value | ||
282 | |||
283 | # Possible suffixes: | ||
284 | # s = seconds | ||
285 | # m = minutes | ||
286 | # h = hours | ||
287 | # d = days | ||
288 | # w = weeks | ||
289 | my %factor = ( "s" => 1, | ||
290 | "m" => 60, | ||
291 | "h" => 60 * 60, | ||
292 | "d" => 60 * 60 * 24, | ||
293 | "w" => 60 * 60 * 24 * 7, | ||
294 | ); | ||
295 | |||
296 | if ( $parameter =~ /^(\d+)([a-z])$/ ) { | ||
297 | my $value = $1; | ||
298 | my $suffix = $2; | ||
299 | print "detected: value=$value, suffix=$suffix\n" if $verbose; | ||
300 | if ( ! defined $factor{$suffix} ) { | ||
301 | print "Error: wrong suffix ($suffix) for value '$parameter'"; | ||
302 | exit $ERRORS{'UNKNOWN'}; | ||
303 | } | ||
304 | $parameter = $value * $factor{$suffix}; | ||
305 | } | ||
306 | |||
307 | return $parameter; | ||
308 | |||
309 | } | ||
diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t index 410a0800..4606718a 100644 --- a/plugins-scripts/t/check_uptime.t +++ b/plugins-scripts/t/check_uptime.t | |||
@@ -26,7 +26,7 @@ $result = NPTest->testCmd( | |||
26 | "./check_uptime -w 5 -c 2" | 26 | "./check_uptime -w 5 -c 2" |
27 | ); | 27 | ); |
28 | cmp_ok( $result->return_code, '==', 3, "Warning greater than critical" ); | 28 | cmp_ok( $result->return_code, '==', 3, "Warning greater than critical" ); |
29 | like ( $result->output, '/^Warning .*cannot be greater than Critical/', "Output for warning greater than critical correct" ); | 29 | like ( $result->output, '/^Upper Warning .*cannot be greater than Critical/', "Output for warning greater than critical correct" ); |
30 | 30 | ||
31 | $result = NPTest->testCmd( | 31 | $result = NPTest->testCmd( |
32 | "./check_uptime -c 1000 -W 100 2>&1" | 32 | "./check_uptime -c 1000 -W 100 2>&1" |