Code

standardize localization string
[nagiosplug.git] / plugins / check_ping.c
index fd73b8cef8eb8f55d7f2e9506825d6cf7291a16a..260e1b651a02921df0225d1147c7d9d26d3f6f25 100644 (file)
@@ -14,6 +14,8 @@
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
+ $Id$
 ******************************************************************************/
 
 const char *progname = "check_ping";
@@ -37,7 +39,8 @@ enum {
 int process_arguments (int, char **);
 int get_threshold (char *, float *, int *);
 int validate_arguments (void);
-int run_ping (char *, char *);
+int run_ping (const char *cmd, const char *addr);
+int error_scan (char buf[MAX_INPUT_BUFFER], const char *addr);
 void print_usage (void);
 void print_help (void);
 
@@ -59,20 +62,24 @@ char *warn_text;
 
 
 
-
-\f
 int
 main (int argc, char **argv)
 {
        char *cmd = NULL;
+       char *rawcmd = NULL;
        int result = STATE_UNKNOWN;
        int this_result = STATE_UNKNOWN;
        int i;
 
-       addresses = malloc ((size_t)max_addr);
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+
+       addresses = malloc (sizeof(char*) * max_addr);
+       addresses[0] = NULL;
 
        if (process_arguments (argc, argv) == ERROR)
-               usage (_("Could not parse arguments"));
+               usage (_("check_ping: could not parse arguments\n"));
 
        /* Set signal handling and alarm */
        if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
@@ -84,27 +91,26 @@ main (int argc, char **argv)
        alarm (timeout_interval);
 
        for (i = 0 ; i < n_addresses ; i++) {
+               
+#ifdef PING6_COMMAND
+               if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
+                       rawcmd = strdup(PING6_COMMAND);
+               else
+                       rawcmd = strdup(PING_COMMAND);
+#else
+               rawcmd = strdup(PING_COMMAND);
+#endif
 
                /* does the host address of number of packets argument come first? */
-#ifdef PING6_COMMAND
-# ifdef PING_PACKETS_FIRST
-       if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
-               asprintf (&cmd, PING6_COMMAND, max_packets, addresses[i]);
-       else
-               asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]);
+#ifdef PING_PACKETS_FIRST
+# ifdef PING_HAS_TIMEOUT
+               asprintf (&cmd, rawcmd, timeout_interval, max_packets, addresses[i]);
 # else
-       if (is_inet6_addr(addresses[i]) && address_family != AF_INET) 
-               asprintf (&cmd, PING6_COMMAND, addresses[i], max_packets);
-       else
-               asprintf (&cmd, PING_COMMAND, addresses[i], max_packets);
+               asprintf (&cmd, rawcmd, max_packets, addresses[i]);
 # endif
-#else /* USE_IPV6 */
-# ifdef PING_PACKETS_FIRST
-               asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]);
-# else
-               asprintf (&cmd, PING_COMMAND, addresses[i], max_packets);
-# endif
-#endif /* USE_IPV6 */
+#else
+               asprintf (&cmd, rawcmd, addresses[i], max_packets);
+#endif
 
                if (verbose)
                        printf ("%s ==> ", cmd);
@@ -144,7 +150,8 @@ main (int argc, char **argv)
                        printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
 
                result = max_state (result, this_result);
-
+               free (rawcmd);
+               free (cmd);
        }
 
        return result;
@@ -152,9 +159,6 @@ main (int argc, char **argv)
 
 
 
-
-
-\f
 /* process command-line arguments */
 int
 process_arguments (int argc, char **argv)
@@ -162,8 +166,8 @@ process_arguments (int argc, char **argv)
        int c = 1;
        char *ptr;
 
-       int option_index = 0;
-       static struct option long_options[] = {
+       int option = 0;
+       static struct option longopts[] = {
                STD_LONG_OPTS,
                {"packets", required_argument, 0, 'p'},
                {"nohtml", no_argument, 0, 'n'},
@@ -184,15 +188,16 @@ process_arguments (int argc, char **argv)
        }
 
        while (1) {
-               c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", long_options, &option_index);
+               c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
 
                if (c == -1 || c == EOF)
                        break;
 
                switch (c) {
                case '?':       /* usage */
-                       usage3 (_("Unknown argument"), optopt);
-                       break;
+                       printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
+                       print_usage ();
+                       exit (STATE_UNKNOWN);
                case 'h':       /* help */
                        print_help ();
                        exit (STATE_OK);
@@ -223,7 +228,7 @@ process_arguments (int argc, char **argv)
                                n_addresses++;
                                if (n_addresses > max_addr) {
                                        max_addr *= 2;
-                                       addresses = realloc (addresses, (size_t)max_addr);
+                                       addresses = realloc (addresses, sizeof(char*) * max_addr);
                                        if (addresses == NULL)
                                                die (STATE_UNKNOWN, _("Could not realloc() addresses\n"));
                                }
@@ -263,10 +268,10 @@ process_arguments (int argc, char **argv)
 
        if (addresses[0] == NULL) {
                if (is_host (argv[c]) == FALSE) {
-                       printf (_("Invalid host name/address: %s\n\n"), argv[c]);
-                       return ERROR;
+                       usage2 (_("Invalid hostname/address"), argv[c]);
                } else {
                        addresses[0] = argv[c++];
+                       n_addresses++;
                        if (c == argc)
                                return validate_arguments ();
                }
@@ -328,6 +333,8 @@ process_arguments (int argc, char **argv)
        return validate_arguments ();
 }
 
+
+
 int
 get_threshold (char *arg, float *trta, int *tpl)
 {
@@ -342,6 +349,8 @@ get_threshold (char *arg, float *trta, int *tpl)
        return STATE_UNKNOWN;
 }
 
+
+
 int
 validate_arguments ()
 {
@@ -382,7 +391,7 @@ validate_arguments ()
 
        for (i=0; i<n_addresses; i++) {
                if (is_host(addresses[i]) == FALSE)
-                       usage2 (_("Invalid host name/address"), addresses[i]);
+                       usage2 (_("Invalid hostname/address"), addresses[i]);
        }
 
        return OK;
@@ -390,11 +399,8 @@ validate_arguments ()
 
 
 
-
-
-\f
 int
-run_ping (char *cmd, char *server_address)
+run_ping (const char *cmd, const char *addr)
 {
        char buf[MAX_INPUT_BUFFER];
        int result = STATE_UNKNOWN;
@@ -408,18 +414,15 @@ run_ping (char *cmd, char *server_address)
 
        while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
 
-               if (strstr (buf, _("(DUP!)"))) {
-                       result = max_state (result, STATE_WARNING);
-                       warn_text = strdup (WARN_DUPLICATES);
-                       if (!warn_text)
-                               die (STATE_UNKNOWN, _("unable to realloc warn_text"));
-               }
+               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%% loss, time",&pl)==1 ||
-                        sscanf(buf,"%*d packets transmitted, %*d 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)
                        continue;
 
                /* get the round trip average */
@@ -438,34 +441,11 @@ run_ping (char *cmd, char *server_address)
        if (pl == 100)
                rta = crta;
 
-       /* check stderr */
-       while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) {
-               if (strstr(buf,"Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP"))
-                               continue;
-
-               if (strstr (buf, "Network is unreachable"))
-                       die (STATE_CRITICAL,
-                                  _("PING CRITICAL - Network unreachable (%s)"),
-                                  server_address);
-               else if (strstr (buf, "Destination Host Unreachable"))
-                       die (STATE_CRITICAL,
-                                  _("PING CRITICAL - Host Unreachable (%s)"),
-                                  server_address);
-               else if (strstr (buf, "unknown host" ))
-                       die (STATE_CRITICAL,
-                                  _("PING CRITICAL - Host not found (%s)"),
-                                  server_address);
-
-               if (warn_text == NULL)
-                       warn_text = strdup (buf);
-               else if (asprintf (&warn_text, "%s %s", warn_text, buf) == -1)
-                       die (STATE_UNKNOWN, _("unable to realloc warn_text"));
+       /* check stderr, setting at least WARNING if there is output here */
+       while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
+               if (! strstr(buf,"WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP"))
+                       result = max_state (STATE_WARNING, error_scan (buf, addr));
 
-               if (strstr (buf, "DUPLICATES FOUND"))
-                       result = max_state (result, STATE_WARNING);
-               else
-                       result = STATE_CRITICAL ;
-       }
        (void) fclose (child_stderr);
 
 
@@ -481,9 +461,30 @@ run_ping (char *cmd, char *server_address)
 
 
 
+int
+error_scan (char buf[MAX_INPUT_BUFFER], const char *addr)
+{
+       if (strstr (buf, "Network is unreachable"))
+               die (STATE_CRITICAL, _("CRITICAL - Network unreachable (%s)"), addr);
+       else if (strstr (buf, "Destination Host Unreachable"))
+               die (STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)"), addr);
+       else if (strstr (buf, "unknown host" ))
+               die (STATE_CRITICAL, _("CRITICAL - Host not found (%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"));
+               return (STATE_WARNING);
+       }
+
+       return (STATE_OK);
+}
+
 
 
-\f
 void
 print_usage (void)
 {
@@ -493,13 +494,15 @@ print_usage (void)
        printf (_(UT_HLP_VRS), progname, progname);
 }
 
+
+
 void
 print_help (void)
 {
        print_revision (progname, revision);
 
-       printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>"));
-       printf (_(COPYRIGHT), copyright, email);
+       printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>");
+       printf (COPYRIGHT, copyright, email);
 
        printf (_("Use ping to check connection statistics for a remote host.\n\n"));