diff options
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/check_by_ssh.c | 65 |
1 files changed, 45 insertions, 20 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 65eee3c5..6855ebc0 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c | |||
| @@ -48,7 +48,8 @@ void print_usage (void); | |||
| 48 | 48 | ||
| 49 | unsigned int commands = 0; | 49 | unsigned int commands = 0; |
| 50 | unsigned int services = 0; | 50 | unsigned int services = 0; |
| 51 | unsigned int skip = 0; | 51 | int skip_stdout = 0; |
| 52 | int skip_stderr = 0; | ||
| 52 | char *remotecmd = NULL; | 53 | char *remotecmd = NULL; |
| 53 | char *comm = NULL; | 54 | char *comm = NULL; |
| 54 | char *hostname = NULL; | 55 | char *hostname = NULL; |
| @@ -92,18 +93,27 @@ main (int argc, char **argv) | |||
| 92 | printf ("%s\n", comm); | 93 | printf ("%s\n", comm); |
| 93 | 94 | ||
| 94 | result = np_runcmd(comm, &chld_out, &chld_err, 0); | 95 | result = np_runcmd(comm, &chld_out, &chld_err, 0); |
| 96 | |||
| 97 | if (skip_stdout == -1) /* --skip-stdout specified without argument */ | ||
| 98 | skip_stdout = chld_out.lines; | ||
| 99 | if (skip_stderr == -1) /* --skip-stderr specified without argument */ | ||
| 100 | skip_stderr = chld_err.lines; | ||
| 101 | |||
| 95 | /* UNKNOWN if (non-skipped) output found on stderr */ | 102 | /* UNKNOWN if (non-skipped) output found on stderr */ |
| 96 | if((signed)chld_err.lines - (signed)skip > 0) { | 103 | if(chld_err.lines > skip_stderr) { |
| 97 | printf(_("Remote command execution failed: %s\n"), | 104 | printf (_("Remote command execution failed: %s\n"), |
| 98 | skip < chld_err.lines ? chld_err.line[skip] : chld_err.buf); | 105 | chld_err.line[skip_stderr]); |
| 99 | return STATE_UNKNOWN; | 106 | return STATE_UNKNOWN; |
| 100 | } | 107 | } |
| 101 | skip -= chld_err.lines; | ||
| 102 | 108 | ||
| 103 | /* this is simple if we're not supposed to be passive. | 109 | /* this is simple if we're not supposed to be passive. |
| 104 | * Wrap up quickly and keep the tricks below */ | 110 | * Wrap up quickly and keep the tricks below */ |
| 105 | if(!passive) { | 111 | if(!passive) { |
| 106 | printf ("%s\n", skip < chld_out.lines ? chld_out.line[skip] : chld_out.buf); | 112 | if (chld_out.lines > skip_stdout) |
| 113 | puts (chld_out.line[skip_stdout]); | ||
| 114 | else | ||
| 115 | printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), | ||
| 116 | state_text(result), remotecmd, result); | ||
| 107 | return result; /* return error status from remote command */ | 117 | return result; /* return error status from remote command */ |
| 108 | } | 118 | } |
| 109 | 119 | ||
| @@ -120,7 +130,7 @@ main (int argc, char **argv) | |||
| 120 | 130 | ||
| 121 | local_time = time (NULL); | 131 | local_time = time (NULL); |
| 122 | commands = 0; | 132 | commands = 0; |
| 123 | for(i = skip; chld_out.line[i]; i++) { | 133 | for(i = skip_stdout; i < chld_out.lines; i++) { |
| 124 | status_text = strstr (chld_out.line[i], "STATUS CODE: "); | 134 | status_text = strstr (chld_out.line[i], "STATUS CODE: "); |
| 125 | if (status_text == NULL) { | 135 | if (status_text == NULL) { |
| 126 | printf ("%s", chld_out.line[i]); | 136 | printf ("%s", chld_out.line[i]); |
| @@ -162,7 +172,9 @@ process_arguments (int argc, char **argv) | |||
| 162 | {"user", required_argument, 0, 'u'}, | 172 | {"user", required_argument, 0, 'u'}, |
| 163 | {"logname", required_argument, 0, 'l'}, | 173 | {"logname", required_argument, 0, 'l'}, |
| 164 | {"command", required_argument, 0, 'C'}, | 174 | {"command", required_argument, 0, 'C'}, |
| 165 | {"skip", required_argument, 0, 'S'}, | 175 | {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */ |
| 176 | {"skip-stdout", optional_argument, 0, 'S'}, | ||
| 177 | {"skip-stderr", optional_argument, 0, 'E'}, | ||
| 166 | {"proto1", no_argument, 0, '1'}, | 178 | {"proto1", no_argument, 0, '1'}, |
| 167 | {"proto2", no_argument, 0, '2'}, | 179 | {"proto2", no_argument, 0, '2'}, |
| 168 | {"use-ipv4", no_argument, 0, '4'}, | 180 | {"use-ipv4", no_argument, 0, '4'}, |
| @@ -180,7 +192,7 @@ process_arguments (int argc, char **argv) | |||
| 180 | strcpy (argv[c], "-t"); | 192 | strcpy (argv[c], "-t"); |
| 181 | 193 | ||
| 182 | while (1) { | 194 | while (1) { |
| 183 | c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S:n:s:o:", longopts, | 195 | c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:", longopts, |
| 184 | &option); | 196 | &option); |
| 185 | 197 | ||
| 186 | if (c == -1 || c == EOF) | 198 | if (c == -1 || c == EOF) |
| @@ -250,11 +262,21 @@ process_arguments (int argc, char **argv) | |||
| 250 | asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); | 262 | asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); |
| 251 | asprintf (&remotecmd, "%s%s", remotecmd, optarg); | 263 | asprintf (&remotecmd, "%s%s", remotecmd, optarg); |
| 252 | break; | 264 | break; |
| 253 | case 'S': /* Skip n lines in the output to ignore system banner */ | 265 | case 'S': /* skip n (or all) lines on stdout */ |
| 254 | if (!is_integer (optarg)) | 266 | if (optarg == NULL) |
| 255 | usage_va(_("skip lines must be an integer")); | 267 | skip_stdout = -1; /* skip all output on stdout */ |
| 268 | else if (!is_integer (optarg)) | ||
| 269 | usage_va(_("skip-stdout argument must be an integer")); | ||
| 270 | else | ||
| 271 | skip_stdout = atoi (optarg); | ||
| 272 | break; | ||
| 273 | case 'E': /* skip n (or all) lines on stderr */ | ||
| 274 | if (optarg == NULL) | ||
| 275 | skip_stderr = -1; /* skip all output on stderr */ | ||
| 276 | else if (!is_integer (optarg)) | ||
| 277 | usage_va(_("skip-stderr argument must be an integer")); | ||
| 256 | else | 278 | else |
| 257 | skip = atoi (optarg); | 279 | skip_stderr = atoi (optarg); |
| 258 | break; | 280 | break; |
| 259 | case 'o': /* Extra options for the ssh command */ | 281 | case 'o': /* Extra options for the ssh command */ |
| 260 | asprintf (&comm, "%s -%c '%s'", comm, c, optarg); | 282 | asprintf (&comm, "%s -%c '%s'", comm, c, optarg); |
| @@ -334,13 +356,15 @@ print_help (void) | |||
| 334 | printf (_(UT_IPv46)); | 356 | printf (_(UT_IPv46)); |
| 335 | 357 | ||
| 336 | printf (" %s\n", "-1, --proto1"); | 358 | printf (" %s\n", "-1, --proto1"); |
| 337 | printf (" %s\n", _("tell ssh to use Protocol 1")); | 359 | printf (" %s\n", _("tell ssh to use Protocol 1 [optional]")); |
| 338 | printf (" %s\n", "-2, --proto2"); | 360 | printf (" %s\n", "-2, --proto2"); |
| 339 | printf (" %s\n", _("tell ssh to use Protocol 2")); | 361 | printf (" %s\n", _("tell ssh to use Protocol 2 [optional]")); |
| 340 | printf (" %s\n", "-S, --skip=n"); | 362 | printf (" %s\n", "-S, --skip-stdout[=n]"); |
| 341 | printf (" %s\n", _("Ignore first n lines on STDERR (to suppress a logon banner)")); | 363 | printf (" %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]")); |
| 364 | printf (" %s\n", "-E, --skip-stderr[=n]"); | ||
| 365 | printf (" %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]")); | ||
| 342 | printf (" %s\n", "-f"); | 366 | printf (" %s\n", "-f"); |
| 343 | printf (" %s\n", _("tells ssh to fork rather than create a tty")); | 367 | printf (" %s\n", _("tells ssh to fork rather than create a tty [optional]")); |
| 344 | printf (" %s\n","-C, --command='COMMAND STRING'"); | 368 | printf (" %s\n","-C, --command='COMMAND STRING'"); |
| 345 | printf (" %s\n", _("command to execute on the remote machine")); | 369 | printf (" %s\n", _("command to execute on the remote machine")); |
| 346 | printf (" %s\n","-l, --logname=USERNAME"); | 370 | printf (" %s\n","-l, --logname=USERNAME"); |
| @@ -385,7 +409,8 @@ print_usage (void) | |||
| 385 | { | 409 | { |
| 386 | printf (_("Usage:")); | 410 | printf (_("Usage:")); |
| 387 | printf (" %s -H <host> -C <command> [-fq] [-1|-2] [-4|-6]\n" | 411 | printf (" %s -H <host> -C <command> [-fq] [-1|-2] [-4|-6]\n" |
| 388 | " [-S lines] [-t timeout] [-i identity] [-l user] [-n name]\n" | 412 | " [-S [lines]] [-E [lines]] [-t timeout] [-i identity]\n" |
| 389 | " [-s servicelist] [-O outputfile] [-p port] [-o ssh-option]\n", | 413 | " [-l user] [-n name] [-s servicelist] [-O outputfile]\n" |
| 414 | " [-p port] [-o ssh-option]\n", | ||
| 390 | progname); | 415 | progname); |
| 391 | } | 416 | } |
