X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fcheck_ping.c;h=6b566608961059c90c8206a8ee94fdcdca347cf9;hb=20d0154211fa46028383ed3a88b558731313c389;hp=6d8ef06d567d6d1a4dbc9a1e1c42a2d747657016;hpb=df1bd0c89ff63de64ee630e0025389651947020c;p=nagiosplug.git diff --git a/plugins/check_ping.c b/plugins/check_ping.c index 6d8ef06..6b56660 100644 --- a/plugins/check_ping.c +++ b/plugins/check_ping.c @@ -20,7 +20,7 @@ const char *progname = "check_ping"; const char *revision = "$Revision$"; -const char *copyright = "2000-2003"; +const char *copyright = "2000-2004"; const char *email = "nagiosplug-devel@lists.sourceforge.net"; #include "common.h" @@ -53,7 +53,7 @@ char **addresses = NULL; int n_addresses; int max_addr = 1; int max_packets = -1; -int verbose = FALSE; +int verbose = 0; float rta = UNKNOWN_TRIP_TIME; int pl = UNKNOWN_PACKET_LOSS; @@ -72,19 +72,19 @@ main (int argc, char **argv) int i; setlocale (LC_ALL, ""); + setlocale (LC_NUMERIC, "C"); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); addresses = malloc (sizeof(char*) * max_addr); addresses[0] = NULL; - if (process_arguments (argc, argv) != TRUE) - usage (_("check_ping: could not parse arguments\n")); + if (process_arguments (argc, argv) == ERROR) + usage4 (_("Could not parse arguments")); /* Set signal handling and alarm */ if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) { - printf (_("Cannot catch SIGALRM")); - return STATE_UNKNOWN; + usage4 (_("Cannot catch SIGALRM")); } /* handle timeouts gracefully */ @@ -112,8 +112,8 @@ main (int argc, char **argv) asprintf (&cmd, rawcmd, addresses[i], max_packets); #endif - if (verbose) - printf ("%s ==> ", cmd); + if (verbose >= 2) + printf ("CMD: %s\n", cmd); /* run the command */ this_result = run_ping (cmd, addresses[i]); @@ -146,7 +146,7 @@ main (int argc, char **argv) printf (""); printf ("\n"); - if (verbose) + if (verbose >= 2) printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl); result = max_state (result, this_result); @@ -195,9 +195,7 @@ process_arguments (int argc, char **argv) switch (c) { case '?': /* usage */ - printf (_("%s: Unknown argument: %s\n\n"), progname, optarg); - print_usage (); - exit (STATE_UNKNOWN); + usage2 (_("Unknown argument"), optarg); case 'h': /* help */ print_help (); exit (STATE_OK); @@ -210,7 +208,7 @@ process_arguments (int argc, char **argv) timeout_interval = atoi (optarg); break; case 'v': /* verbose mode */ - verbose = TRUE; + verbose++; break; case '4': /* IPv4 only */ address_family = AF_INET; @@ -406,7 +404,7 @@ run_ping (const char *cmd, const char *addr) int result = STATE_UNKNOWN; if ((child_process = spopen (cmd)) == NULL) - die (STATE_UNKNOWN, _("Cannot open pipe: %s"), cmd); + die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd); child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); if (child_stderr == NULL) @@ -414,15 +412,22 @@ run_ping (const char *cmd, const char *addr) while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) { + if (verbose >= 3) + printf("Output: %s", buf); + result = max_state (result, error_scan (buf, addr)); /* get the percent loss statistics */ if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 || - sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 || + sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss", &pl) == 1 || + sscanf(buf,"%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss", &pl) == 1 || + sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 || sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 || sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1 || sscanf(buf,"%*d packets transmitted, %*d received, %d%% packet loss, time", &pl)==1 || - sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl) == 1) + sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl) == 1 || + sscanf(buf,"%*d packets transmitted %*d received, +%*d errors, %d%% packet loss", &pl) == 1 + ) continue; /* get the round trip average */ @@ -433,6 +438,7 @@ run_ping (const char *cmd, const char *addr) sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 || sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta)==1 || sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta)==1 || + sscanf(buf,"round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 || sscanf(buf,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta)==1) continue; } @@ -470,13 +476,15 @@ error_scan (char buf[MAX_INPUT_BUFFER], const char *addr) die (STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)"), addr); else if (strstr (buf, "unknown host" )) die (STATE_CRITICAL, _("CRITICAL - Host not found (%s)"), addr); + else if (strstr (buf, "Time to live exceeded")) + die (STATE_CRITICAL, _("CRITICAL - Time to live exceeded (%s)"), addr); if (strstr (buf, "(DUP!)") || strstr (buf, "DUPLICATES FOUND")) { if (warn_text == NULL) warn_text = strdup (_(WARN_DUPLICATES)); else if (! strstr (warn_text, _(WARN_DUPLICATES)) && asprintf (&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1) - die (STATE_UNKNOWN, _("unable to realloc warn_text")); + die (STATE_UNKNOWN, _("Unable to realloc warn_text")); return (STATE_WARNING); } @@ -485,17 +493,6 @@ error_scan (char buf[MAX_INPUT_BUFFER], const char *addr) -void -print_usage (void) -{ - printf (\ -"Usage: %s -H -w ,%% -c ,%%\n\ - [-p packets] [-t timeout] [-L] [-4|-6]\n", progname); - printf (_(UT_HLP_VRS), progname, progname); -} - - - void print_help (void) { @@ -540,3 +537,10 @@ the contrib area of the downloads section at http://www.nagios.org\n\n")); printf (_(UT_SUPPORT)); } + +void +print_usage (void) +{ + printf ("Usage: %s -H -w ,%% -c ,%%\n\ + [-p packets] [-t timeout] [-L] [-4|-6]\n", progname); +}