diff options
Diffstat (limited to 'plugins/check_ping.c')
-rw-r--r-- | plugins/check_ping.c | 109 |
1 files changed, 54 insertions, 55 deletions
diff --git a/plugins/check_ping.c b/plugins/check_ping.c index 9eff13c..26c0192 100644 --- a/plugins/check_ping.c +++ b/plugins/check_ping.c | |||
@@ -26,11 +26,13 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; | |||
26 | #include "popen.h" | 26 | #include "popen.h" |
27 | #include "utils.h" | 27 | #include "utils.h" |
28 | 28 | ||
29 | #define UNKNOWN_PACKET_LOSS 200 /* 200% */ | 29 | #define WARN_DUPLICATES "DUPLICATES FOUND! " |
30 | #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */ | 30 | #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */ |
31 | #define DEFAULT_MAX_PACKETS 5 /* default no. of ICMP ECHO packets */ | ||
32 | 31 | ||
33 | #define WARN_DUPLICATES "DUPLICATES FOUND! " | 32 | enum { |
33 | UNKNOWN_PACKET_LOSS = 200, /* 200% */ | ||
34 | DEFAULT_MAX_PACKETS = 5 /* default no. of ICMP ECHO packets */ | ||
35 | }; | ||
34 | 36 | ||
35 | int process_arguments (int, char **); | 37 | int process_arguments (int, char **); |
36 | int get_threshold (char *, float *, int *); | 38 | int get_threshold (char *, float *, int *); |
@@ -53,12 +55,16 @@ int verbose = FALSE; | |||
53 | float rta = UNKNOWN_TRIP_TIME; | 55 | float rta = UNKNOWN_TRIP_TIME; |
54 | int pl = UNKNOWN_PACKET_LOSS; | 56 | int pl = UNKNOWN_PACKET_LOSS; |
55 | 57 | ||
56 | char *warn_text = NULL; | 58 | char *warn_text = ""; |
59 | |||
60 | |||
57 | 61 | ||
62 | |||
63 | |||
58 | int | 64 | int |
59 | main (int argc, char **argv) | 65 | main (int argc, char **argv) |
60 | { | 66 | { |
61 | char *command_line = NULL; | 67 | char *cmd = NULL; |
62 | int result = STATE_UNKNOWN; | 68 | int result = STATE_UNKNOWN; |
63 | int this_result = STATE_UNKNOWN; | 69 | int this_result = STATE_UNKNOWN; |
64 | int i; | 70 | int i; |
@@ -84,32 +90,32 @@ main (int argc, char **argv) | |||
84 | #ifdef PING6_COMMAND | 90 | #ifdef PING6_COMMAND |
85 | # ifdef PING_PACKETS_FIRST | 91 | # ifdef PING_PACKETS_FIRST |
86 | if (is_inet6_addr(addresses[i]) && address_family != AF_INET) | 92 | if (is_inet6_addr(addresses[i]) && address_family != AF_INET) |
87 | asprintf (&command_line, PING6_COMMAND, max_packets, addresses[i]); | 93 | asprintf (&cmd, PING6_COMMAND, max_packets, addresses[i]); |
88 | else | 94 | else |
89 | asprintf (&command_line, PING_COMMAND, max_packets, addresses[i]); | 95 | asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]); |
90 | # else | 96 | # else |
91 | if (is_inet6_addr(addresses[i]) && address_family != AF_INET) | 97 | if (is_inet6_addr(addresses[i]) && address_family != AF_INET) |
92 | asprintf (&command_line, PING6_COMMAND, addresses[i], max_packets); | 98 | asprintf (&cmd, PING6_COMMAND, addresses[i], max_packets); |
93 | else | 99 | else |
94 | asprintf (&command_line, PING_COMMAND, addresses[i], max_packets); | 100 | asprintf (&cmd, PING_COMMAND, addresses[i], max_packets); |
95 | # endif | 101 | # endif |
96 | #else /* USE_IPV6 */ | 102 | #else /* USE_IPV6 */ |
97 | # ifdef PING_PACKETS_FIRST | 103 | # ifdef PING_PACKETS_FIRST |
98 | asprintf (&command_line, PING_COMMAND, max_packets, addresses[i]); | 104 | asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]); |
99 | # else | 105 | # else |
100 | asprintf (&command_line, PING_COMMAND, addresses[i], max_packets); | 106 | asprintf (&cmd, PING_COMMAND, addresses[i], max_packets); |
101 | # endif | 107 | # endif |
102 | #endif /* USE_IPV6 */ | 108 | #endif /* USE_IPV6 */ |
103 | 109 | ||
104 | if (verbose) | 110 | if (verbose) |
105 | printf ("%s ==> ", command_line); | 111 | printf ("%s ==> ", cmd); |
106 | 112 | ||
107 | /* run the command */ | 113 | /* run the command */ |
108 | this_result = run_ping (command_line, addresses[i]); | 114 | this_result = run_ping (cmd, addresses[i]); |
109 | 115 | ||
110 | if (pl == UNKNOWN_PACKET_LOSS || rta == UNKNOWN_TRIP_TIME) { | 116 | if (pl == UNKNOWN_PACKET_LOSS || rta == UNKNOWN_TRIP_TIME) { |
111 | printf ("%s\n", command_line); | 117 | printf ("%s\n", cmd); |
112 | terminate (STATE_UNKNOWN, | 118 | die (STATE_UNKNOWN, |
113 | _("Error: Could not interpret output from ping command\n")); | 119 | _("Error: Could not interpret output from ping command\n")); |
114 | } | 120 | } |
115 | 121 | ||
@@ -121,7 +127,7 @@ main (int argc, char **argv) | |||
121 | this_result = max_state (STATE_OK, this_result); | 127 | this_result = max_state (STATE_OK, this_result); |
122 | 128 | ||
123 | if (n_addresses > 1 && this_result != STATE_UNKNOWN) | 129 | if (n_addresses > 1 && this_result != STATE_UNKNOWN) |
124 | terminate (STATE_OK, "%s is alive\n", addresses[i]); | 130 | die (STATE_OK, "%s is alive\n", addresses[i]); |
125 | 131 | ||
126 | if (display_html == TRUE) | 132 | if (display_html == TRUE) |
127 | printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]); | 133 | printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]); |
@@ -144,8 +150,12 @@ main (int argc, char **argv) | |||
144 | 150 | ||
145 | return result; | 151 | return result; |
146 | } | 152 | } |
147 | |||
148 | 153 | ||
154 | |||
155 | |||
156 | |||
157 | |||
158 | |||
149 | /* process command-line arguments */ | 159 | /* process command-line arguments */ |
150 | int | 160 | int |
151 | process_arguments (int argc, char **argv) | 161 | process_arguments (int argc, char **argv) |
@@ -213,7 +223,7 @@ process_arguments (int argc, char **argv) | |||
213 | max_addr *= 2; | 223 | max_addr *= 2; |
214 | addresses = realloc (addresses, max_addr); | 224 | addresses = realloc (addresses, max_addr); |
215 | if (addresses == NULL) | 225 | if (addresses == NULL) |
216 | terminate (STATE_UNKNOWN, _("Could not realloc() addresses\n")); | 226 | die (STATE_UNKNOWN, _("Could not realloc() addresses\n")); |
217 | } | 227 | } |
218 | addresses[n_addresses-1] = ptr; | 228 | addresses[n_addresses-1] = ptr; |
219 | if ((ptr = index (ptr, ','))) { | 229 | if ((ptr = index (ptr, ','))) { |
@@ -307,7 +317,7 @@ process_arguments (int argc, char **argv) | |||
307 | if (max_packets == -1) { | 317 | if (max_packets == -1) { |
308 | if (is_intnonneg (argv[c])) { | 318 | if (is_intnonneg (argv[c])) { |
309 | max_packets = atoi (argv[c++]); | 319 | max_packets = atoi (argv[c++]); |
310 | } else { | 320 | } else { |
311 | printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]); | 321 | printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]); |
312 | return ERROR; | 322 | return ERROR; |
313 | } | 323 | } |
@@ -375,40 +385,33 @@ validate_arguments () | |||
375 | 385 | ||
376 | return OK; | 386 | return OK; |
377 | } | 387 | } |
378 | |||
379 | 388 | ||
389 | |||
390 | |||
391 | |||
392 | |||
393 | |||
380 | int | 394 | int |
381 | run_ping (char *command_line, char *server_address) | 395 | run_ping (char *cmd, char *server_address) |
382 | { | 396 | { |
383 | char buf[MAX_INPUT_BUFFER]; | 397 | char buf[MAX_INPUT_BUFFER]; |
384 | int result = STATE_UNKNOWN; | 398 | int result = STATE_UNKNOWN; |
385 | 399 | ||
386 | warn_text = malloc (1); | 400 | if ((child_process = spopen (cmd)) == NULL) { |
387 | if (warn_text == NULL) | ||
388 | terminate (STATE_UNKNOWN, _("unable to malloc warn_text")); | ||
389 | warn_text[0] = 0; | ||
390 | |||
391 | if ((child_process = spopen (command_line)) == NULL) { | ||
392 | printf (_("Cannot open pipe: ")); | 401 | printf (_("Cannot open pipe: ")); |
393 | terminate (STATE_UNKNOWN, command_line); | 402 | die (STATE_UNKNOWN, cmd); |
394 | } | 403 | } |
395 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | 404 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); |
396 | if (child_stderr == NULL) | 405 | if (child_stderr == NULL) |
397 | printf (_("Cannot open stderr for %s\n"), command_line); | 406 | printf (_("Cannot open stderr for %s\n"), cmd); |
398 | 407 | ||
399 | while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) { | 408 | while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) { |
400 | 409 | ||
401 | if (strstr (buf, _("(DUP!)"))) { | 410 | if (strstr (buf, _("(DUP!)"))) { |
402 | /* cannot use the max function since STATE_UNKNOWN is max | 411 | result = max_state (result, STATE_WARNING); |
403 | result = max (result, STATE_WARNING); */ | 412 | warn_text = strdup (WARN_DUPLICATES); |
404 | if( !(result == STATE_CRITICAL) ){ | 413 | if (!warn_text) |
405 | result = STATE_WARNING; | 414 | die (STATE_UNKNOWN, _("unable to realloc warn_text")); |
406 | } | ||
407 | |||
408 | warn_text = realloc (warn_text, strlen (WARN_DUPLICATES) + 1); | ||
409 | if (warn_text == NULL) | ||
410 | terminate (STATE_UNKNOWN, _("unable to realloc warn_text")); | ||
411 | strcpy (warn_text, WARN_DUPLICATES); | ||
412 | } | 415 | } |
413 | 416 | ||
414 | /* get the percent loss statistics */ | 417 | /* get the percent loss statistics */ |
@@ -434,39 +437,31 @@ run_ping (char *command_line, char *server_address) | |||
434 | if (pl == 100) | 437 | if (pl == 100) |
435 | rta = crta; | 438 | rta = crta; |
436 | 439 | ||
437 | |||
438 | /* check stderr */ | 440 | /* check stderr */ |
439 | while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) { | 441 | while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) { |
440 | if (strstr(buf,"Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP")) | 442 | if (strstr(buf,"Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP")) |
441 | continue; | 443 | continue; |
442 | 444 | ||
443 | if (strstr (buf, "Network is unreachable")) | 445 | if (strstr (buf, "Network is unreachable")) |
444 | terminate (STATE_CRITICAL, | 446 | die (STATE_CRITICAL, |
445 | _("PING CRITICAL - Network unreachable (%s)"), | 447 | _("PING CRITICAL - Network unreachable (%s)"), |
446 | server_address); | 448 | server_address); |
447 | else if (strstr (buf, "Destination Host Unreachable")) | 449 | else if (strstr (buf, "Destination Host Unreachable")) |
448 | terminate (STATE_CRITICAL, | 450 | die (STATE_CRITICAL, |
449 | _("PING CRITICAL - Host Unreachable (%s)"), | 451 | _("PING CRITICAL - Host Unreachable (%s)"), |
450 | server_address); | 452 | server_address); |
451 | else if (strstr (buf, "unknown host" )) | 453 | else if (strstr (buf, "unknown host" )) |
452 | terminate (STATE_CRITICAL, | 454 | die (STATE_CRITICAL, |
453 | _("PING CRITICAL - Host not found (%s)"), | 455 | _("PING CRITICAL - Host not found (%s)"), |
454 | server_address); | 456 | server_address); |
455 | 457 | ||
456 | warn_text = | ||
457 | realloc (warn_text, strlen (warn_text) + strlen (buf) + 2); | ||
458 | if (warn_text == NULL) | ||
459 | terminate (STATE_UNKNOWN, _("unable to realloc warn_text")); | ||
460 | if (strlen (warn_text) == 0) | 458 | if (strlen (warn_text) == 0) |
461 | strcpy (warn_text, buf); | 459 | warn_text = strdup (buf); |
462 | else | 460 | else if (asprintf (&warn_text, "%s %s", warn_text, buf) == -1) |
463 | sprintf (warn_text, "%s %s", warn_text, buf); | 461 | die (STATE_UNKNOWN, _("unable to realloc warn_text")); |
464 | 462 | ||
465 | if (strstr (buf, "DUPLICATES FOUND")) { | 463 | if (strstr (buf, "DUPLICATES FOUND")) |
466 | if( !(result == STATE_CRITICAL) ){ | 464 | result = max_state (result, STATE_WARNING); |
467 | result = STATE_WARNING; | ||
468 | } | ||
469 | } | ||
470 | else | 465 | else |
471 | result = STATE_CRITICAL ; | 466 | result = STATE_CRITICAL ; |
472 | } | 467 | } |
@@ -481,6 +476,10 @@ run_ping (char *command_line, char *server_address) | |||
481 | } | 476 | } |
482 | 477 | ||
483 | 478 | ||
479 | |||
480 | |||
481 | |||
482 | |||
484 | void | 483 | void |
485 | print_usage (void) | 484 | print_usage (void) |
486 | { | 485 | { |