diff options
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | lib/Makefile.am | 4 | ||||
-rw-r--r-- | lib/utils_tcp.c | 60 | ||||
-rw-r--r-- | lib/utils_tcp.h | 4 | ||||
-rw-r--r-- | plugins/check_tcp.c | 47 |
5 files changed, 91 insertions, 26 deletions
@@ -10,6 +10,8 @@ This file documents the major additions and syntax changes between releases. | |||
10 | Make check_http output more consistent | 10 | Make check_http output more consistent |
11 | Fix possible check_http segfaults when following HTTP redirects | 11 | Fix possible check_http segfaults when following HTTP redirects |
12 | check_snmp don't warn anymore if something is printed on stderr | 12 | check_snmp don't warn anymore if something is printed on stderr |
13 | Fix check_tcp segfault when multiple expect strings are given | ||
14 | New option for check_tcp: -A/--all to test if all given expect strings match | ||
13 | 15 | ||
14 | 1.4.8 11th April 2007 | 16 | 1.4.8 11th April 2007 |
15 | Respects --without-world-permissions for setuid plugins | 17 | Respects --without-world-permissions for setuid plugins |
diff --git a/lib/Makefile.am b/lib/Makefile.am index 3909bb9..5d0f635 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am | |||
@@ -5,8 +5,8 @@ SUBDIRS = tests | |||
5 | noinst_LIBRARIES = libnagiosplug.a | 5 | noinst_LIBRARIES = libnagiosplug.a |
6 | 6 | ||
7 | 7 | ||
8 | libnagiosplug_a_SOURCES = utils_base.c utils_disk.c | 8 | libnagiosplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c |
9 | EXTRA_DIST = utils_base.h utils_disk.h | 9 | EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h |
10 | 10 | ||
11 | INCLUDES = -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins | 11 | INCLUDES = -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins |
12 | 12 | ||
diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c new file mode 100644 index 0000000..b707519 --- /dev/null +++ b/lib/utils_tcp.c | |||
@@ -0,0 +1,60 @@ | |||
1 | /**************************************************************************** | ||
2 | * Utils for check_tcp | ||
3 | * | ||
4 | * License: GPL | ||
5 | * Copyright (c) 1999-2007 nagios-plugins team | ||
6 | * | ||
7 | * Last Modified: $Date$ | ||
8 | * | ||
9 | * Description: | ||
10 | * | ||
11 | * This file contains utilities for check_tcp. These are tested by libtap | ||
12 | * | ||
13 | * License Information: | ||
14 | * | ||
15 | * This program is free software; you can redistribute it and/or modify | ||
16 | * it under the terms of the GNU General Public License as published by | ||
17 | * the Free Software Foundation; either version 2 of the License, or | ||
18 | * (at your option) any later version. | ||
19 | * | ||
20 | * This program is distributed in the hope that it will be useful, | ||
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
23 | * GNU General Public License for more details. | ||
24 | * | ||
25 | * You should have received a copy of the GNU General Public License | ||
26 | * along with this program; if not, write to the Free Software | ||
27 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
28 | * | ||
29 | * $Id$ | ||
30 | * | ||
31 | *****************************************************************************/ | ||
32 | |||
33 | #include "common.h" | ||
34 | #include "utils_tcp.h" | ||
35 | |||
36 | int | ||
37 | np_expect_match(char* status, char** server_expect, int expect_count, int all, int exact_match, int verbose) | ||
38 | { | ||
39 | int match = 0; | ||
40 | int i; | ||
41 | for (i = 0; i < expect_count; i++) { | ||
42 | if (verbose) | ||
43 | printf ("looking for [%s] %s [%s]\n", server_expect[i], | ||
44 | (exact_match) ? "in beginning of" : "anywhere in", | ||
45 | status); | ||
46 | |||
47 | if ((exact_match && !strncmp(status, server_expect[i], strlen(server_expect[i]))) || | ||
48 | (! exact_match && strstr(status, server_expect[i]))) | ||
49 | { | ||
50 | if(verbose) puts("found it"); | ||
51 | match += 1; | ||
52 | } else | ||
53 | if(verbose) puts("couldn't find it"); | ||
54 | } | ||
55 | if ((all == true && match == expect_count) || | ||
56 | (! all && match >= 1)) { | ||
57 | return true; | ||
58 | } else | ||
59 | return false; | ||
60 | } | ||
diff --git a/lib/utils_tcp.h b/lib/utils_tcp.h new file mode 100644 index 0000000..06506f3 --- /dev/null +++ b/lib/utils_tcp.h | |||
@@ -0,0 +1,4 @@ | |||
1 | /* Header file for utils_disk */ | ||
2 | |||
3 | int np_expect_match(char* status, char** server_expect, int server_expect_count, | ||
4 | int all, int exact_match, int verbose); | ||
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index 315c752..e502c0d 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c | |||
@@ -40,6 +40,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; | |||
40 | #include "common.h" | 40 | #include "common.h" |
41 | #include "netutils.h" | 41 | #include "netutils.h" |
42 | #include "utils.h" | 42 | #include "utils.h" |
43 | #include "utils_tcp.h" | ||
43 | 44 | ||
44 | #ifdef HAVE_SSL | 45 | #ifdef HAVE_SSL |
45 | static int check_cert = FALSE; | 46 | static int check_cert = FALSE; |
@@ -90,6 +91,7 @@ static int expect_mismatch_state = STATE_WARNING; | |||
90 | #define FLAG_TIME_WARN 0x08 | 91 | #define FLAG_TIME_WARN 0x08 |
91 | #define FLAG_TIME_CRIT 0x10 | 92 | #define FLAG_TIME_CRIT 0x10 |
92 | #define FLAG_HIDE_OUTPUT 0x20 | 93 | #define FLAG_HIDE_OUTPUT 0x20 |
94 | #define FLAG_MATCH_ALL 0x40 | ||
93 | static size_t flags = FLAG_EXACT_MATCH; | 95 | static size_t flags = FLAG_EXACT_MATCH; |
94 | 96 | ||
95 | int | 97 | int |
@@ -99,7 +101,8 @@ main (int argc, char **argv) | |||
99 | int i; | 101 | int i; |
100 | char *status = NULL; | 102 | char *status = NULL; |
101 | struct timeval tv; | 103 | struct timeval tv; |
102 | size_t len, match = -1; | 104 | size_t len; |
105 | int match = -1; | ||
103 | 106 | ||
104 | setlocale (LC_ALL, ""); | 107 | setlocale (LC_ALL, ""); |
105 | bindtextdomain (PACKAGE, LOCALEDIR); | 108 | bindtextdomain (PACKAGE, LOCALEDIR); |
@@ -298,22 +301,12 @@ main (int argc, char **argv) | |||
298 | (int)len + 1, status); | 301 | (int)len + 1, status); |
299 | while(isspace(status[len])) status[len--] = '\0'; | 302 | while(isspace(status[len])) status[len--] = '\0'; |
300 | 303 | ||
301 | for (i = 0; i < server_expect_count; i++) { | 304 | match = np_expect_match(status, |
302 | match = -2; /* tag it so we know if we tried and failed */ | 305 | server_expect, |
303 | if (flags & FLAG_VERBOSE) | 306 | server_expect_count, |
304 | printf ("looking for [%s] %s [%s]\n", server_expect[i], | 307 | (flags & FLAG_MATCH_ALL ? TRUE : FALSE), |
305 | (flags & FLAG_EXACT_MATCH) ? "in beginning of" : "anywhere in", | 308 | (flags & FLAG_EXACT_MATCH ? TRUE : FALSE), |
306 | status); | 309 | (flags & FLAG_VERBOSE ? TRUE : FALSE)); |
307 | |||
308 | /* match it. math first in short-circuit */ | ||
309 | if ((flags & FLAG_EXACT_MATCH && !strncmp(status, server_expect[i], strlen(server_expect[i]))) || | ||
310 | (!(flags & FLAG_EXACT_MATCH) && strstr(status, server_expect[i]))) | ||
311 | { | ||
312 | if(flags & FLAG_VERBOSE) puts("found it"); | ||
313 | match = i; | ||
314 | break; | ||
315 | } | ||
316 | } | ||
317 | } | 310 | } |
318 | 311 | ||
319 | if (server_quit != NULL) { | 312 | if (server_quit != NULL) { |
@@ -333,7 +326,7 @@ main (int argc, char **argv) | |||
333 | result = STATE_WARNING; | 326 | result = STATE_WARNING; |
334 | 327 | ||
335 | /* did we get the response we hoped? */ | 328 | /* did we get the response we hoped? */ |
336 | if(match == -2 && result != STATE_CRITICAL) | 329 | if(match == FALSE && result != STATE_CRITICAL) |
337 | result = expect_mismatch_state; | 330 | result = expect_mismatch_state; |
338 | 331 | ||
339 | /* reset the alarm */ | 332 | /* reset the alarm */ |
@@ -344,10 +337,10 @@ main (int argc, char **argv) | |||
344 | * the response we were looking for. if-else */ | 337 | * the response we were looking for. if-else */ |
345 | printf("%s %s - ", SERVICE, state_text(result)); | 338 | printf("%s %s - ", SERVICE, state_text(result)); |
346 | 339 | ||
347 | if(match == -2 && len && !(flags & FLAG_HIDE_OUTPUT)) | 340 | if(match == FALSE && len && !(flags & FLAG_HIDE_OUTPUT)) |
348 | printf("Unexpected response from host/socket: %s", status); | 341 | printf("Unexpected response from host/socket: %s", status); |
349 | else { | 342 | else { |
350 | if(match == -2) | 343 | if(match == FALSE) |
351 | printf("Unexpected response from host/socket on "); | 344 | printf("Unexpected response from host/socket on "); |
352 | else | 345 | else |
353 | printf("%.3f second response time on ", elapsed_time); | 346 | printf("%.3f second response time on ", elapsed_time); |
@@ -357,13 +350,13 @@ main (int argc, char **argv) | |||
357 | printf("socket %s", server_address); | 350 | printf("socket %s", server_address); |
358 | } | 351 | } |
359 | 352 | ||
360 | if (match != -2 && !(flags & FLAG_HIDE_OUTPUT) && len) | 353 | if (match != FALSE && !(flags & FLAG_HIDE_OUTPUT) && len) |
361 | printf (" [%s]", status); | 354 | printf (" [%s]", status); |
362 | 355 | ||
363 | /* perf-data doesn't apply when server doesn't talk properly, | 356 | /* perf-data doesn't apply when server doesn't talk properly, |
364 | * so print all zeroes on warn and crit. Use fperfdata since | 357 | * so print all zeroes on warn and crit. Use fperfdata since |
365 | * localisation settings can make different outputs */ | 358 | * localisation settings can make different outputs */ |
366 | if(match == -2) | 359 | if(match == FALSE) |
367 | printf ("|%s", | 360 | printf ("|%s", |
368 | fperfdata ("time", elapsed_time, "s", | 361 | fperfdata ("time", elapsed_time, "s", |
369 | (flags & FLAG_TIME_WARN ? TRUE : FALSE), 0, | 362 | (flags & FLAG_TIME_WARN ? TRUE : FALSE), 0, |
@@ -404,6 +397,7 @@ process_arguments (int argc, char **argv) | |||
404 | {"protocol", required_argument, 0, 'P'}, | 397 | {"protocol", required_argument, 0, 'P'}, |
405 | {"port", required_argument, 0, 'p'}, | 398 | {"port", required_argument, 0, 'p'}, |
406 | {"escape", required_argument, 0, 'E'}, | 399 | {"escape", required_argument, 0, 'E'}, |
400 | {"all", required_argument, 0, 'A'}, | ||
407 | {"send", required_argument, 0, 's'}, | 401 | {"send", required_argument, 0, 's'}, |
408 | {"expect", required_argument, 0, 'e'}, | 402 | {"expect", required_argument, 0, 'e'}, |
409 | {"maxbytes", required_argument, 0, 'm'}, | 403 | {"maxbytes", required_argument, 0, 'm'}, |
@@ -445,7 +439,7 @@ process_arguments (int argc, char **argv) | |||
445 | } | 439 | } |
446 | 440 | ||
447 | while (1) { | 441 | while (1) { |
448 | c = getopt_long (argc, argv, "+hVv46EH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", | 442 | c = getopt_long (argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", |
449 | longopts, &option); | 443 | longopts, &option); |
450 | 444 | ||
451 | if (c == -1 || c == EOF || c == 1) | 445 | if (c == -1 || c == EOF || c == 1) |
@@ -581,6 +575,9 @@ process_arguments (int argc, char **argv) | |||
581 | die (STATE_UNKNOWN, _("Invalid option - SSL is not available")); | 575 | die (STATE_UNKNOWN, _("Invalid option - SSL is not available")); |
582 | #endif | 576 | #endif |
583 | break; | 577 | break; |
578 | case 'A': | ||
579 | flags |= FLAG_MATCH_ALL; | ||
580 | break; | ||
584 | } | 581 | } |
585 | } | 582 | } |
586 | 583 | ||
@@ -619,9 +616,11 @@ print_help (void) | |||
619 | printf (" %s\n", _("String to send to the server")); | 616 | printf (" %s\n", _("String to send to the server")); |
620 | printf (" %s\n", "-e, --expect=STRING"); | 617 | printf (" %s\n", "-e, --expect=STRING"); |
621 | printf (" %s %s\n", _("String to expect in server response"), _("(may be repeated)")); | 618 | printf (" %s %s\n", _("String to expect in server response"), _("(may be repeated)")); |
619 | printf (" %s\n", "-A, --all"); | ||
620 | printf (" %s\n", _("All expect strings need to occur in server response. Default is any")); | ||
622 | printf (" %s\n", "-q, --quit=STRING"); | 621 | printf (" %s\n", "-q, --quit=STRING"); |
623 | printf (" %s\n", _("String to send server to initiate a clean close of the connection")); | 622 | printf (" %s\n", _("String to send server to initiate a clean close of the connection")); |
624 | printf (" %s\n", "-r, --refuse=ok|warn|crit"); | 623 | printf (" %s\n", "-r, --refuse=ok|warn|crit"); |
625 | printf (" %s\n", _("Accept tcp refusals with states ok, warn, crit (default: crit)")); | 624 | printf (" %s\n", _("Accept tcp refusals with states ok, warn, crit (default: crit)")); |
626 | printf (" %s\n", "-M, --mismatch=ok|warn|crit"); | 625 | printf (" %s\n", "-M, --mismatch=ok|warn|crit"); |
627 | printf (" %s\n", _("Accept expected string mismatches with states ok, warn, crit (default: warn)")); | 626 | printf (" %s\n", _("Accept expected string mismatches with states ok, warn, crit (default: warn)")); |