diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2006-03-29 16:33:36 (GMT) |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2006-03-29 16:33:36 (GMT) |
commit | f5c1cf6dd406be0b795f7388617d5469c6a358be (patch) | |
tree | 841b908d652b621846457a1e46f12c397a315286 /plugins | |
parent | a0f387467691292fe62c66e56fbc8476c7ecbb2d (diff) | |
download | monitoring-plugins-f5c1cf6dd406be0b795f7388617d5469c6a358be.tar.gz |
New function to for escaped strings from command line for send/quit.
Adapted from Sebastian Wiesinger's patch (1292404)
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1365 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_tcp.c | 55 | ||||
-rw-r--r-- | plugins/tests/test_utils.c | 31 | ||||
-rw-r--r-- | plugins/utils.c | 30 | ||||
-rw-r--r-- | plugins/utils.h | 2 |
4 files changed, 85 insertions, 33 deletions
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index d8fc26e..5fe024b 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c | |||
@@ -54,8 +54,6 @@ static int server_port = 0; | |||
54 | static char *server_address = NULL; | 54 | static char *server_address = NULL; |
55 | static char *server_send = NULL; | 55 | static char *server_send = NULL; |
56 | static char *server_quit = NULL; | 56 | static char *server_quit = NULL; |
57 | char *lineend = ""; | ||
58 | char *lineendquit = "\r\n"; | ||
59 | static char **server_expect; | 57 | static char **server_expect; |
60 | static size_t server_expect_count = 0; | 58 | static size_t server_expect_count = 0; |
61 | static size_t maxbytes = 0; | 59 | static size_t maxbytes = 0; |
@@ -246,6 +244,12 @@ main (int argc, char **argv) | |||
246 | } | 244 | } |
247 | 245 | ||
248 | if(flags & FLAG_VERBOSE) { | 246 | if(flags & FLAG_VERBOSE) { |
247 | if (server_send) { | ||
248 | printf("Send string: %s\n", server_send); | ||
249 | } | ||
250 | if (server_quit) { | ||
251 | printf("Quit string: %s\n", server_quit); | ||
252 | } | ||
249 | printf("server_expect_count: %d\n", (int)server_expect_count); | 253 | printf("server_expect_count: %d\n", (int)server_expect_count); |
250 | for(i = 0; i < server_expect_count; i++) | 254 | for(i = 0; i < server_expect_count; i++) |
251 | printf("\t%d: %s\n", i, server_expect[i]); | 255 | printf("\t%d: %s\n", i, server_expect[i]); |
@@ -364,6 +368,7 @@ static int | |||
364 | process_arguments (int argc, char **argv) | 368 | process_arguments (int argc, char **argv) |
365 | { | 369 | { |
366 | int c; | 370 | int c; |
371 | int escape = 0; | ||
367 | 372 | ||
368 | int option = 0; | 373 | int option = 0; |
369 | static struct option longopts[] = { | 374 | static struct option longopts[] = { |
@@ -375,7 +380,7 @@ process_arguments (int argc, char **argv) | |||
375 | {"timeout", required_argument, 0, 't'}, | 380 | {"timeout", required_argument, 0, 't'}, |
376 | {"protocol", required_argument, 0, 'P'}, | 381 | {"protocol", required_argument, 0, 'P'}, |
377 | {"port", required_argument, 0, 'p'}, | 382 | {"port", required_argument, 0, 'p'}, |
378 | {"lineend", required_argument, 0, 'l'}, | 383 | {"escape", required_argument, 0, 'E'}, |
379 | {"send", required_argument, 0, 's'}, | 384 | {"send", required_argument, 0, 's'}, |
380 | {"expect", required_argument, 0, 'e'}, | 385 | {"expect", required_argument, 0, 'e'}, |
381 | {"maxbytes", required_argument, 0, 'm'}, | 386 | {"maxbytes", required_argument, 0, 'm'}, |
@@ -417,7 +422,7 @@ process_arguments (int argc, char **argv) | |||
417 | } | 422 | } |
418 | 423 | ||
419 | while (1) { | 424 | while (1) { |
420 | c = getopt_long (argc, argv, "+hVv46H:l:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", | 425 | c = getopt_long (argc, argv, "+hVv46EH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", |
421 | longopts, &option); | 426 | longopts, &option); |
422 | 427 | ||
423 | if (c == -1 || c == EOF || c == 1) | 428 | if (c == -1 || c == EOF || c == 1) |
@@ -485,30 +490,14 @@ process_arguments (int argc, char **argv) | |||
485 | else | 490 | else |
486 | server_port = atoi (optarg); | 491 | server_port = atoi (optarg); |
487 | break; | 492 | break; |
488 | case 'l': | 493 | case 'E': |
489 | switch (*optarg) { | 494 | escape = 1; |
490 | case 'n': | ||
491 | lineend = strdup("\n"); | ||
492 | lineendquit = lineend; | ||
493 | break; | ||
494 | case 'r': | ||
495 | lineend = strdup("\r"); | ||
496 | lineendquit = lineend; | ||
497 | break; | ||
498 | case 'b': | ||
499 | lineend = strdup("\r\n"); | ||
500 | lineendquit = lineend; | ||
501 | break; | ||
502 | case 'e': | ||
503 | lineend = strdup(""); | ||
504 | lineendquit = lineend; | ||
505 | break; | ||
506 | default: | ||
507 | usage4 (_("Unrecognized option to -l, must be r, n, b or e")); | ||
508 | } | ||
509 | break; | 495 | break; |
510 | case 's': | 496 | case 's': |
511 | asprintf(&server_send, "%s%s", optarg, lineend); | 497 | if (escape) |
498 | server_send = np_escaped_string(optarg); | ||
499 | else | ||
500 | asprintf(&server_send, "%s", optarg); | ||
512 | break; | 501 | break; |
513 | case 'e': /* expect string (may be repeated) */ | 502 | case 'e': /* expect string (may be repeated) */ |
514 | EXPECT = NULL; | 503 | EXPECT = NULL; |
@@ -525,7 +514,10 @@ process_arguments (int argc, char **argv) | |||
525 | else | 514 | else |
526 | maxbytes = strtol (optarg, NULL, 0); | 515 | maxbytes = strtol (optarg, NULL, 0); |
527 | case 'q': | 516 | case 'q': |
528 | asprintf(&server_quit, "%s%s", optarg, lineendquit); | 517 | if (escape) |
518 | server_quit = np_escaped_string(optarg); | ||
519 | else | ||
520 | asprintf(&server_quit, "%s\r\n", optarg); | ||
529 | break; | 521 | break; |
530 | case 'r': | 522 | case 'r': |
531 | if (!strncmp(optarg,"ok",2)) | 523 | if (!strncmp(optarg,"ok",2)) |
@@ -604,10 +596,9 @@ print_help (void) | |||
604 | printf (_(UT_IPv46)); | 596 | printf (_(UT_IPv46)); |
605 | 597 | ||
606 | printf (_("\ | 598 | printf (_("\ |
607 | -l, --lineend=b|e|n|r\n\ | 599 | -E, --escape\n\ |
608 | Ending on -s and -q strings. b - both: <cr><lf> style, e - empty no\n\ | 600 | Can use \\n, \\r, \\t or \\ in send or quit string.\n\ |
609 | end, n - newline: newline end, r - return: carriage return end\n\ | 601 | Default: nothing added to send, \\r\\n added to end of quit\n\ |
610 | Default is \"-l e -s <send> -l b -q <quit>\".\n\ | ||
611 | -s, --send=STRING\n\ | 602 | -s, --send=STRING\n\ |
612 | String to send to the server\n\ | 603 | String to send to the server\n\ |
613 | -e, --expect=STRING\n\ | 604 | -e, --expect=STRING\n\ |
@@ -653,6 +644,6 @@ Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\ | |||
653 | [-s <send string>] [-e <expect string>] [-q <quit string>]\n\ | 644 | [-s <send string>] [-e <expect string>] [-q <quit string>]\n\ |
654 | [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\ | 645 | [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\ |
655 | [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n\ | 646 | [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n\ |
656 | [-D <days to cert expiry>] [-S <use SSL>] [-l <n|r|b|e>]\n", progname); | 647 | [-D <days to cert expiry>] [-S <use SSL>] [-E]\n", progname); |
657 | } | 648 | } |
658 | 649 | ||
diff --git a/plugins/tests/test_utils.c b/plugins/tests/test_utils.c index 27e28c7..5aa0028 100644 --- a/plugins/tests/test_utils.c +++ b/plugins/tests/test_utils.c | |||
@@ -34,7 +34,7 @@ main (int argc, char **argv) | |||
34 | thresholds *thresholds = NULL; | 34 | thresholds *thresholds = NULL; |
35 | int rc; | 35 | int rc; |
36 | 36 | ||
37 | plan_tests(66); | 37 | plan_tests(73); |
38 | 38 | ||
39 | range = parse_range_string("6"); | 39 | range = parse_range_string("6"); |
40 | ok( range != NULL, "'6' is valid range"); | 40 | ok( range != NULL, "'6' is valid range"); |
@@ -136,6 +136,35 @@ main (int argc, char **argv) | |||
136 | ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); | 136 | ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); |
137 | ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); | 137 | ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); |
138 | 138 | ||
139 | char *test; | ||
140 | test = np_escaped_string("bob\\n"); | ||
141 | ok( strcmp(test, "bob\n") == 0, "bob\\n ok"); | ||
142 | free(test); | ||
143 | |||
144 | test = np_escaped_string("rhuba\\rb"); | ||
145 | ok( strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay"); | ||
146 | free(test); | ||
147 | |||
148 | test = np_escaped_string("ba\\nge\\r"); | ||
149 | ok( strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay"); | ||
150 | free(test); | ||
151 | |||
152 | test = np_escaped_string("\\rabbi\\t"); | ||
153 | ok( strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay"); | ||
154 | free(test); | ||
155 | |||
156 | test = np_escaped_string("and\\\\or"); | ||
157 | ok( strcmp(test, "and\\or") == 0, "and\\\\or okay"); | ||
158 | free(test); | ||
159 | |||
160 | test = np_escaped_string("bo\\gus"); | ||
161 | ok( strcmp(test, "bogus") == 0, "bo\\gus okay"); | ||
162 | free(test); | ||
163 | |||
164 | test = np_escaped_string("everything"); | ||
165 | ok( strcmp(test, "everything") == 0, "everything okay"); | ||
166 | free(test); | ||
167 | |||
139 | return exit_status(); | 168 | return exit_status(); |
140 | } | 169 | } |
141 | 170 | ||
diff --git a/plugins/utils.c b/plugins/utils.c index 685a638..a5245c6 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -727,3 +727,33 @@ char *fperfdata (const char *label, | |||
727 | 727 | ||
728 | return data; | 728 | return data; |
729 | } | 729 | } |
730 | |||
731 | char *np_escaped_string (const char *string) { | ||
732 | char *data; | ||
733 | int i, j=0; | ||
734 | data = strdup(string); | ||
735 | for (i=0; data[i]; i++) { | ||
736 | if (data[i] == '\\') { | ||
737 | switch(data[++i]) { | ||
738 | case 'n': | ||
739 | data[j++] = '\n'; | ||
740 | break; | ||
741 | case 'r': | ||
742 | data[j++] = '\r'; | ||
743 | break; | ||
744 | case 't': | ||
745 | data[j++] = '\t'; | ||
746 | break; | ||
747 | case '\\': | ||
748 | data[j++] = '\\'; | ||
749 | break; | ||
750 | default: | ||
751 | data[j++] = data[i]; | ||
752 | } | ||
753 | } else { | ||
754 | data[j++] = data[i]; | ||
755 | } | ||
756 | } | ||
757 | data[j] = '\0'; | ||
758 | return data; | ||
759 | } | ||
diff --git a/plugins/utils.h b/plugins/utils.h index 2345ed5..4bbe33d 100644 --- a/plugins/utils.h +++ b/plugins/utils.h | |||
@@ -132,6 +132,8 @@ char *fperfdata (const char *, | |||
132 | int, | 132 | int, |
133 | double); | 133 | double); |
134 | 134 | ||
135 | char *np_escaped_string (const char *); | ||
136 | |||
135 | /* The idea here is that, although not every plugin will use all of these, | 137 | /* The idea here is that, although not every plugin will use all of these, |
136 | most will or should. Therefore, for consistency, these very common | 138 | most will or should. Therefore, for consistency, these very common |
137 | options should have only these meanings throughout the overall suite */ | 139 | options should have only these meanings throughout the overall suite */ |