summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-05-21 08:57:13 (GMT)
committerThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-05-21 08:57:13 (GMT)
commitb52e63da65ac5db7affbd09e110904855f3b1ed7 (patch)
tree58f05c66343ca46dc792a3d0842da1598f3ae1f7
parent6b2cc2f23c6665f0990701a1fd93d4c99840842d (diff)
downloadmonitoring-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
-rw-r--r--NEWS2
-rw-r--r--plugins/check_by_ssh.c21
-rw-r--r--plugins/t/check_by_ssh.t42
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.
23 negate now has the ability to replace the status text as well (-s, --substitute) 23 negate now has the ability to replace the status text as well (-s, --substitute)
24 Added performance data to check_ping (Christian Schneemann) 24 Added performance data to check_ping (Christian Schneemann)
25 Added support for --extra-opts in all C plugins (disabled by default, see configure --help) 25 Added support for --extra-opts in all C plugins (disabled by default, see configure --help)
26 Fixed passive option in check_by_ssh
27 On non-skipped stderr, check_by_ssh now returns UNKNOWN or worse (result from command) instead of always UNKNOWN.
26 28
271.4.11 13th December 2007 291.4.11 13th December 2007
28 Fixed check_http regression in 1.4.10 where following redirects to 30 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)
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 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",
20 20
21plan skip_all => "SSH_HOST and SSH_IDENTITY must be defined" unless ($ssh_service && $ssh_key); 21plan skip_all => "SSH_HOST and SSH_IDENTITY must be defined" unless ($ssh_service && $ssh_key);
22 22
23plan tests => 38; 23plan tests => 40;
24 24
25# Some random check strings/response 25# Some random check strings/response
26my @responce = ('OK: Everything is fine!', 26my @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);
32my @responce_re;
32my @check; 33my @check;
33for (@responce) { 34for (@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
37my $result; 41my $result;
@@ -88,6 +92,7 @@ $result = NPTest->testCmd(
88 ); 92 );
89cmp_ok($result->return_code, '==', 0, "Multiple checks always return OK"); 93cmp_ok($result->return_code, '==', 0, "Multiple checks always return OK");
90my @lines = split(/\n/, $result->output); 94my @lines = split(/\n/, $result->output);
95cmp_ok(scalar(@lines), '==', 8, "Correct number of output lined for multiple checks");
91my %linemap = ( 96my %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
110unlink("/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 );
108cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks"); 114cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
109open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!"); 115open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
110my $count=0; 116my @pasv = <PASV>;
111while (<PASV>) { 117close(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'); 118cmp_ok(scalar(@pasv), '==', 1, 'One passive result for one check performed');
113 $count++; 119for (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}
115cmp_ok($count, '==', 1, 'One passive result for one check performed');
116unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!"); 126unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");
127undef @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 );
121cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks"); 132cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
122$count=0;
123open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!"); 133open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
124while (<PASV>) { 134@pasv = <PASV>;
125 my $ret; 135close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!");
126 ($count == 4 ? $ret = 7 : $ret = $count); 136cmp_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"); 137for (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}
129cmp_ok($count, '==', 5, 'Five passive result for five checks performed');
130unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!"); 146unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");
131 147