From 250adb31ef1849adea8b3bd7f66e92df1bbdd431 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Sun, 3 Jun 2018 16:51:43 +0200 Subject: Added test file --- plugins-scripts/t/check_uptime.t | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 plugins-scripts/t/check_uptime.t (limited to 'plugins-scripts/t') diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t new file mode 100644 index 00000000..410a0800 --- /dev/null +++ b/plugins-scripts/t/check_uptime.t @@ -0,0 +1,73 @@ +#!/usr/bin/perl -w -I .. +# +# check_uptime tests +# +# + +use strict; +use Test::More tests => 21; +use NPTest; + +my $result; + +$result = NPTest->testCmd( + "./check_uptime" + ); +cmp_ok( $result->return_code, '==', 3, "Missing parameters" ); +like ( $result->output, '/^Usage: check_uptime -w/', "Output for missing parameters correct" ); + +$result = NPTest->testCmd( + "./check_uptime --help" + ); +cmp_ok( $result->return_code, '==', 3, "Help output requested" ); +like ( $result->output, '/ABSOLUTELY NO WARRANTY/', "Output for help correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 5 -c 2" + ); +cmp_ok( $result->return_code, '==', 3, "Warning greater than critical" ); +like ( $result->output, '/^Warning .*cannot be greater than Critical/', "Output for warning greater than critical correct" ); + +$result = NPTest->testCmd( + "./check_uptime -c 1000 -W 100 2>&1" + ); +like ( $result->output, '/^Unknown option: W/', "Output with wrong parameter is correct" ); + +$result = NPTest->testCmd( + "./check_uptime -f -w 1 -c 2" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); +like ( $result->output, '/Running for \d+/', "Output for the f parameter correct" ); + +$result = NPTest->testCmd( + "./check_uptime -s -w 1 -c 2" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); +like ( $result->output, '/Running since \d+/', "Output for the s 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" ); + +$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" ); + +$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, '/\|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, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + -- cgit v1.2.3-74-g34f1 From db499b6f5b82aff65171ac4cfa5b936ba1e5e5ad Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Wed, 13 Jun 2018 16:56:22 +0200 Subject: 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($) --- plugins-scripts/check_uptime.pl | 135 +++++++++++++++++++++++++++------------ plugins-scripts/t/check_uptime.t | 2 +- 2 files changed, 95 insertions(+), 42 deletions(-) (limited to 'plugins-scripts/t') 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; use Getopt::Long; use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_f $opt_s + $lower_warn_threshold $upper_warn_threshold + $lower_crit_threshold $upper_crit_threshold $status $state $msg); use FindBin; use lib "$FindBin::Bin"; @@ -118,10 +120,19 @@ $pretty_uptime .= sprintf( "%d second%s", $secs, $secs == 1 ? "" : "s" ); my $state_str = "UNKNOWN"; # Check values -if ( $uptime_seconds > $opt_c ) { +my $out_of_bounds_text = ""; +if ( $uptime_seconds > $upper_crit_threshold ) { $state_str = "CRITICAL"; -} elsif ( $uptime_seconds > $opt_w ) { + $out_of_bounds_text = "Exceeds upper crit threshold"; +} elsif ( $uptime_seconds < $lower_crit_threshold ) { + $state_str = "CRITICAL"; + $out_of_bounds_text = "Exceeds lower crit threshold"; +} elsif ( $uptime_seconds > $upper_warn_threshold ) { + $state_str = "WARNING"; + $out_of_bounds_text = "Exceeds upper warn threshold"; +} elsif ( $uptime_seconds < $lower_warn_threshold ) { $state_str = "WARNING"; + $out_of_bounds_text = "Exceeds lower warn threshold"; } else { $state_str = "OK"; } @@ -129,6 +140,7 @@ if ( $uptime_seconds > $opt_c ) { $msg = "$state_str: "; $msg .= "uptime is $uptime_seconds seconds. "; +$msg .= "$out_of_bounds_text. " if $out_of_bounds_text; $msg .= "Running for $pretty_uptime. " if $opt_f; if ( $opt_s ) { chomp( my $up_since = `uptime -s` ); @@ -138,7 +150,7 @@ if ( $opt_s ) { $state = $ERRORS{$state_str}; # Perfdata support -print "$msg|uptime=${uptime_seconds}s;$opt_w;$opt_c;0\n"; +print "$msg|uptime=${uptime_seconds}s;$upper_warn_threshold;$upper_crit_threshold;0\n"; exit $state; @@ -176,51 +188,56 @@ sub process_arguments(){ exit $ERRORS{'UNKNOWN'}; } - # Check if suffix is present - # Calculate parameter to seconds (to get an integer value finally) - # s = seconds - # m = minutes - # h = hours - # d = days - # w = weeks - my %factor = ( "s" => 1, - "m" => 60, - "h" => 60 * 60, - "d" => 60 * 60 * 24, - "w" => 60 * 60 * 24 * 7, - ); - if ( $opt_w =~ /^(\d+)([a-z])$/ ) { - my $value = $1; - my $suffix = $2; - print "warning: value=$value, suffix=$suffix\n" if $verbose; - if ( ! defined $factor{$suffix} ) { - print "Error: wrong suffix ($suffix) for warning"; - exit $ERRORS{'UNKNOWN'}; - } - $opt_w = $value * $factor{$suffix}; + # Check if a range was supplied ("lowvalue:highvalue") for warning and critical + # Otherwise, set 0 as the lower threshold and the parameter value as upper threshold + # (the uptime should always be positive, so there should be no issue) + if ( $opt_w =~ /^(.+):(.+)$/ ) { + $lower_warn_threshold = $1; + $upper_warn_threshold = $2; + } else { + $lower_warn_threshold = 0; + $upper_warn_threshold = $opt_w; + } + if ( $opt_c =~ /^(.+):(.+)$/ ) { + $lower_crit_threshold = $1; + $upper_crit_threshold = $2; + } else { + $lower_crit_threshold = 0; + $upper_crit_threshold = $opt_c; + } + + # Set as seconds (calculate if suffix present) + $lower_warn_threshold = calc_as_seconds( $lower_warn_threshold ); + $lower_crit_threshold = calc_as_seconds( $lower_crit_threshold ); + $upper_warn_threshold = calc_as_seconds( $upper_warn_threshold ); + $upper_crit_threshold = calc_as_seconds( $upper_crit_threshold ); + + # Check for numeric value of warning parameter + if ( $lower_warn_threshold !~ /^\d+$/ ) { + print "Lower warning (-w) is not numeric\n"; + exit $ERRORS{'UNKNOWN'}; } - if ( $opt_c =~ /^(\d+)([a-z])$/ ) { - my $value = $1; - my $suffix = $2; - print "critical: value=$value, suffix=$suffix\n" if $verbose; - if ( ! defined $factor{$suffix} ) { - print "Error: wrong suffix ($suffix) for critical"; - exit $ERRORS{'UNKNOWN'}; - } - $opt_c = $value * $factor{$suffix}; + if ( $upper_warn_threshold !~ /^\d+$/ ) { + print "Upper warning (-w) is not numeric\n"; + exit $ERRORS{'UNKNOWN'}; } - - if ( $opt_w !~ /^\d+$/ ) { - print "Warning (-w) is not numeric\n"; + # Check for numeric value of critical parameter + if ( $lower_crit_threshold !~ /^\d+$/ ) { + print "Lower critical (-c) is not numeric\n"; exit $ERRORS{'UNKNOWN'}; } - if ( $opt_c !~ /^\d+$/ ) { - print "Critical (-c) is not numeric\n"; + if ( $upper_crit_threshold !~ /^\d+$/ ) { + print "Upper critical (-c) is not numeric\n"; exit $ERRORS{'UNKNOWN'}; } - if ( $opt_w >= $opt_c) { - print "Warning (-w) cannot be greater than Critical (-c)!\n"; + # Check boundaries + if ( $upper_warn_threshold >= $upper_crit_threshold ) { + print "Upper Warning (-w) cannot be greater than Critical (-c)!\n"; + exit $ERRORS{'UNKNOWN'}; + } + if ( $lower_warn_threshold < $lower_crit_threshold ) { + print "Lower Warning (-w) cannot be greater than Critical (-c)!\n"; exit $ERRORS{'UNKNOWN'}; } @@ -254,3 +271,39 @@ sub print_help () { print "\n\n"; support(); } + +sub calc_as_seconds () { + + my $parameter = shift; + + # Check if suffix is present + # Calculate parameter to seconds (to get an integer value finally) + # If no suffix is present, just return the value + + # Possible suffixes: + # s = seconds + # m = minutes + # h = hours + # d = days + # w = weeks + my %factor = ( "s" => 1, + "m" => 60, + "h" => 60 * 60, + "d" => 60 * 60 * 24, + "w" => 60 * 60 * 24 * 7, + ); + + if ( $parameter =~ /^(\d+)([a-z])$/ ) { + my $value = $1; + my $suffix = $2; + print "detected: value=$value, suffix=$suffix\n" if $verbose; + if ( ! defined $factor{$suffix} ) { + print "Error: wrong suffix ($suffix) for value '$parameter'"; + exit $ERRORS{'UNKNOWN'}; + } + $parameter = $value * $factor{$suffix}; + } + + return $parameter; + +} 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( "./check_uptime -w 5 -c 2" ); cmp_ok( $result->return_code, '==', 3, "Warning greater than critical" ); -like ( $result->output, '/^Warning .*cannot be greater than Critical/', "Output for warning greater than critical correct" ); +like ( $result->output, '/^Upper Warning .*cannot be greater than Critical/', "Output for warning greater than critical correct" ); $result = NPTest->testCmd( "./check_uptime -c 1000 -W 100 2>&1" -- cgit v1.2.3-74-g34f1 From 558090a7d884a265aa909d894672154892a0baae Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Wed, 13 Jun 2018 17:13:20 +0200 Subject: Added tests for range values --- plugins-scripts/t/check_uptime.t | 58 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'plugins-scripts/t') diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t index 4606718a..c395307c 100644 --- a/plugins-scripts/t/check_uptime.t +++ b/plugins-scripts/t/check_uptime.t @@ -5,7 +5,7 @@ # use strict; -use Test::More tests => 21; +use Test::More tests => 40; use NPTest; my $result; @@ -71,3 +71,59 @@ 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, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); +# Same as before, hopefully uptime is higher than 2 seconds so no warning +$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, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + +# Same as before, now the low warning should trigger +$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, '/Exceeds lower warn threshold/', "Exceeds text correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + +# Same as before, now the low critical should trigger +$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, '/Exceeds lower crit threshold/', "Exceeds text correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + + +# +# Range values using ":" without two parts ("a:b") is invalid +# Strings without two parts are always considered as upper threshold +# + +$result = NPTest->testCmd( + "./check_uptime -w 2: -c 1:4" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper warning .* is not numeric/', "Output for wrong parameter format correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 2:3 -c 1:" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper critical .* is not numeric/', "Output for wrong parameter format correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w :3 -c 1:4" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper warning .* is not numeric/', "Output for wrong parameter format correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 2:3 -c :4" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper critical .* is not numeric/', "Output for wrong parameter format correct" ); + -- cgit v1.2.3-74-g34f1