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/t/check_uptime.t | 8 +++++++- 1 file changed, 7 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 c395307c..b31d0c6c 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 => 40; +use Test::More tests => 42; use NPTest; my $result; @@ -45,6 +45,12 @@ $result = NPTest->testCmd( 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 -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" ); + $result = NPTest->testCmd( "./check_uptime -w 1 -c 2" ); -- 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/t') 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 From c410ad38798fde8cc278a3f1522a9571dbdb7fae Mon Sep 17 00:00:00 2001 From: lorenzg Date: Fri, 20 Jan 2023 08:52:38 +0100 Subject: add tests for check_log --- plugins-scripts/t/check_log.t | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 plugins-scripts/t/check_log.t (limited to 'plugins-scripts/t') diff --git a/plugins-scripts/t/check_log.t b/plugins-scripts/t/check_log.t new file mode 100644 index 00000000..b66e0fd8 --- /dev/null +++ b/plugins-scripts/t/check_log.t @@ -0,0 +1,82 @@ +#!/usr/bin/perl -w -I .. +# +# check_log tests +# +# + +use strict; +use Test::More; +use NPTest; + +my $tests = 18; +plan tests => $tests; + +my $firstTimeOutput ='/^Log check data initialized/'; +my $okOutput = '/^Log check ok - 0 pattern matches found/'; +my $criticalOutput = '/^\(\d+\) < /'; +my $multilineOutput = '/\(3\) <.*\n.*\n.*$/'; +my $unknownOutput = '/^Usage: /'; +my $unknownArgOutput = '/^Unknown argument: /'; +my $bothRegexOutput = '/^Can not use extended and perl regex/'; + +my $result; +my $temp_file = "/tmp/check_log.tmp"; +my $oldlog = "/tmp/oldlog.tmp"; + +open(FH, '>', $temp_file) or die $!; +close(FH); + +$result = NPTest->testCmd("./check_log"); +cmp_ok( $result->return_code, '==', 3, "Missing parameters" ); +like ( $result->output, $unknownOutput, "Output for unknown correct" ); + +$result = NPTest->testCmd("./check_log -f"); +cmp_ok( $result->return_code, '==', 3, "Wrong parameters" ); +like ( $result->output, $unknownArgOutput, "Output for unknown correct" ); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Simple match' -e -p"); +cmp_ok( $result->return_code, '==', 3, "Both regex parameters" ); +like ( $result->output, $bothRegexOutput, "Output for unknown correct" ); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Simple match'"); +cmp_ok( $result->return_code, '==', 0, "First time executing" ); +like ( $result->output, $firstTimeOutput, "Output for first time executing correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "This is some text, that should not match\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'No match'"); +cmp_ok( $result->return_code, '==', 0, "No match" ); +like ( $result->output, $okOutput, "Output for no match correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "This text should match\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'should match'"); +cmp_ok( $result->return_code, '==', 2, "Pattern match" ); +like ( $result->output, $criticalOutput, "Output for match correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "This text should not match, because it is excluded\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'match' --exclude 'because'"); +cmp_ok( $result->return_code, '==', 0, "Exclude a pattern" ); +like ( $result->output, $okOutput, "Output for no match correct" ); + +open(FH, '>>', $temp_file) or die $!; +print FH "Trying\nwith\nmultiline\nignore me\n"; +close(FH); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'Trying\\|with\\|multiline\\|ignore' --exclude 'me' --all"); +cmp_ok( $result->return_code, '==', 2, "Multiline pattern match with --all" ); +like ( $result->output, $multilineOutput, "Output for multiline match correct" ); + +$result = NPTest->testCmd("./check_log -F ".$temp_file." -O ".$oldlog." -q 'match' -a"); +cmp_ok( $result->return_code, '==', 0, "Non matching --all" ); +like ( $result->output, $okOutput, "Output for no match correct" ); + +unlink($oldlog); +unlink($temp_file); -- cgit v1.2.3-74-g34f1