summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/check_by_ssh.c99
1 files changed, 63 insertions, 36 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c
index 905b2393..2ac7805d 100644
--- a/plugins/check_by_ssh.c
+++ b/plugins/check_by_ssh.c
@@ -82,8 +82,9 @@ int main(int argc, char **argv) {
82 argv = np_extra_opts(&argc, argv, progname); 82 argv = np_extra_opts(&argc, argv, progname);
83 83
84 /* process arguments */ 84 /* process arguments */
85 if (process_arguments(argc, argv) == ERROR) 85 if (process_arguments(argc, argv) == ERROR) {
86 usage_va(_("Could not parse arguments")); 86 usage_va(_("Could not parse arguments"));
87 }
87 88
88 /* Set signal handling and alarm timeout */ 89 /* Set signal handling and alarm timeout */
89 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { 90 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
@@ -94,8 +95,9 @@ int main(int argc, char **argv) {
94 /* run the command */ 95 /* run the command */
95 if (verbose) { 96 if (verbose) {
96 printf("Command: %s\n", commargv[0]); 97 printf("Command: %s\n", commargv[0]);
97 for (int i = 1; i < commargc; i++) 98 for (int i = 1; i < commargc; i++) {
98 printf("Argument %i: %s\n", i, commargv[i]); 99 printf("Argument %i: %s\n", i, commargv[i]);
100 }
99 } 101 }
100 102
101 result = cmd_run_array(commargv, &chld_out, &chld_err, 0); 103 result = cmd_run_array(commargv, &chld_out, &chld_err, 0);
@@ -107,33 +109,40 @@ int main(int argc, char **argv) {
107 } 109 }
108 110
109 if (verbose) { 111 if (verbose) {
110 for (size_t i = 0; i < chld_out.lines; i++) 112 for (size_t i = 0; i < chld_out.lines; i++) {
111 printf("stdout: %s\n", chld_out.line[i]); 113 printf("stdout: %s\n", chld_out.line[i]);
112 for (size_t i = 0; i < chld_err.lines; i++) 114 }
115 for (size_t i = 0; i < chld_err.lines; i++) {
113 printf("stderr: %s\n", chld_err.line[i]); 116 printf("stderr: %s\n", chld_err.line[i]);
117 }
114 } 118 }
115 119
116 if (skip_stdout == -1) /* --skip-stdout specified without argument */ 120 if (skip_stdout == -1) { /* --skip-stdout specified without argument */
117 skip_stdout = chld_out.lines; 121 skip_stdout = chld_out.lines;
118 if (skip_stderr == -1) /* --skip-stderr specified without argument */ 122 }
123 if (skip_stderr == -1) { /* --skip-stderr specified without argument */
119 skip_stderr = chld_err.lines; 124 skip_stderr = chld_err.lines;
125 }
120 126
121 /* UNKNOWN or worse if (non-skipped) output found on stderr */ 127 /* UNKNOWN or worse if (non-skipped) output found on stderr */
122 if (chld_err.lines > (size_t)skip_stderr) { 128 if (chld_err.lines > (size_t)skip_stderr) {
123 printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); 129 printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]);
124 if (warn_on_stderr) 130 if (warn_on_stderr) {
125 return max_state_alt(result, STATE_WARNING); 131 return max_state_alt(result, STATE_WARNING);
132 }
126 return max_state_alt(result, STATE_UNKNOWN); 133 return max_state_alt(result, STATE_UNKNOWN);
127 } 134 }
128 135
129 /* this is simple if we're not supposed to be passive. 136 /* this is simple if we're not supposed to be passive.
130 * Wrap up quickly and keep the tricks below */ 137 * Wrap up quickly and keep the tricks below */
131 if (!passive) { 138 if (!passive) {
132 if (chld_out.lines > (size_t)skip_stdout) 139 if (chld_out.lines > (size_t)skip_stdout) {
133 for (size_t i = skip_stdout; i < chld_out.lines; i++) 140 for (size_t i = skip_stdout; i < chld_out.lines; i++) {
134 puts(chld_out.line[i]); 141 puts(chld_out.line[i]);
135 else 142 }
143 } else {
136 printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), remotecmd, result); 144 printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), remotecmd, result);
145 }
137 return result; /* return error status from remote command */ 146 return result; /* return error status from remote command */
138 } 147 }
139 148
@@ -151,8 +160,9 @@ int main(int argc, char **argv) {
151 commands = 0; 160 commands = 0;
152 for (size_t i = skip_stdout; i < chld_out.lines; i++) { 161 for (size_t i = skip_stdout; i < chld_out.lines; i++) {
153 status_text = chld_out.line[i++]; 162 status_text = chld_out.line[i++];
154 if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) 163 if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) {
155 die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); 164 die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname);
165 }
156 166
157 if (service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { 167 if (service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) {
158 fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, host_shortname, service[commands++], 168 fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, host_shortname, service[commands++],
@@ -200,18 +210,22 @@ int process_arguments(int argc, char **argv) {
200 {"configfile", optional_argument, 0, 'F'}, 210 {"configfile", optional_argument, 0, 'F'},
201 {0, 0, 0, 0}}; 211 {0, 0, 0, 0}};
202 212
203 if (argc < 2) 213 if (argc < 2) {
204 return ERROR; 214 return ERROR;
215 }
205 216
206 for (c = 1; c < argc; c++) 217 for (c = 1; c < argc; c++) {
207 if (strcmp("-to", argv[c]) == 0) 218 if (strcmp("-to", argv[c]) == 0) {
208 strcpy(argv[c], "-t"); 219 strcpy(argv[c], "-t");
220 }
221 }
209 222
210 while (1) { 223 while (1) {
211 c = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); 224 c = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option);
212 225
213 if (c == -1 || c == EOF) 226 if (c == -1 || c == EOF) {
214 break; 227 break;
228 }
215 229
216 switch (c) { 230 switch (c) {
217 case 'V': /* version */ 231 case 'V': /* version */
@@ -224,10 +238,11 @@ int process_arguments(int argc, char **argv) {
224 verbose = true; 238 verbose = true;
225 break; 239 break;
226 case 't': /* timeout period */ 240 case 't': /* timeout period */
227 if (!is_integer(optarg)) 241 if (!is_integer(optarg)) {
228 usage_va(_("Timeout interval must be a positive integer")); 242 usage_va(_("Timeout interval must be a positive integer"));
229 else 243 } else {
230 timeout_interval = atoi(optarg); 244 timeout_interval = atoi(optarg);
245 }
231 break; 246 break;
232 case 'U': 247 case 'U':
233 unknown_timeout = true; 248 unknown_timeout = true;
@@ -236,8 +251,9 @@ int process_arguments(int argc, char **argv) {
236 hostname = optarg; 251 hostname = optarg;
237 break; 252 break;
238 case 'p': /* port number */ 253 case 'p': /* port number */
239 if (!is_integer(optarg)) 254 if (!is_integer(optarg)) {
240 usage_va(_("Port must be a positive integer")); 255 usage_va(_("Port must be a positive integer"));
256 }
241 comm_append("-p"); 257 comm_append("-p");
242 comm_append(optarg); 258 comm_append(optarg);
243 break; 259 break;
@@ -290,25 +306,28 @@ int process_arguments(int argc, char **argv) {
290 break; 306 break;
291 case 'C': /* Command for remote machine */ 307 case 'C': /* Command for remote machine */
292 commands++; 308 commands++;
293 if (commands > 1) 309 if (commands > 1) {
294 xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); 310 xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
311 }
295 xasprintf(&remotecmd, "%s%s", remotecmd, optarg); 312 xasprintf(&remotecmd, "%s%s", remotecmd, optarg);
296 break; 313 break;
297 case 'S': /* skip n (or all) lines on stdout */ 314 case 'S': /* skip n (or all) lines on stdout */
298 if (optarg == NULL) 315 if (optarg == NULL) {
299 skip_stdout = -1; /* skip all output on stdout */ 316 skip_stdout = -1; /* skip all output on stdout */
300 else if (!is_integer(optarg)) 317 } else if (!is_integer(optarg)) {
301 usage_va(_("skip-stdout argument must be an integer")); 318 usage_va(_("skip-stdout argument must be an integer"));
302 else 319 } else {
303 skip_stdout = atoi(optarg); 320 skip_stdout = atoi(optarg);
321 }
304 break; 322 break;
305 case 'E': /* skip n (or all) lines on stderr */ 323 case 'E': /* skip n (or all) lines on stderr */
306 if (optarg == NULL) 324 if (optarg == NULL) {
307 skip_stderr = -1; /* skip all output on stderr */ 325 skip_stderr = -1; /* skip all output on stderr */
308 else if (!is_integer(optarg)) 326 } else if (!is_integer(optarg)) {
309 usage_va(_("skip-stderr argument must be an integer")); 327 usage_va(_("skip-stderr argument must be an integer"));
310 else 328 } else {
311 skip_stderr = atoi(optarg); 329 skip_stderr = atoi(optarg);
330 }
312 break; 331 break;
313 case 'W': /* exit with warning if there is an output on stderr */ 332 case 'W': /* exit with warning if there is an output on stderr */
314 warn_on_stderr = 1; 333 warn_on_stderr = 1;
@@ -338,18 +357,22 @@ int process_arguments(int argc, char **argv) {
338 } 357 }
339 358
340 if (strlen(remotecmd) == 0) { 359 if (strlen(remotecmd) == 0) {
341 for (; c < argc; c++) 360 for (; c < argc; c++) {
342 if (strlen(remotecmd) > 0) 361 if (strlen(remotecmd) > 0) {
343 xasprintf(&remotecmd, "%s %s", remotecmd, argv[c]); 362 xasprintf(&remotecmd, "%s %s", remotecmd, argv[c]);
344 else 363 } else {
345 xasprintf(&remotecmd, "%s", argv[c]); 364 xasprintf(&remotecmd, "%s", argv[c]);
365 }
366 }
346 } 367 }
347 368
348 if (commands > 1 || passive) 369 if (commands > 1 || passive) {
349 xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); 370 xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
371 }
350 372
351 if (remotecmd == NULL || strlen(remotecmd) <= 1) 373 if (remotecmd == NULL || strlen(remotecmd) <= 1) {
352 usage_va(_("No remotecmd")); 374 usage_va(_("No remotecmd"));
375 }
353 376
354 comm_append(hostname); 377 comm_append(hostname);
355 comm_append(remotecmd); 378 comm_append(remotecmd);
@@ -359,25 +382,30 @@ int process_arguments(int argc, char **argv) {
359 382
360void comm_append(const char *str) { 383void comm_append(const char *str) {
361 384
362 if (++commargc > NP_MAXARGS) 385 if (++commargc > NP_MAXARGS) {
363 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); 386 die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS);
387 }
364 388
365 if ((commargv = (char **)realloc(commargv, (commargc + 1) * sizeof(char *))) == NULL) 389 if ((commargv = (char **)realloc(commargv, (commargc + 1) * sizeof(char *))) == NULL) {
366 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); 390 die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n"));
391 }
367 392
368 commargv[commargc - 1] = strdup(str); 393 commargv[commargc - 1] = strdup(str);
369 commargv[commargc] = NULL; 394 commargv[commargc] = NULL;
370} 395}
371 396
372int validate_arguments(void) { 397int validate_arguments(void) {
373 if (remotecmd == NULL || hostname == NULL) 398 if (remotecmd == NULL || hostname == NULL) {
374 return ERROR; 399 return ERROR;
400 }
375 401
376 if (passive && commands != services) 402 if (passive && commands != services) {
377 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); 403 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
404 }
378 405
379 if (passive && host_shortname == NULL) 406 if (passive && host_shortname == NULL) {
380 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); 407 die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname);
408 }
381 409
382 return OK; 410 return OK;
383} 411}
@@ -432,7 +460,6 @@ void print_help(void) {
432 printf(" %s\n", _("Tell ssh to use this configfile [optional]")); 460 printf(" %s\n", _("Tell ssh to use this configfile [optional]"));
433 printf(" %s\n", "-q, --quiet"); 461 printf(" %s\n", "-q, --quiet");
434 printf(" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]")); 462 printf(" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
435 printf(UT_WARN_CRIT);
436 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 463 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
437 printf(" %s\n", "-U, --unknown-timeout"); 464 printf(" %s\n", "-U, --unknown-timeout");
438 printf(" %s\n", _("Make connection problems return UNKNOWN instead of CRITICAL")); 465 printf(" %s\n", _("Make connection problems return UNKNOWN instead of CRITICAL"));