summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Weiss <holger@zedat.fu-berlin.de>2013-09-12 17:42:10 +0200
committerHolger Weiss <holger@zedat.fu-berlin.de>2013-09-12 17:42:10 +0200
commit662997251d4fc43f4155784f9e7df827f193305e (patch)
tree0a9ad5c5607598da0eb6b6d71fa8c47ca44186fb
parentca9ce71576ddf78cc54fe3b6f0428cdfea72e9df (diff)
downloadmonitoring-plugins-662997251d4fc43f4155784f9e7df827f193305e.tar.gz
Improve interface of np_expect_match() function
Replace the three boolean parameters of lib/utils_tcp.c's np_expect_match() function with a single "flags" parameter.
-rw-r--r--lib/tests/test_tcp.c16
-rw-r--r--lib/utils_tcp.c19
-rw-r--r--lib/utils_tcp.h6
-rw-r--r--plugins/check_tcp.c23
4 files changed, 32 insertions, 32 deletions
diff --git a/lib/tests/test_tcp.c b/lib/tests/test_tcp.c
index 6cf93946..8e9d43c8 100644
--- a/lib/tests/test_tcp.c
+++ b/lib/tests/test_tcp.c
@@ -33,21 +33,21 @@ main (int argc, char **argv)
33 server_expect[1] = strdup("bb"); 33 server_expect[1] = strdup("bb");
34 server_expect[2] = strdup("CC"); 34 server_expect[2] = strdup("CC");
35 35
36 ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == TRUE, 36 ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == TRUE,
37 "Test matching any string at the beginning (first expect string)"); 37 "Test matching any string at the beginning (first expect string)");
38 ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == TRUE, 38 ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == TRUE,
39 "Test matching any string at the beginning (second expect string)"); 39 "Test matching any string at the beginning (second expect string)");
40 ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == FALSE, 40 ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE,
41 "Test with strings not matching at the beginning"); 41 "Test with strings not matching at the beginning");
42 ok(np_expect_match("XX CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == FALSE, 42 ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE,
43 "Test matching any string"); 43 "Test matching any string");
44 ok(np_expect_match("XX", server_expect, server_expect_count, FALSE, FALSE, FALSE) == FALSE, 44 ok(np_expect_match("XX", server_expect, server_expect_count, 0) == FALSE,
45 "Test not matching any string"); 45 "Test not matching any string");
46 ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == TRUE, 46 ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == TRUE,
47 "Test matching all strings"); 47 "Test matching all strings");
48 ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == FALSE, 48 ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE,
49 "Test not matching all strings"); 49 "Test not matching all strings");
50 ok(np_expect_match("XX XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == FALSE, 50 ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE,
51 "Test not matching any string (testing all)"); 51 "Test not matching any string (testing all)");
52 52
53 53
diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c
index 8589ce67..cf67b116 100644
--- a/lib/utils_tcp.c
+++ b/lib/utils_tcp.c
@@ -30,26 +30,27 @@
30#include "utils_tcp.h" 30#include "utils_tcp.h"
31 31
32int 32int
33np_expect_match(char* status, char** server_expect, int expect_count, int all, int exact_match, int verbose) 33np_expect_match(char* status, char** server_expect, int expect_count, int flags)
34{ 34{
35 int match = 0; 35 int match = 0;
36 int i; 36 int i;
37 for (i = 0; i < expect_count; i++) { 37 for (i = 0; i < expect_count; i++) {
38 if (verbose) 38 if (flags & NP_MATCH_VERBOSE)
39 printf ("looking for [%s] %s [%s]\n", server_expect[i], 39 printf ("looking for [%s] %s [%s]\n", server_expect[i],
40 (exact_match) ? "in beginning of" : "anywhere in", 40 (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in",
41 status); 41 status);
42 42
43 if ((exact_match && !strncmp(status, server_expect[i], strlen(server_expect[i]))) || 43 if ((flags & NP_MATCH_EXACT &&
44 (! exact_match && strstr(status, server_expect[i]))) 44 !strncmp(status, server_expect[i], strlen(server_expect[i]))) ||
45 (!(flags & NP_MATCH_EXACT) && strstr(status, server_expect[i])))
45 { 46 {
46 if(verbose) puts("found it"); 47 if(flags & NP_MATCH_VERBOSE) puts("found it");
47 match += 1; 48 match += 1;
48 } else 49 } else
49 if(verbose) puts("couldn't find it"); 50 if(flags & NP_MATCH_VERBOSE) puts("couldn't find it");
50 } 51 }
51 if ((all == TRUE && match == expect_count) || 52 if ((flags & NP_MATCH_ALL && match == expect_count) ||
52 (! all && match >= 1)) { 53 (!(flags & NP_MATCH_ALL) && match >= 1)) {
53 return TRUE; 54 return TRUE;
54 } else 55 } else
55 return FALSE; 56 return FALSE;
diff --git a/lib/utils_tcp.h b/lib/utils_tcp.h
index b0eb8bee..34b771d6 100644
--- a/lib/utils_tcp.h
+++ b/lib/utils_tcp.h
@@ -1,4 +1,8 @@
1/* Header file for utils_tcp */ 1/* Header file for utils_tcp */
2 2
3#define NP_MATCH_ALL 0x1
4#define NP_MATCH_EXACT 0x2
5#define NP_MATCH_VERBOSE 0x4
6
3int np_expect_match(char* status, char** server_expect, int server_expect_count, 7int np_expect_match(char* status, char** server_expect, int server_expect_count,
4 int all, int exact_match, int verbose); 8 int flags);
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c
index 9e62638b..e8d7ec68 100644
--- a/plugins/check_tcp.c
+++ b/plugins/check_tcp.c
@@ -82,15 +82,14 @@ static int sd = 0;
82#define MAXBUF 1024 82#define MAXBUF 1024
83static char buffer[MAXBUF]; 83static char buffer[MAXBUF];
84static int expect_mismatch_state = STATE_WARNING; 84static int expect_mismatch_state = STATE_WARNING;
85static int match_flags = NP_MATCH_EXACT;
85 86
86#define FLAG_SSL 0x01 87#define FLAG_SSL 0x01
87#define FLAG_VERBOSE 0x02 88#define FLAG_VERBOSE 0x02
88#define FLAG_EXACT_MATCH 0x04 89#define FLAG_TIME_WARN 0x04
89#define FLAG_TIME_WARN 0x08 90#define FLAG_TIME_CRIT 0x08
90#define FLAG_TIME_CRIT 0x10 91#define FLAG_HIDE_OUTPUT 0x10
91#define FLAG_HIDE_OUTPUT 0x20 92static size_t flags;
92#define FLAG_MATCH_ALL 0x40
93static size_t flags = FLAG_EXACT_MATCH;
94 93
95int 94int
96main (int argc, char **argv) 95main (int argc, char **argv)
@@ -296,12 +295,7 @@ main (int argc, char **argv)
296 (int)len + 1, status); 295 (int)len + 1, status);
297 while(isspace(status[len])) status[len--] = '\0'; 296 while(isspace(status[len])) status[len--] = '\0';
298 297
299 match = np_expect_match(status, 298 match = np_expect_match(status, server_expect, server_expect_count, match_flags);
300 server_expect,
301 server_expect_count,
302 (flags & FLAG_MATCH_ALL ? TRUE : FALSE),
303 (flags & FLAG_EXACT_MATCH ? TRUE : FALSE),
304 (flags & FLAG_VERBOSE ? TRUE : FALSE));
305 } 299 }
306 300
307 if (server_quit != NULL) { 301 if (server_quit != NULL) {
@@ -450,6 +444,7 @@ process_arguments (int argc, char **argv)
450 exit (STATE_OK); 444 exit (STATE_OK);
451 case 'v': /* verbose mode */ 445 case 'v': /* verbose mode */
452 flags |= FLAG_VERBOSE; 446 flags |= FLAG_VERBOSE;
447 match_flags |= NP_MATCH_VERBOSE;
453 break; 448 break;
454 case '4': 449 case '4':
455 address_family = AF_INET; 450 address_family = AF_INET;
@@ -506,7 +501,7 @@ process_arguments (int argc, char **argv)
506 xasprintf(&server_send, "%s", optarg); 501 xasprintf(&server_send, "%s", optarg);
507 break; 502 break;
508 case 'e': /* expect string (may be repeated) */ 503 case 'e': /* expect string (may be repeated) */
509 flags &= ~FLAG_EXACT_MATCH; 504 match_flags &= ~NP_MATCH_EXACT;
510 if (server_expect_count == 0) 505 if (server_expect_count == 0)
511 server_expect = malloc (sizeof (char *) * (++server_expect_count)); 506 server_expect = malloc (sizeof (char *) * (++server_expect_count));
512 else 507 else
@@ -584,7 +579,7 @@ process_arguments (int argc, char **argv)
584#endif 579#endif
585 break; 580 break;
586 case 'A': 581 case 'A':
587 flags |= FLAG_MATCH_ALL; 582 match_flags |= NP_MATCH_ALL;
588 break; 583 break;
589 } 584 }
590 } 585 }