From b52e63da65ac5db7affbd09e110904855f3b1ed7 Mon Sep 17 00:00:00 2001 From: Thomas Guyot-Sionnest Date: Wed, 21 May 2008 08:57:13 +0000 Subject: [PATCH] Fixed passive option in check_by_ssh Also: - On non-skipped stderr, check_by_ssh now returns UNKNOWN or worse (result from command) instead of always UNKNOWN. - Fixed passive tests and make is always run the specified number of tests (using fail if there's nothing to test). git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1997 f882894a-f735-0410-b71e-b25c423dba1c --- NEWS | 2 ++ plugins/check_by_ssh.c | 21 ++++++++++---------- plugins/t/check_by_ssh.t | 42 +++++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/NEWS b/NEWS index d01b678..62cb47a 100644 --- a/NEWS +++ b/NEWS @@ -23,6 +23,8 @@ This file documents the major additions and syntax changes between releases. negate now has the ability to replace the status text as well (-s, --substitute) Added performance data to check_ping (Christian Schneemann) Added support for --extra-opts in all C plugins (disabled by default, see configure --help) + Fixed passive option in check_by_ssh + On non-skipped stderr, check_by_ssh now returns UNKNOWN or worse (result from command) instead of always UNKNOWN. 1.4.11 13th December 2007 Fixed check_http regression in 1.4.10 where following redirects to diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 9def404..05c3484 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c @@ -100,11 +100,11 @@ main (int argc, char **argv) if (skip_stderr == -1) /* --skip-stderr specified without argument */ skip_stderr = chld_err.lines; - /* UNKNOWN if (non-skipped) output found on stderr */ + /* UNKNOWN or worse if (non-skipped) output found on stderr */ if(chld_err.lines > skip_stderr) { printf (_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); - return STATE_UNKNOWN; + return max_state_alt(result, STATE_UNKNOWN); } /* this is simple if we're not supposed to be passive. @@ -133,21 +133,20 @@ main (int argc, char **argv) local_time = time (NULL); commands = 0; for(i = skip_stdout; i < chld_out.lines; i++) { - status_text = strstr (chld_out.line[i], "STATUS CODE: "); - if (status_text == NULL) { - printf ("%s", chld_out.line[i]); - return result; - } + status_text = chld_out.line[i++]; + if (i == chld_out.lines || strstr (chld_out.line[i], "STATUS CODE: ") == NULL) + die (STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); + if (service[commands] && status_text - && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) + && sscanf (chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int) local_time, host_shortname, service[commands++], - cresult, chld_out.line[i]); + cresult, status_text); } } - /* force an OK state */ + /* Multiple commands and passive checking should always return OK */ return result; } @@ -308,7 +307,7 @@ process_arguments (int argc, char **argv) asprintf (&remotecmd, "%s", argv[c]); } - if (commands > 1) + if (commands > 1 || passive) asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); if (remotecmd == NULL || strlen (remotecmd) <= 1) diff --git a/plugins/t/check_by_ssh.t b/plugins/t/check_by_ssh.t index 8bdb656..88e5405 100644 --- a/plugins/t/check_by_ssh.t +++ b/plugins/t/check_by_ssh.t @@ -20,7 +20,7 @@ my $ssh_key = getTestParameter( "NP_SSH_IDENTITY", plan skip_all => "SSH_HOST and SSH_IDENTITY must be defined" unless ($ssh_service && $ssh_key); -plan tests => 38; +plan tests => 40; # Some random check strings/response my @responce = ('OK: Everything is fine!', @@ -29,9 +29,13 @@ my @responce = ('OK: Everything is fine!', 'UNKNOWN: What can I do for ya?', 'WOOPS: What did I smoke?', ); +my @responce_re; my @check; for (@responce) { push(@check, "echo $_"); + my $re_str = $_; + $re_str =~ s{(.)} { "\Q$1" }ge; + push(@responce_re, $re_str); } my $result; @@ -88,6 +92,7 @@ $result = NPTest->testCmd( ); cmp_ok($result->return_code, '==', 0, "Multiple checks always return OK"); my @lines = split(/\n/, $result->output); +cmp_ok(scalar(@lines), '==', 8, "Correct number of output lined for multiple checks"); my %linemap = ( '0' => '1', '2' => '0', @@ -102,30 +107,41 @@ foreach my $line (0, 2, 4, 6) { } # Passive checks +unlink("/tmp/check_by_ssh.$$"); $result = NPTest->testCmd( "./check_by_ssh -i $ssh_key -H $ssh_service -n flint -s serv -C '$check[2]; sh -c exit\\ 2' -O /tmp/check_by_ssh.$$" ); cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks"); open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!"); -my $count=0; -while () { - like($_, '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;serv;2;$responce[2]$/', 'proper result for passive check'); - $count++; +my @pasv = ; +close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!"); +cmp_ok(scalar(@pasv), '==', 1, 'One passive result for one check performed'); +for (0) { + if ($pasv[$_]) { + like($pasv[$_], '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;serv;2;' . $responce_re[2] . '$/', 'proper result for passive check'); + } else { + fail('proper result for passive check'); + } } -cmp_ok($count, '==', 1, 'One passive result for one check performed'); unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!"); +undef @pasv; $result = NPTest->testCmd( - "./check_by_ssh -i $ssh_key -H $ssh_service -n flint -s c0:c1:c2:c3:c4 -C '$check[0], exit 0' -C '$check[1]; exit 1' -C '$check[2]; exit 2' -C '$check[3]; exit 3' -C '$check[4]; exit 9' -O /tmp/check_by_ssh.$$" + "./check_by_ssh -i $ssh_key -H $ssh_service -n flint -s c0:c1:c2:c3:c4 -C '$check[0];sh -c exit\\ 0' -C '$check[1];sh -c exit\\ 1' -C '$check[2];sh -c exit\\ 2' -C '$check[3];sh -c exit\\ 3' -C '$check[4];sh -c exit\\ 9' -O /tmp/check_by_ssh.$$" ); cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks"); -$count=0; open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!"); -while () { - my $ret; - ($count == 4 ? $ret = 7 : $ret = $count); - like($_, '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;c' . $count . ';' . $ret . ';' . $responce[$count] . '$/', "proper result for passive check $count"); +@pasv = ; +close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!"); +cmp_ok(scalar(@pasv), '==', 5, 'Five passive result for five checks performed'); +for (0, 1, 2, 3, 4) { + if ($pasv[$_]) { + my $ret = $_; + $ret = 9 if ($_ == 4); + like($pasv[$_], '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;c' . $_ . ';' . $ret . ';' . $responce_re[$_] . '$/', "proper result for passive check $_"); + } else { + fail("proper result for passive check $_"); + } } -cmp_ok($count, '==', 5, 'Five passive result for five checks performed'); unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!"); -- 2.30.2