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 /plugins/check_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 'plugins/check_tcp.c')
-rw-r--r-- | plugins/check_tcp.c | 27 |
1 files changed, 16 insertions, 11 deletions
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, |