diff options
author | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-09-12 19:37:20 (GMT) |
---|---|---|
committer | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-09-12 19:37:20 (GMT) |
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 cf67b11..497a170 100644 --- a/lib/utils_tcp.c +++ b/lib/utils_tcp.c | |||
@@ -3,7 +3,7 @@ | |||
3 | * Library for check_tcp | 3 | * Library for check_tcp |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 1999-2007 Nagios Plugins Development Team | 6 | * Copyright (c) 1999-2013 Nagios Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
@@ -29,29 +29,44 @@ | |||
29 | #include "common.h" | 29 | #include "common.h" |
30 | #include "utils_tcp.h" | 30 | #include "utils_tcp.h" |
31 | 31 | ||
32 | int | 32 | #define VERBOSE(message) \ |
33 | do { \ | ||
34 | if (flags & NP_MATCH_VERBOSE) \ | ||
35 | puts(message); \ | ||
36 | } while (0) | ||
37 | |||
38 | enum np_match_result | ||
33 | np_expect_match(char* status, char** server_expect, int expect_count, int flags) | 39 | np_expect_match(char* status, char** server_expect, int expect_count, int flags) |
34 | { | 40 | { |
35 | int match = 0; | 41 | int i, match = 0, partial = 0; |
36 | int i; | ||
37 | for (i = 0; i < expect_count; i++) { | 42 | for (i = 0; i < expect_count; i++) { |
38 | if (flags & NP_MATCH_VERBOSE) | 43 | if (flags & NP_MATCH_VERBOSE) |
39 | printf ("looking for [%s] %s [%s]\n", server_expect[i], | 44 | printf ("looking for [%s] %s [%s]\n", server_expect[i], |
40 | (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", | 45 | (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", |
41 | status); | 46 | status); |
42 | 47 | ||
43 | if ((flags & NP_MATCH_EXACT && | 48 | if (flags & NP_MATCH_EXACT) { |
44 | !strncmp(status, server_expect[i], strlen(server_expect[i]))) || | 49 | if (strncmp(status, server_expect[i], strlen(server_expect[i])) == 0) { |
45 | (!(flags & NP_MATCH_EXACT) && strstr(status, server_expect[i]))) | 50 | VERBOSE("found it"); |
46 | { | 51 | match++; |
47 | if(flags & NP_MATCH_VERBOSE) puts("found it"); | 52 | continue; |
48 | match += 1; | 53 | } else if (strncmp(status, server_expect[i], strlen(status)) == 0) { |
49 | } else | 54 | VERBOSE("found a substring"); |
50 | if(flags & NP_MATCH_VERBOSE) puts("couldn't find it"); | 55 | partial++; |
56 | continue; | ||
57 | } | ||
58 | } else if (strstr(status, server_expect[i]) != NULL) { | ||
59 | VERBOSE("found it"); | ||
60 | match++; | ||
61 | continue; | ||
62 | } | ||
63 | VERBOSE("couldn't find it"); | ||
51 | } | 64 | } |
52 | if ((flags & NP_MATCH_ALL && match == expect_count) || | 65 | if ((flags & NP_MATCH_ALL && match == expect_count) || |
53 | (!(flags & NP_MATCH_ALL) && match >= 1)) { | 66 | (!(flags & NP_MATCH_ALL) && match >= 1)) |
54 | return TRUE; | 67 | return NP_MATCH_SUCCESS; |
55 | } else | 68 | else if (partial > 0 || !(flags & NP_MATCH_EXACT)) |
56 | return FALSE; | 69 | return NP_MATCH_RETRY; |
70 | else | ||
71 | return NP_MATCH_FAILURE; | ||
57 | } | 72 | } |