diff options
author | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-09-12 21:37:20 +0200 |
---|---|---|
committer | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-09-12 21:37:20 +0200 |
commit | e8044713d41f5ef1d9ce814df4a079d8f92306b0 (patch) | |
tree | e4b6f3d068c850774b9cda16f7c5830b9fc15774 /lib/utils_tcp.c | |
parent | 662997251d4fc43f4155784f9e7df827f193305e (diff) | |
download | monitoring-plugins-e8044713d41f5ef1d9ce814df4a079d8f92306b0.tar.gz |
check_tcp: Properly deal will partial recv(3)s
The np_expect_match() function now returns one of three possible states
instead of just TRUE or FALSE:
- NP_MATCH_SUCCESS
- NP_MATCH_FAILURE
- NP_MATCH_RETRY
The NP_MATCH_RETRY state indicates that matching might succeed if
np_expect_match() is called with a longer input string. This allows
check_tcp to decide whether it makes sense to wait for additional data
from the server.
Diffstat (limited to 'lib/utils_tcp.c')
-rw-r--r-- | lib/utils_tcp.c | 47 |
1 files changed, 31 insertions, 16 deletions
diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c index cf67b116..497a1701 100644 --- a/lib/utils_tcp.c +++ b/lib/utils_tcp.c @@ -3,7 +3,7 @@ * Library for check_tcp * * License: GPL -* Copyright (c) 1999-2007 Nagios Plugins Development Team +* Copyright (c) 1999-2013 Nagios Plugins Development Team * * Description: * @@ -29,29 +29,44 @@ #include "common.h" #include "utils_tcp.h" -int +#define VERBOSE(message) \ + do { \ + if (flags & NP_MATCH_VERBOSE) \ + puts(message); \ + } while (0) + +enum np_match_result np_expect_match(char* status, char** server_expect, int expect_count, int flags) { - int match = 0; - int i; + int i, match = 0, partial = 0; for (i = 0; i < expect_count; i++) { if (flags & NP_MATCH_VERBOSE) printf ("looking for [%s] %s [%s]\n", server_expect[i], (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", status); - if ((flags & NP_MATCH_EXACT && - !strncmp(status, server_expect[i], strlen(server_expect[i]))) || - (!(flags & NP_MATCH_EXACT) && strstr(status, server_expect[i]))) - { - if(flags & NP_MATCH_VERBOSE) puts("found it"); - match += 1; - } else - if(flags & NP_MATCH_VERBOSE) puts("couldn't find it"); + if (flags & NP_MATCH_EXACT) { + if (strncmp(status, server_expect[i], strlen(server_expect[i])) == 0) { + VERBOSE("found it"); + match++; + continue; + } else if (strncmp(status, server_expect[i], strlen(status)) == 0) { + VERBOSE("found a substring"); + partial++; + continue; + } + } else if (strstr(status, server_expect[i]) != NULL) { + VERBOSE("found it"); + match++; + continue; + } + VERBOSE("couldn't find it"); } if ((flags & NP_MATCH_ALL && match == expect_count) || - (!(flags & NP_MATCH_ALL) && match >= 1)) { - return TRUE; - } else - return FALSE; + (!(flags & NP_MATCH_ALL) && match >= 1)) + return NP_MATCH_SUCCESS; + else if (partial > 0 || !(flags & NP_MATCH_EXACT)) + return NP_MATCH_RETRY; + else + return NP_MATCH_FAILURE; } |