summaryrefslogtreecommitdiffstats
path: root/plugins/check_by_ssh.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_by_ssh.c')
-rw-r--r--plugins/check_by_ssh.c129
1 files changed, 78 insertions, 51 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c
index 2bc38d49..a43c0d34 100644
--- a/plugins/check_by_ssh.c
+++ b/plugins/check_by_ssh.c
@@ -45,7 +45,8 @@ typedef struct {
45 check_by_ssh_config config; 45 check_by_ssh_config config;
46} check_by_ssh_config_wrapper; 46} check_by_ssh_config_wrapper;
47static check_by_ssh_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); 47static check_by_ssh_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
48static check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper /*config_wrapper*/); 48static check_by_ssh_config_wrapper
49 validate_arguments(check_by_ssh_config_wrapper /*config_wrapper*/);
49 50
50static command_construct comm_append(command_construct /*cmd*/, const char * /*str*/); 51static command_construct comm_append(command_construct /*cmd*/, const char * /*str*/);
51static void print_help(void); 52static void print_help(void);
@@ -90,7 +91,8 @@ int main(int argc, char **argv) {
90 91
91 /* SSH returns 255 if connection attempt fails; include the first line of error output */ 92 /* SSH returns 255 if connection attempt fails; include the first line of error output */
92 if (result == 255 && config.unknown_timeout) { 93 if (result == 255 && config.unknown_timeout) {
93 printf(_("SSH connection failed: %s\n"), chld_err.lines > 0 ? chld_err.line[0] : "(no error output)"); 94 printf(_("SSH connection failed: %s\n"),
95 chld_err.lines > 0 ? chld_err.line[0] : "(no error output)");
94 return STATE_UNKNOWN; 96 return STATE_UNKNOWN;
95 } 97 }
96 98
@@ -117,13 +119,14 @@ int main(int argc, char **argv) {
117 skip_stderr = config.skip_stderr; 119 skip_stderr = config.skip_stderr;
118 } 120 }
119 121
120 /* UNKNOWN or worse if (non-skipped) output found on stderr */ 122 /* Allow UNKNOWN or WARNING state for (non-skipped) output found on stderr */
121 if (chld_err.lines > (size_t)skip_stderr) { 123 if (chld_err.lines > (size_t)skip_stderr && (config.unknown_on_stderr || config.warn_on_stderr)) {
122 printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); 124 printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]);
123 if (config.warn_on_stderr) { 125 if (config.unknown_on_stderr) {
126 return max_state_alt(result, STATE_UNKNOWN);
127 } else if (config.warn_on_stderr) {
124 return max_state_alt(result, STATE_WARNING); 128 return max_state_alt(result, STATE_WARNING);
125 } 129 }
126 return max_state_alt(result, STATE_UNKNOWN);
127 } 130 }
128 131
129 /* this is simple if we're not supposed to be passive. 132 /* this is simple if we're not supposed to be passive.
@@ -134,7 +137,8 @@ int main(int argc, char **argv) {
134 puts(chld_out.line[i]); 137 puts(chld_out.line[i]);
135 } 138 }
136 } else { 139 } else {
137 printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), config.remotecmd, result); 140 printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"),
141 state_text(result), config.remotecmd, result);
138 } 142 }
139 return result; /* return error status from remote command */ 143 return result; /* return error status from remote command */
140 } 144 }
@@ -160,9 +164,11 @@ int main(int argc, char **argv) {
160 die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); 164 die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname);
161 } 165 }
162 166
163 if (config.service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { 167 if (config.service[commands] && status_text &&
164 fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, config.host_shortname, 168 sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) {
165 config.service[commands++], cresult, status_text); 169 fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
170 (int)local_time, config.host_shortname, config.service[commands++], cresult,
171 status_text);
166 } 172 }
167 } 173 }
168 174
@@ -172,34 +178,36 @@ int main(int argc, char **argv) {
172 178
173/* process command-line arguments */ 179/* process command-line arguments */
174check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { 180check_by_ssh_config_wrapper process_arguments(int argc, char **argv) {
175 static struct option longopts[] = {{"version", no_argument, 0, 'V'}, 181 static struct option longopts[] = {
176 {"help", no_argument, 0, 'h'}, 182 {"version", no_argument, 0, 'V'},
177 {"verbose", no_argument, 0, 'v'}, 183 {"help", no_argument, 0, 'h'},
178 {"fork", no_argument, 0, 'f'}, 184 {"verbose", no_argument, 0, 'v'},
179 {"timeout", required_argument, 0, 't'}, 185 {"fork", no_argument, 0, 'f'},
180 {"unknown-timeout", no_argument, 0, 'U'}, 186 {"timeout", required_argument, 0, 't'},
181 {"host", required_argument, 0, 'H'}, /* backward compatibility */ 187 {"unknown-timeout", no_argument, 0, 'U'},
182 {"hostname", required_argument, 0, 'H'}, 188 {"host", required_argument, 0, 'H'}, /* backward compatibility */
183 {"port", required_argument, 0, 'p'}, 189 {"hostname", required_argument, 0, 'H'},
184 {"output", required_argument, 0, 'O'}, 190 {"port", required_argument, 0, 'p'},
185 {"name", required_argument, 0, 'n'}, 191 {"output", required_argument, 0, 'O'},
186 {"services", required_argument, 0, 's'}, 192 {"name", required_argument, 0, 'n'},
187 {"identity", required_argument, 0, 'i'}, 193 {"services", required_argument, 0, 's'},
188 {"user", required_argument, 0, 'u'}, 194 {"identity", required_argument, 0, 'i'},
189 {"logname", required_argument, 0, 'l'}, 195 {"user", required_argument, 0, 'u'}, /* backwards compatibility */
190 {"command", required_argument, 0, 'C'}, 196 {"logname", required_argument, 0, 'l'},
191 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */ 197 {"command", required_argument, 0, 'C'},
192 {"skip-stdout", optional_argument, 0, 'S'}, 198 {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
193 {"skip-stderr", optional_argument, 0, 'E'}, 199 {"skip-stdout", optional_argument, 0, 'S'},
194 {"warn-on-stderr", no_argument, 0, 'W'}, 200 {"skip-stderr", optional_argument, 0, 'E'},
195 {"proto1", no_argument, 0, '1'}, 201 {"unknown-on-stderr", no_argument, 0, 'e'},
196 {"proto2", no_argument, 0, '2'}, 202 {"warn-on-stderr", no_argument, 0, 'W'},
197 {"use-ipv4", no_argument, 0, '4'}, 203 {"proto1", no_argument, 0, '1'},
198 {"use-ipv6", no_argument, 0, '6'}, 204 {"proto2", no_argument, 0, '2'},
199 {"ssh-option", required_argument, 0, 'o'}, 205 {"use-ipv4", no_argument, 0, '4'},
200 {"quiet", no_argument, 0, 'q'}, 206 {"use-ipv6", no_argument, 0, '6'},
201 {"configfile", optional_argument, 0, 'F'}, 207 {"ssh-option", required_argument, 0, 'o'},
202 {0, 0, 0, 0}}; 208 {"quiet", no_argument, 0, 'q'},
209 {"configfile", optional_argument, 0, 'F'},
210 {0, 0, 0, 0}};
203 211
204 check_by_ssh_config_wrapper result = { 212 check_by_ssh_config_wrapper result = {
205 .errorcode = OK, 213 .errorcode = OK,
@@ -221,7 +229,8 @@ check_by_ssh_config_wrapper process_arguments(int argc, char **argv) {
221 229
222 int option = 0; 230 int option = 0;
223 while (true) { 231 while (true) {
224 int opt_index = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); 232 int opt_index =
233 getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option);
225 234
226 if (opt_index == -1 || opt_index == EOF) { 235 if (opt_index == -1 || opt_index == EOF) {
227 break; 236 break;
@@ -266,11 +275,13 @@ check_by_ssh_config_wrapper process_arguments(int argc, char **argv) {
266 char *p2; 275 char *p2;
267 276
268 p1 = optarg; 277 p1 = optarg;
269 result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *)); 278 result.config.service = realloc(result.config.service,
279 (++result.config.number_of_services) * sizeof(char *));
270 while ((p2 = index(p1, ':'))) { 280 while ((p2 = index(p1, ':'))) {
271 *p2 = '\0'; 281 *p2 = '\0';
272 result.config.service[result.config.number_of_services - 1] = p1; 282 result.config.service[result.config.number_of_services - 1] = p1;
273 result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *)); 283 result.config.service = realloc(
284 result.config.service, (++result.config.number_of_services) * sizeof(char *));
274 p1 = p2 + 1; 285 p1 = p2 + 1;
275 } 286 }
276 result.config.service[result.config.number_of_services - 1] = p1; 287 result.config.service[result.config.number_of_services - 1] = p1;
@@ -309,7 +320,8 @@ check_by_ssh_config_wrapper process_arguments(int argc, char **argv) {
309 case 'C': /* Command for remote machine */ 320 case 'C': /* Command for remote machine */
310 result.config.commands++; 321 result.config.commands++;
311 if (result.config.commands > 1) { 322 if (result.config.commands > 1) {
312 xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", result.config.remotecmd); 323 xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;",
324 result.config.remotecmd);
313 } 325 }
314 xasprintf(&result.config.remotecmd, "%s%s", result.config.remotecmd, optarg); 326 xasprintf(&result.config.remotecmd, "%s%s", result.config.remotecmd, optarg);
315 break; 327 break;
@@ -331,6 +343,9 @@ check_by_ssh_config_wrapper process_arguments(int argc, char **argv) {
331 result.config.skip_stderr = atoi(optarg); 343 result.config.skip_stderr = atoi(optarg);
332 } 344 }
333 break; 345 break;
346 case 'e': /* exit with unknown if there is an output on stderr */
347 result.config.unknown_on_stderr = true;
348 break;
334 case 'W': /* exit with warning if there is an output on stderr */ 349 case 'W': /* exit with warning if there is an output on stderr */
335 result.config.warn_on_stderr = true; 350 result.config.warn_on_stderr = true;
336 break; 351 break;
@@ -396,7 +411,8 @@ command_construct comm_append(command_construct cmd, const char *str) {
396 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); 411 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS);
397 } 412 }
398 413
399 if ((cmd.commargv = (char **)realloc(cmd.commargv, (cmd.commargc + 1) * sizeof(char *))) == NULL) { 414 if ((cmd.commargv = (char **)realloc(cmd.commargv, (cmd.commargc + 1) * sizeof(char *))) ==
415 NULL) {
400 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); 416 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n"));
401 } 417 }
402 418
@@ -412,12 +428,18 @@ check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper confi
412 return config_wrapper; 428 return config_wrapper;
413 } 429 }
414 430
415 if (config_wrapper.config.passive && config_wrapper.config.commands != config_wrapper.config.number_of_services) { 431 if (config_wrapper.config.passive &&
416 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); 432 config_wrapper.config.commands != config_wrapper.config.number_of_services) {
433 die(STATE_UNKNOWN,
434 _("%s: In passive mode, you must provide a service name for each command.\n"),
435 progname);
417 } 436 }
418 437
419 if (config_wrapper.config.passive && config_wrapper.config.host_shortname == NULL) { 438 if (config_wrapper.config.passive && config_wrapper.config.host_shortname == NULL) {
420 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); 439 die(STATE_UNKNOWN,
440 _("%s: In passive mode, you must provide the host short name from the monitoring "
441 "configs.\n"),
442 progname);
421 } 443 }
422 444
423 return config_wrapper; 445 return config_wrapper;
@@ -451,10 +473,13 @@ void print_help(void) {
451 printf(" %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]")); 473 printf(" %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]"));
452 printf(" %s\n", "-E, --skip-stderr[=n]"); 474 printf(" %s\n", "-E, --skip-stderr[=n]");
453 printf(" %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]")); 475 printf(" %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]"));
454 printf(" %s\n", "-W, --warn-on-stderr]"); 476 printf(" %s\n", "-e, --unknown-on-stderr");
455 printf(" %s\n", _("Exit with an warning, if there is an output on STDERR")); 477 printf(" %s\n", _("Exit with UNKNOWN, if there is output on STDERR"));
478 printf(" %s\n", "-W, --warn-on-stderr");
479 printf(" %s\n", _("Exit with WARNING, if there is output on STDERR"));
456 printf(" %s\n", "-f"); 480 printf(" %s\n", "-f");
457 printf(" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always return OK if ssh is executed")); 481 printf(" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always "
482 "return OK if ssh is executed"));
458 printf(" %s\n", "-C, --command='COMMAND STRING'"); 483 printf(" %s\n", "-C, --command='COMMAND STRING'");
459 printf(" %s\n", _("command to execute on the remote machine")); 484 printf(" %s\n", _("command to execute on the remote machine"));
460 printf(" %s\n", "-l, --logname=USERNAME"); 485 printf(" %s\n", "-l, --logname=USERNAME");
@@ -490,7 +515,9 @@ void print_help(void) {
490 printf(" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)")); 515 printf(" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
491 printf("\n"); 516 printf("\n");
492 printf("%s\n", _("Examples:")); 517 printf("%s\n", _("Examples:"));
493 printf(" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo"); 518 printf(
519 " %s\n",
520 "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
494 printf(" %s\n", "$ cat /tmp/foo"); 521 printf(" %s\n", "$ cat /tmp/foo");
495 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days"); 522 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
496 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days"); 523 printf(" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
@@ -502,7 +529,7 @@ void print_help(void) {
502void print_usage(void) { 529void print_usage(void) {
503 printf("%s\n", _("Usage:")); 530 printf("%s\n", _("Usage:"));
504 printf(" %s -H <host> -C <command> [-fqvU] [-1|-2] [-4|-6]\n" 531 printf(" %s -H <host> -C <command> [-fqvU] [-1|-2] [-4|-6]\n"
505 " [-S [lines]] [-E [lines]] [-W] [-t timeout] [-i identity]\n" 532 " [-S [lines]] [-E [lines]] [-e|-W] [-t timeout] [-i identity]\n"
506 " [-l user] [-n name] [-s servicelist] [-O outputfile]\n" 533 " [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
507 " [-p port] [-o ssh-option] [-F configfile]\n", 534 " [-p port] [-o ssh-option] [-F configfile]\n",
508 progname); 535 progname);