From c99a166a43fb9da42ba68073224921124a435aab Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Sat, 12 Feb 2022 14:41:54 +0100 Subject: 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. --- plugins-scripts/check_uptime.pl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'plugins-scripts/check_uptime.pl') 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; use strict; use Getopt::Long; use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c - $opt_f $opt_s + $opt_f $opt_s $opt_d $lower_warn_threshold $upper_warn_threshold $lower_crit_threshold $upper_crit_threshold $status $state $msg); @@ -137,9 +137,20 @@ if ( $uptime_seconds > $upper_crit_threshold ) { $state_str = "OK"; } +# Prepare uptime value (seconds or days) +my $uptime_text = ""; +my $uptime_unit = ""; +if ( $opt_d ) { + $uptime_text = floor($uptime_seconds / 60 / 60 / 24); + $uptime_unit = "days"; +} else { + $uptime_text = $uptime_seconds; + $uptime_unit = "seconds"; +} + $msg = "$state_str: "; -$msg .= "uptime is $uptime_seconds seconds. "; +$msg .= "uptime is $uptime_text $uptime_unit. "; $msg .= "Exceeds $out_of_bounds_text threshold. " if $out_of_bounds_text; $msg .= "Running for $pretty_uptime. " if $opt_f; if ( $opt_s ) { @@ -167,6 +178,7 @@ sub process_arguments(){ "c=s" => \$opt_c, "critical=s" => \$opt_c, # critical if above this number "f" => \$opt_f, "for" => \$opt_f, # show "running for ..." "s" => \$opt_s, "since" => \$opt_s, # show "running since ..." + "d" => \$opt_d, "days" => \$opt_d, # report uptime in days ); if ($opt_V) { @@ -262,6 +274,7 @@ sub print_help () { print "-c (--critical) = Min. number of uptime to generate critical alert ( w < c )\n"; print "-f (--for) = Show uptime in a pretty format (Running for x weeks, x days, ...)\n"; print "-s (--since) = Show last boot in yyyy-mm-dd HH:MM:SS format (output from 'uptime -s')\n"; + print "-d (--days) = Show uptime in days\n"; print "-h (--help)\n"; print "-V (--version)\n"; print "-v (--verbose) = debugging output\n"; -- cgit v1.2.3-74-g34f1 From 6c8b45a1691f4ce98f1c559a1e9cd1fef68c0fe2 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Tue, 15 Feb 2022 01:39:21 +0100 Subject: check_uptime: Fix lowercase typo in plugin output --- plugins-scripts/check_uptime.pl | 2 +- plugins-scripts/t/check_uptime.t | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'plugins-scripts/check_uptime.pl') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 04324b2e..f9542872 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -150,7 +150,7 @@ if ( $opt_d ) { $msg = "$state_str: "; -$msg .= "uptime is $uptime_text $uptime_unit. "; +$msg .= "Uptime is $uptime_text $uptime_unit. "; $msg .= "Exceeds $out_of_bounds_text threshold. " if $out_of_bounds_text; $msg .= "Running for $pretty_uptime. " if $opt_f; if ( $opt_s ) { diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t index b31d0c6c..6e81db3c 100644 --- a/plugins-scripts/t/check_uptime.t +++ b/plugins-scripts/t/check_uptime.t @@ -49,32 +49,32 @@ $result = NPTest->testCmd( "./check_uptime -d -w 1 -c 2" ); cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); -like ( $result->output, '/CRITICAL: uptime is \d+ days/', "Output for the d parameter correct" ); +like ( $result->output, '/CRITICAL: Uptime is \d+ days/', "Output for the d parameter correct" ); $result = NPTest->testCmd( "./check_uptime -w 1 -c 2" ); cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); -like ( $result->output, '/^CRITICAL: uptime is \d+ seconds/', "Output for uptime higher than 2 seconds correct" ); +like ( $result->output, '/^CRITICAL: Uptime is \d+ seconds/', "Output for uptime higher than 2 seconds correct" ); $result = NPTest->testCmd( "./check_uptime -w 1 -c 9999w" ); cmp_ok( $result->return_code, '==', 1, "Uptime lower than 9999 weeks" ); -like ( $result->output, '/^WARNING: uptime is \d+ seconds/', "Output for uptime lower than 9999 weeks correct" ); +like ( $result->output, '/^WARNING: Uptime is \d+ seconds/', "Output for uptime lower than 9999 weeks correct" ); $result = NPTest->testCmd( "./check_uptime -w 9998w -c 9999w" ); cmp_ok( $result->return_code, '==', 0, "Uptime lower than 9998 weeks" ); -like ( $result->output, '/^OK: uptime is \d+ seconds/', "Output for uptime lower than 9998 weeks correct" ); +like ( $result->output, '/^OK: Uptime is \d+ seconds/', "Output for uptime lower than 9998 weeks correct" ); like ( $result->output, '/\|uptime=[0-9]+s;6046790400;6047395200;/', "Checking for performance output" ); $result = NPTest->testCmd( "./check_uptime -w 111222d -c 222333d" ); cmp_ok( $result->return_code, '==', 0, "Uptime lower than 111222 days" ); -like ( $result->output, '/^OK: uptime is \d+ seconds/', "Output for uptime lower than 111222 days correct" ); +like ( $result->output, '/^OK: Uptime is \d+ seconds/', "Output for uptime lower than 111222 days correct" ); like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); # Same as before, hopefully uptime is higher than 2 seconds so no warning @@ -82,7 +82,7 @@ $result = NPTest->testCmd( "./check_uptime -w 2:111222d -c 1:222333d" ); cmp_ok( $result->return_code, '==', 0, "Uptime lower than 111222 days, and higher 2 seconds" ); -like ( $result->output, '/^OK: uptime is \d+ seconds/', "Output for uptime lower than 111222 days, and higher 2 seconds correct" ); +like ( $result->output, '/^OK: Uptime is \d+ seconds/', "Output for uptime lower than 111222 days, and higher 2 seconds correct" ); like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); # Same as before, now the low warning should trigger @@ -90,7 +90,7 @@ $result = NPTest->testCmd( "./check_uptime -w 111221d:111222d -c 1:222333d" ); cmp_ok( $result->return_code, '==', 1, "Uptime lower than 111221 days raises warning" ); -like ( $result->output, '/^WARNING: uptime is \d+ seconds/', "Output for uptime lower than 111221 days correct" ); +like ( $result->output, '/^WARNING: Uptime is \d+ seconds/', "Output for uptime lower than 111221 days correct" ); like ( $result->output, '/Exceeds lower warn threshold/', "Exceeds text correct" ); like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); @@ -99,7 +99,7 @@ $result = NPTest->testCmd( "./check_uptime -w 111221d:111222d -c 111220d:222333d" ); cmp_ok( $result->return_code, '==', 2, "Uptime lower than 111220 days raises critical" ); -like ( $result->output, '/^CRITICAL: uptime is \d+ seconds/', "Output for uptime lower than 111220 days correct" ); +like ( $result->output, '/^CRITICAL: Uptime is \d+ seconds/', "Output for uptime lower than 111220 days correct" ); like ( $result->output, '/Exceeds lower crit threshold/', "Exceeds text correct" ); like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); -- cgit v1.2.3-74-g34f1