summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/tests/test_tcp.c20
-rw-r--r--lib/utils_tcp.c47
-rw-r--r--lib/utils_tcp.h18
-rw-r--r--plugins/check_tcp.c27
4 files changed, 74 insertions, 38 deletions
diff --git a/lib/tests/test_tcp.c b/lib/tests/test_tcp.c
index 8e9d43c..ae6dc1f 100644
--- a/lib/tests/test_tcp.c
+++ b/lib/tests/test_tcp.c
@@ -25,7 +25,7 @@ main (int argc, char **argv)
25{ 25{
26 char** server_expect; 26 char** server_expect;
27 int server_expect_count = 3; 27 int server_expect_count = 3;
28 plan_tests(8); 28 plan_tests(9);
29 29
30 server_expect = malloc(sizeof(char*) * server_expect_count); 30 server_expect = malloc(sizeof(char*) * server_expect_count);
31 31
@@ -33,21 +33,23 @@ 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, NP_MATCH_EXACT) == TRUE, 36 ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_SUCCESS,
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, NP_MATCH_EXACT) == TRUE, 38 ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_SUCCESS,
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, NP_MATCH_EXACT) == FALSE, 40 ok(np_expect_match("b", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_RETRY,
41 "Test matching any string at the beginning (substring match)");
42 ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_FAILURE,
41 "Test with strings not matching at the beginning"); 43 "Test with strings not matching at the beginning");
42 ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE, 44 ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_FAILURE,
43 "Test matching any string"); 45 "Test matching any string");
44 ok(np_expect_match("XX", server_expect, server_expect_count, 0) == FALSE, 46 ok(np_expect_match("XX", server_expect, server_expect_count, 0) == NP_MATCH_RETRY,
45 "Test not matching any string"); 47 "Test not matching any string");
46 ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == TRUE, 48 ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_SUCCESS,
47 "Test matching all strings"); 49 "Test matching all strings");
48 ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE, 50 ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_RETRY,
49 "Test not matching all strings"); 51 "Test not matching all strings");
50 ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE, 52 ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_RETRY,
51 "Test not matching any string (testing all)"); 53 "Test not matching any string (testing all)");
52 54
53 55
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
32int 32#define VERBOSE(message) \
33 do { \
34 if (flags & NP_MATCH_VERBOSE) \
35 puts(message); \
36 } while (0)
37
38enum np_match_result
33np_expect_match(char* status, char** server_expect, int expect_count, int flags) 39np_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}
diff --git a/lib/utils_tcp.h b/lib/utils_tcp.h
index 34b771d..0328a9c 100644
--- a/lib/utils_tcp.h
+++ b/lib/utils_tcp.h
@@ -4,5 +4,19 @@
4#define NP_MATCH_EXACT 0x2 4#define NP_MATCH_EXACT 0x2
5#define NP_MATCH_VERBOSE 0x4 5#define NP_MATCH_VERBOSE 0x4
6 6
7int np_expect_match(char* status, char** server_expect, int server_expect_count, 7/*
8 int flags); 8 * The NP_MATCH_RETRY state indicates that matching might succeed if
9 * np_expect_match() is called with a longer input string. This allows the
10 * caller to decide whether it makes sense to wait for additional data from the
11 * server.
12 */
13enum np_match_result {
14 NP_MATCH_FAILURE,
15 NP_MATCH_SUCCESS,
16 NP_MATCH_RETRY
17};
18
19enum np_match_result np_expect_match(char *status,
20 char **server_expect,
21 int server_expect_count,
22 int flags);
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c
index e8d7ec6..517b6b5 100644
--- a/plugins/check_tcp.c
+++ b/plugins/check_tcp.c
@@ -3,7 +3,7 @@
3* Nagios check_tcp plugin 3* Nagios check_tcp plugin
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 1999-2008 Nagios Plugins Development Team 6* Copyright (c) 1999-2013 Nagios Plugins Development Team
7* 7*
8* Description: 8* Description:
9* 9*
@@ -277,25 +277,30 @@ main (int argc, char **argv)
277 status = realloc(status, len + i + 1); 277 status = realloc(status, len + i + 1);
278 memcpy(&status[len], buffer, i); 278 memcpy(&status[len], buffer, i);
279 len += i; 279 len += i;
280 status[len] = '\0';
280 281
281 /* stop reading if user-forced */ 282 /* stop reading if user-forced */
282 if (maxbytes && len >= maxbytes) 283 if (maxbytes && len >= maxbytes)
283 break; 284 break;
285
286 if ((match = np_expect_match(status,
287 server_expect,
288 server_expect_count,
289 match_flags)) != NP_MATCH_RETRY)
290 break;
284 } 291 }
285 292
286 /* no data when expected, so return critical */ 293 /* no data when expected, so return critical */
287 if (len == 0) 294 if (len == 0)
288 die (STATE_CRITICAL, _("No data received from host\n")); 295 die (STATE_CRITICAL, _("No data received from host\n"));
289 296
290 /* force null-termination and strip whitespace from end of output */
291 status[len--] = '\0';
292 /* print raw output if we're debugging */ 297 /* print raw output if we're debugging */
293 if(flags & FLAG_VERBOSE) 298 if(flags & FLAG_VERBOSE)
294 printf("received %d bytes from host\n#-raw-recv-------#\n%s\n#-raw-recv-------#\n", 299 printf("received %d bytes from host\n#-raw-recv-------#\n%s\n#-raw-recv-------#\n",
295 (int)len + 1, status); 300 (int)len + 1, status);
296 while(isspace(status[len])) status[len--] = '\0'; 301 /* strip whitespace from end of output */
297 302 while(--len > 0 && isspace(status[len]))
298 match = np_expect_match(status, server_expect, server_expect_count, match_flags); 303 status[len] = '\0';
299 } 304 }
300 305
301 if (server_quit != NULL) { 306 if (server_quit != NULL) {
@@ -315,7 +320,7 @@ main (int argc, char **argv)
315 result = STATE_WARNING; 320 result = STATE_WARNING;
316 321
317 /* did we get the response we hoped? */ 322 /* did we get the response we hoped? */
318 if(match == FALSE && result != STATE_CRITICAL) 323 if(match != NP_MATCH_SUCCESS && result != STATE_CRITICAL)
319 result = expect_mismatch_state; 324 result = expect_mismatch_state;
320 325
321 /* reset the alarm */ 326 /* reset the alarm */
@@ -326,10 +331,10 @@ main (int argc, char **argv)
326 * the response we were looking for. if-else */ 331 * the response we were looking for. if-else */
327 printf("%s %s - ", SERVICE, state_text(result)); 332 printf("%s %s - ", SERVICE, state_text(result));
328 333
329 if(match == FALSE && len && !(flags & FLAG_HIDE_OUTPUT)) 334 if(match != NP_MATCH_SUCCESS && len && !(flags & FLAG_HIDE_OUTPUT))
330 printf("Unexpected response from host/socket: %s", status); 335 printf("Unexpected response from host/socket: %s", status);
331 else { 336 else {
332 if(match == FALSE) 337 if(match != NP_MATCH_SUCCESS)
333 printf("Unexpected response from host/socket on "); 338 printf("Unexpected response from host/socket on ");
334 else 339 else
335 printf("%.3f second response time on ", elapsed_time); 340 printf("%.3f second response time on ", elapsed_time);
@@ -339,13 +344,13 @@ main (int argc, char **argv)
339 printf("socket %s", server_address); 344 printf("socket %s", server_address);
340 } 345 }
341 346
342 if (match != FALSE && !(flags & FLAG_HIDE_OUTPUT) && len) 347 if (match == NP_MATCH_SUCCESS && !(flags & FLAG_HIDE_OUTPUT) && len)
343 printf (" [%s]", status); 348 printf (" [%s]", status);
344 349
345 /* perf-data doesn't apply when server doesn't talk properly, 350 /* perf-data doesn't apply when server doesn't talk properly,
346 * so print all zeroes on warn and crit. Use fperfdata since 351 * so print all zeroes on warn and crit. Use fperfdata since
347 * localisation settings can make different outputs */ 352 * localisation settings can make different outputs */
348 if(match == FALSE) 353 if(match != NP_MATCH_SUCCESS)
349 printf ("|%s", 354 printf ("|%s",
350 fperfdata ("time", elapsed_time, "s", 355 fperfdata ("time", elapsed_time, "s",
351 (flags & FLAG_TIME_WARN ? TRUE : FALSE), 0, 356 (flags & FLAG_TIME_WARN ? TRUE : FALSE), 0,