summaryrefslogtreecommitdiffstats
path: root/plugins/check_tcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_tcp.c')
-rw-r--r--plugins/check_tcp.c47
1 files changed, 23 insertions, 24 deletions
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c
index 315c7525..e502c0d1 100644
--- a/plugins/check_tcp.c
+++ b/plugins/check_tcp.c
@@ -40,6 +40,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
40#include "common.h" 40#include "common.h"
41#include "netutils.h" 41#include "netutils.h"
42#include "utils.h" 42#include "utils.h"
43#include "utils_tcp.h"
43 44
44#ifdef HAVE_SSL 45#ifdef HAVE_SSL
45static int check_cert = FALSE; 46static int check_cert = FALSE;
@@ -90,6 +91,7 @@ static int expect_mismatch_state = STATE_WARNING;
90#define FLAG_TIME_WARN 0x08 91#define FLAG_TIME_WARN 0x08
91#define FLAG_TIME_CRIT 0x10 92#define FLAG_TIME_CRIT 0x10
92#define FLAG_HIDE_OUTPUT 0x20 93#define FLAG_HIDE_OUTPUT 0x20
94#define FLAG_MATCH_ALL 0x40
93static size_t flags = FLAG_EXACT_MATCH; 95static size_t flags = FLAG_EXACT_MATCH;
94 96
95int 97int
@@ -99,7 +101,8 @@ main (int argc, char **argv)
99 int i; 101 int i;
100 char *status = NULL; 102 char *status = NULL;
101 struct timeval tv; 103 struct timeval tv;
102 size_t len, match = -1; 104 size_t len;
105 int match = -1;
103 106
104 setlocale (LC_ALL, ""); 107 setlocale (LC_ALL, "");
105 bindtextdomain (PACKAGE, LOCALEDIR); 108 bindtextdomain (PACKAGE, LOCALEDIR);
@@ -298,22 +301,12 @@ main (int argc, char **argv)
298 (int)len + 1, status); 301 (int)len + 1, status);
299 while(isspace(status[len])) status[len--] = '\0'; 302 while(isspace(status[len])) status[len--] = '\0';
300 303
301 for (i = 0; i < server_expect_count; i++) { 304 match = np_expect_match(status,
302 match = -2; /* tag it so we know if we tried and failed */ 305 server_expect,
303 if (flags & FLAG_VERBOSE) 306 server_expect_count,
304 printf ("looking for [%s] %s [%s]\n", server_expect[i], 307 (flags & FLAG_MATCH_ALL ? TRUE : FALSE),
305 (flags & FLAG_EXACT_MATCH) ? "in beginning of" : "anywhere in", 308 (flags & FLAG_EXACT_MATCH ? TRUE : FALSE),
306 status); 309 (flags & FLAG_VERBOSE ? TRUE : FALSE));
307
308 /* match it. math first in short-circuit */
309 if ((flags & FLAG_EXACT_MATCH && !strncmp(status, server_expect[i], strlen(server_expect[i]))) ||
310 (!(flags & FLAG_EXACT_MATCH) && strstr(status, server_expect[i])))
311 {
312 if(flags & FLAG_VERBOSE) puts("found it");
313 match = i;
314 break;
315 }
316 }
317 } 310 }
318 311
319 if (server_quit != NULL) { 312 if (server_quit != NULL) {
@@ -333,7 +326,7 @@ main (int argc, char **argv)
333 result = STATE_WARNING; 326 result = STATE_WARNING;
334 327
335 /* did we get the response we hoped? */ 328 /* did we get the response we hoped? */
336 if(match == -2 && result != STATE_CRITICAL) 329 if(match == FALSE && result != STATE_CRITICAL)
337 result = expect_mismatch_state; 330 result = expect_mismatch_state;
338 331
339 /* reset the alarm */ 332 /* reset the alarm */
@@ -344,10 +337,10 @@ main (int argc, char **argv)
344 * the response we were looking for. if-else */ 337 * the response we were looking for. if-else */
345 printf("%s %s - ", SERVICE, state_text(result)); 338 printf("%s %s - ", SERVICE, state_text(result));
346 339
347 if(match == -2 && len && !(flags & FLAG_HIDE_OUTPUT)) 340 if(match == FALSE && len && !(flags & FLAG_HIDE_OUTPUT))
348 printf("Unexpected response from host/socket: %s", status); 341 printf("Unexpected response from host/socket: %s", status);
349 else { 342 else {
350 if(match == -2) 343 if(match == FALSE)
351 printf("Unexpected response from host/socket on "); 344 printf("Unexpected response from host/socket on ");
352 else 345 else
353 printf("%.3f second response time on ", elapsed_time); 346 printf("%.3f second response time on ", elapsed_time);
@@ -357,13 +350,13 @@ main (int argc, char **argv)
357 printf("socket %s", server_address); 350 printf("socket %s", server_address);
358 } 351 }
359 352
360 if (match != -2 && !(flags & FLAG_HIDE_OUTPUT) && len) 353 if (match != FALSE && !(flags & FLAG_HIDE_OUTPUT) && len)
361 printf (" [%s]", status); 354 printf (" [%s]", status);
362 355
363 /* perf-data doesn't apply when server doesn't talk properly, 356 /* perf-data doesn't apply when server doesn't talk properly,
364 * so print all zeroes on warn and crit. Use fperfdata since 357 * so print all zeroes on warn and crit. Use fperfdata since
365 * localisation settings can make different outputs */ 358 * localisation settings can make different outputs */
366 if(match == -2) 359 if(match == FALSE)
367 printf ("|%s", 360 printf ("|%s",
368 fperfdata ("time", elapsed_time, "s", 361 fperfdata ("time", elapsed_time, "s",
369 (flags & FLAG_TIME_WARN ? TRUE : FALSE), 0, 362 (flags & FLAG_TIME_WARN ? TRUE : FALSE), 0,
@@ -404,6 +397,7 @@ process_arguments (int argc, char **argv)
404 {"protocol", required_argument, 0, 'P'}, 397 {"protocol", required_argument, 0, 'P'},
405 {"port", required_argument, 0, 'p'}, 398 {"port", required_argument, 0, 'p'},
406 {"escape", required_argument, 0, 'E'}, 399 {"escape", required_argument, 0, 'E'},
400 {"all", required_argument, 0, 'A'},
407 {"send", required_argument, 0, 's'}, 401 {"send", required_argument, 0, 's'},
408 {"expect", required_argument, 0, 'e'}, 402 {"expect", required_argument, 0, 'e'},
409 {"maxbytes", required_argument, 0, 'm'}, 403 {"maxbytes", required_argument, 0, 'm'},
@@ -445,7 +439,7 @@ process_arguments (int argc, char **argv)
445 } 439 }
446 440
447 while (1) { 441 while (1) {
448 c = getopt_long (argc, argv, "+hVv46EH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", 442 c = getopt_long (argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:",
449 longopts, &option); 443 longopts, &option);
450 444
451 if (c == -1 || c == EOF || c == 1) 445 if (c == -1 || c == EOF || c == 1)
@@ -581,6 +575,9 @@ process_arguments (int argc, char **argv)
581 die (STATE_UNKNOWN, _("Invalid option - SSL is not available")); 575 die (STATE_UNKNOWN, _("Invalid option - SSL is not available"));
582#endif 576#endif
583 break; 577 break;
578 case 'A':
579 flags |= FLAG_MATCH_ALL;
580 break;
584 } 581 }
585 } 582 }
586 583
@@ -619,9 +616,11 @@ print_help (void)
619 printf (" %s\n", _("String to send to the server")); 616 printf (" %s\n", _("String to send to the server"));
620 printf (" %s\n", "-e, --expect=STRING"); 617 printf (" %s\n", "-e, --expect=STRING");
621 printf (" %s %s\n", _("String to expect in server response"), _("(may be repeated)")); 618 printf (" %s %s\n", _("String to expect in server response"), _("(may be repeated)"));
619 printf (" %s\n", "-A, --all");
620 printf (" %s\n", _("All expect strings need to occur in server response. Default is any"));
622 printf (" %s\n", "-q, --quit=STRING"); 621 printf (" %s\n", "-q, --quit=STRING");
623 printf (" %s\n", _("String to send server to initiate a clean close of the connection")); 622 printf (" %s\n", _("String to send server to initiate a clean close of the connection"));
624 printf (" %s\n", "-r, --refuse=ok|warn|crit"); 623 printf (" %s\n", "-r, --refuse=ok|warn|crit");
625 printf (" %s\n", _("Accept tcp refusals with states ok, warn, crit (default: crit)")); 624 printf (" %s\n", _("Accept tcp refusals with states ok, warn, crit (default: crit)"));
626 printf (" %s\n", "-M, --mismatch=ok|warn|crit"); 625 printf (" %s\n", "-M, --mismatch=ok|warn|crit");
627 printf (" %s\n", _("Accept expected string mismatches with states ok, warn, crit (default: warn)")); 626 printf (" %s\n", _("Accept expected string mismatches with states ok, warn, crit (default: warn)"));