diff options
author | Andreas Motl <andreas.motl@elmyra.de> | 2022-02-12 14:41:54 +0100 |
---|---|---|
committer | Sven Nierlein <sven@nierlein.org> | 2022-02-15 16:09:47 +0100 |
commit | c99a166a43fb9da42ba68073224921124a435aab (patch) | |
tree | 559d6b1a6dd822004d5f1e2becf822ed7838edea | |
parent | 5943528121033579033c5a372df6c5e91b22e723 (diff) | |
download | monitoring-plugins-c99a166a43fb9da42ba68073224921124a435aab.tar.gz |
check_uptime: Add option to report uptime in days instead of seconds
Currently, the plugin output is:
CRITICAL: Uptime is 38829029 seconds.
When using the proposed `--days|-d` option, it will be:
CRITICAL: Uptime is 449 days.
-rwxr-xr-x | plugins-scripts/check_uptime.pl | 17 | ||||
-rw-r--r-- | plugins-scripts/t/check_uptime.t | 8 |
2 files changed, 22 insertions, 3 deletions
diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 4c9f22da..04324b2e 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl | |||
@@ -25,7 +25,7 @@ use POSIX; | |||
25 | use strict; | 25 | 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 $opt_d |
29 | $lower_warn_threshold $upper_warn_threshold | 29 | $lower_warn_threshold $upper_warn_threshold |
30 | $lower_crit_threshold $upper_crit_threshold | 30 | $lower_crit_threshold $upper_crit_threshold |
31 | $status $state $msg); | 31 | $status $state $msg); |
@@ -137,9 +137,20 @@ if ( $uptime_seconds > $upper_crit_threshold ) { | |||
137 | $state_str = "OK"; | 137 | $state_str = "OK"; |
138 | } | 138 | } |
139 | 139 | ||
140 | # Prepare uptime value (seconds or days) | ||
141 | my $uptime_text = ""; | ||
142 | my $uptime_unit = ""; | ||
143 | if ( $opt_d ) { | ||
144 | $uptime_text = floor($uptime_seconds / 60 / 60 / 24); | ||
145 | $uptime_unit = "days"; | ||
146 | } else { | ||
147 | $uptime_text = $uptime_seconds; | ||
148 | $uptime_unit = "seconds"; | ||
149 | } | ||
150 | |||
140 | $msg = "$state_str: "; | 151 | $msg = "$state_str: "; |
141 | 152 | ||
142 | $msg .= "uptime is $uptime_seconds seconds. "; | 153 | $msg .= "uptime is $uptime_text $uptime_unit. "; |
143 | $msg .= "Exceeds $out_of_bounds_text threshold. " if $out_of_bounds_text; | 154 | $msg .= "Exceeds $out_of_bounds_text threshold. " if $out_of_bounds_text; |
144 | $msg .= "Running for $pretty_uptime. " if $opt_f; | 155 | $msg .= "Running for $pretty_uptime. " if $opt_f; |
145 | if ( $opt_s ) { | 156 | if ( $opt_s ) { |
@@ -167,6 +178,7 @@ sub process_arguments(){ | |||
167 | "c=s" => \$opt_c, "critical=s" => \$opt_c, # critical if above this number | 178 | "c=s" => \$opt_c, "critical=s" => \$opt_c, # critical if above this number |
168 | "f" => \$opt_f, "for" => \$opt_f, # show "running for ..." | 179 | "f" => \$opt_f, "for" => \$opt_f, # show "running for ..." |
169 | "s" => \$opt_s, "since" => \$opt_s, # show "running since ..." | 180 | "s" => \$opt_s, "since" => \$opt_s, # show "running since ..." |
181 | "d" => \$opt_d, "days" => \$opt_d, # report uptime in days | ||
170 | ); | 182 | ); |
171 | 183 | ||
172 | if ($opt_V) { | 184 | if ($opt_V) { |
@@ -262,6 +274,7 @@ sub print_help () { | |||
262 | print "-c (--critical) = Min. number of uptime to generate critical alert ( w < c )\n"; | 274 | print "-c (--critical) = Min. number of uptime to generate critical alert ( w < c )\n"; |
263 | print "-f (--for) = Show uptime in a pretty format (Running for x weeks, x days, ...)\n"; | 275 | print "-f (--for) = Show uptime in a pretty format (Running for x weeks, x days, ...)\n"; |
264 | print "-s (--since) = Show last boot in yyyy-mm-dd HH:MM:SS format (output from 'uptime -s')\n"; | 276 | print "-s (--since) = Show last boot in yyyy-mm-dd HH:MM:SS format (output from 'uptime -s')\n"; |
277 | print "-d (--days) = Show uptime in days\n"; | ||
265 | print "-h (--help)\n"; | 278 | print "-h (--help)\n"; |
266 | print "-V (--version)\n"; | 279 | print "-V (--version)\n"; |
267 | print "-v (--verbose) = debugging output\n"; | 280 | print "-v (--verbose) = debugging output\n"; |
diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t index c395307c..b31d0c6c 100644 --- a/plugins-scripts/t/check_uptime.t +++ b/plugins-scripts/t/check_uptime.t | |||
@@ -5,7 +5,7 @@ | |||
5 | # | 5 | # |
6 | 6 | ||
7 | use strict; | 7 | use strict; |
8 | use Test::More tests => 40; | 8 | use Test::More tests => 42; |
9 | use NPTest; | 9 | use NPTest; |
10 | 10 | ||
11 | my $result; | 11 | my $result; |
@@ -46,6 +46,12 @@ cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); | |||
46 | like ( $result->output, '/Running since \d+/', "Output for the s parameter correct" ); | 46 | like ( $result->output, '/Running since \d+/', "Output for the s parameter correct" ); |
47 | 47 | ||
48 | $result = NPTest->testCmd( | 48 | $result = NPTest->testCmd( |
49 | "./check_uptime -d -w 1 -c 2" | ||
50 | ); | ||
51 | cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); | ||
52 | like ( $result->output, '/CRITICAL: uptime is \d+ days/', "Output for the d parameter correct" ); | ||
53 | |||
54 | $result = NPTest->testCmd( | ||
49 | "./check_uptime -w 1 -c 2" | 55 | "./check_uptime -w 1 -c 2" |
50 | ); | 56 | ); |
51 | cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); | 57 | cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); |