diff options
| author | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | 2008-05-21 08:57:13 +0000 |
|---|---|---|
| committer | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | 2008-05-21 08:57:13 +0000 |
| commit | b52e63da65ac5db7affbd09e110904855f3b1ed7 (patch) | |
| tree | 58f05c66343ca46dc792a3d0842da1598f3ae1f7 /plugins | |
| parent | 6b2cc2f23c6665f0990701a1fd93d4c99840842d (diff) | |
| download | monitoring-plugins-b52e63da65ac5db7affbd09e110904855f3b1ed7.tar.gz | |
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
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/check_by_ssh.c | 21 | ||||
| -rw-r--r-- | plugins/t/check_by_ssh.t | 42 |
2 files changed, 39 insertions, 24 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 9def404d..05c34848 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c | |||
| @@ -100,11 +100,11 @@ main (int argc, char **argv) | |||
| 100 | if (skip_stderr == -1) /* --skip-stderr specified without argument */ | 100 | if (skip_stderr == -1) /* --skip-stderr specified without argument */ |
| 101 | skip_stderr = chld_err.lines; | 101 | skip_stderr = chld_err.lines; |
| 102 | 102 | ||
| 103 | /* UNKNOWN if (non-skipped) output found on stderr */ | 103 | /* UNKNOWN or worse if (non-skipped) output found on stderr */ |
| 104 | if(chld_err.lines > skip_stderr) { | 104 | if(chld_err.lines > skip_stderr) { |
| 105 | printf (_("Remote command execution failed: %s\n"), | 105 | printf (_("Remote command execution failed: %s\n"), |
| 106 | chld_err.line[skip_stderr]); | 106 | chld_err.line[skip_stderr]); |
| 107 | return STATE_UNKNOWN; | 107 | return max_state_alt(result, STATE_UNKNOWN); |
| 108 | } | 108 | } |
| 109 | 109 | ||
| 110 | /* this is simple if we're not supposed to be passive. | 110 | /* this is simple if we're not supposed to be passive. |
| @@ -133,21 +133,20 @@ main (int argc, char **argv) | |||
| 133 | local_time = time (NULL); | 133 | local_time = time (NULL); |
| 134 | commands = 0; | 134 | commands = 0; |
| 135 | for(i = skip_stdout; i < chld_out.lines; i++) { | 135 | for(i = skip_stdout; i < chld_out.lines; i++) { |
| 136 | status_text = strstr (chld_out.line[i], "STATUS CODE: "); | 136 | status_text = chld_out.line[i++]; |
| 137 | if (status_text == NULL) { | 137 | if (i == chld_out.lines || strstr (chld_out.line[i], "STATUS CODE: ") == NULL) |
| 138 | printf ("%s", chld_out.line[i]); | 138 | die (STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); |
| 139 | return result; | 139 | |
| 140 | } | ||
| 141 | if (service[commands] && status_text | 140 | if (service[commands] && status_text |
| 142 | && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) | 141 | && sscanf (chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) |
| 143 | { | 142 | { |
| 144 | fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", | 143 | fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", |
| 145 | (int) local_time, host_shortname, service[commands++], | 144 | (int) local_time, host_shortname, service[commands++], |
| 146 | cresult, chld_out.line[i]); | 145 | cresult, status_text); |
| 147 | } | 146 | } |
| 148 | } | 147 | } |
| 149 | 148 | ||
| 150 | /* force an OK state */ | 149 | /* Multiple commands and passive checking should always return OK */ |
| 151 | return result; | 150 | return result; |
| 152 | } | 151 | } |
| 153 | 152 | ||
| @@ -308,7 +307,7 @@ process_arguments (int argc, char **argv) | |||
| 308 | asprintf (&remotecmd, "%s", argv[c]); | 307 | asprintf (&remotecmd, "%s", argv[c]); |
| 309 | } | 308 | } |
| 310 | 309 | ||
| 311 | if (commands > 1) | 310 | if (commands > 1 || passive) |
| 312 | asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); | 311 | asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); |
| 313 | 312 | ||
| 314 | if (remotecmd == NULL || strlen (remotecmd) <= 1) | 313 | if (remotecmd == NULL || strlen (remotecmd) <= 1) |
diff --git a/plugins/t/check_by_ssh.t b/plugins/t/check_by_ssh.t index 8bdb6567..88e5405c 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", | |||
| 20 | 20 | ||
| 21 | plan skip_all => "SSH_HOST and SSH_IDENTITY must be defined" unless ($ssh_service && $ssh_key); | 21 | plan skip_all => "SSH_HOST and SSH_IDENTITY must be defined" unless ($ssh_service && $ssh_key); |
| 22 | 22 | ||
| 23 | plan tests => 38; | 23 | plan tests => 40; |
| 24 | 24 | ||
| 25 | # Some random check strings/response | 25 | # Some random check strings/response |
| 26 | my @responce = ('OK: Everything is fine!', | 26 | my @responce = ('OK: Everything is fine!', |
| @@ -29,9 +29,13 @@ my @responce = ('OK: Everything is fine!', | |||
| 29 | 'UNKNOWN: What can I do for ya?', | 29 | 'UNKNOWN: What can I do for ya?', |
| 30 | 'WOOPS: What did I smoke?', | 30 | 'WOOPS: What did I smoke?', |
| 31 | ); | 31 | ); |
| 32 | my @responce_re; | ||
| 32 | my @check; | 33 | my @check; |
| 33 | for (@responce) { | 34 | for (@responce) { |
| 34 | push(@check, "echo $_"); | 35 | push(@check, "echo $_"); |
| 36 | my $re_str = $_; | ||
| 37 | $re_str =~ s{(.)} { "\Q$1" }ge; | ||
| 38 | push(@responce_re, $re_str); | ||
| 35 | } | 39 | } |
| 36 | 40 | ||
| 37 | my $result; | 41 | my $result; |
| @@ -88,6 +92,7 @@ $result = NPTest->testCmd( | |||
| 88 | ); | 92 | ); |
| 89 | cmp_ok($result->return_code, '==', 0, "Multiple checks always return OK"); | 93 | cmp_ok($result->return_code, '==', 0, "Multiple checks always return OK"); |
| 90 | my @lines = split(/\n/, $result->output); | 94 | my @lines = split(/\n/, $result->output); |
| 95 | cmp_ok(scalar(@lines), '==', 8, "Correct number of output lined for multiple checks"); | ||
| 91 | my %linemap = ( | 96 | my %linemap = ( |
| 92 | '0' => '1', | 97 | '0' => '1', |
| 93 | '2' => '0', | 98 | '2' => '0', |
| @@ -102,30 +107,41 @@ foreach my $line (0, 2, 4, 6) { | |||
| 102 | } | 107 | } |
| 103 | 108 | ||
| 104 | # Passive checks | 109 | # Passive checks |
| 110 | unlink("/tmp/check_by_ssh.$$"); | ||
| 105 | $result = NPTest->testCmd( | 111 | $result = NPTest->testCmd( |
| 106 | "./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.$$" | 112 | "./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.$$" |
| 107 | ); | 113 | ); |
| 108 | cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks"); | 114 | cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks"); |
| 109 | open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!"); | 115 | open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!"); |
| 110 | my $count=0; | 116 | my @pasv = <PASV>; |
| 111 | while (<PASV>) { | 117 | close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!"); |
| 112 | like($_, '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;serv;2;$responce[2]$/', 'proper result for passive check'); | 118 | cmp_ok(scalar(@pasv), '==', 1, 'One passive result for one check performed'); |
| 113 | $count++; | 119 | for (0) { |
| 120 | if ($pasv[$_]) { | ||
| 121 | like($pasv[$_], '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;serv;2;' . $responce_re[2] . '$/', 'proper result for passive check'); | ||
| 122 | } else { | ||
| 123 | fail('proper result for passive check'); | ||
| 124 | } | ||
| 114 | } | 125 | } |
| 115 | cmp_ok($count, '==', 1, 'One passive result for one check performed'); | ||
| 116 | unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!"); | 126 | unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!"); |
| 127 | undef @pasv; | ||
| 117 | 128 | ||
| 118 | $result = NPTest->testCmd( | 129 | $result = NPTest->testCmd( |
| 119 | "./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.$$" | 130 | "./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.$$" |
| 120 | ); | 131 | ); |
| 121 | cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks"); | 132 | cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks"); |
| 122 | $count=0; | ||
| 123 | open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!"); | 133 | open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!"); |
| 124 | while (<PASV>) { | 134 | @pasv = <PASV>; |
| 125 | my $ret; | 135 | close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!"); |
| 126 | ($count == 4 ? $ret = 7 : $ret = $count); | 136 | cmp_ok(scalar(@pasv), '==', 5, 'Five passive result for five checks performed'); |
| 127 | like($_, '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;c' . $count . ';' . $ret . ';' . $responce[$count] . '$/', "proper result for passive check $count"); | 137 | for (0, 1, 2, 3, 4) { |
| 138 | if ($pasv[$_]) { | ||
| 139 | my $ret = $_; | ||
| 140 | $ret = 9 if ($_ == 4); | ||
| 141 | like($pasv[$_], '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;c' . $_ . ';' . $ret . ';' . $responce_re[$_] . '$/', "proper result for passive check $_"); | ||
| 142 | } else { | ||
| 143 | fail("proper result for passive check $_"); | ||
| 144 | } | ||
| 128 | } | 145 | } |
| 129 | cmp_ok($count, '==', 5, 'Five passive result for five checks performed'); | ||
| 130 | unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!"); | 146 | unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!"); |
| 131 | 147 | ||
