Code

test GNU_SOURCE and include features.h if present to clear warning about asprintf...
[nagiosplug.git] / plugins / check_fping.c
index f6531a5416e31aee0c9d923863c4646566fee3a6..ac203f523d3a646cd8e2dd11f54f098857cd0426 100644 (file)
@@ -32,7 +32,7 @@
 #include "popen.h"
 #include "utils.h"
 
-#define PROGNAME "check_fping"
+const char *progname = "check_fping";
 #define PACKET_COUNT 1
 #define PACKET_SIZE 56
 #define UNKNOWN_PACKET_LOSS 200        /* 200% */
@@ -71,9 +71,8 @@ main (int argc, char **argv)
        server = strscpy (server, server_name);
 
        /* compose the command */
-       command_line = ssprintf
-               (command_line, "%s -b %d -c %d %s",
-                PATH_TO_FPING, packet_size, packet_count, server);
+       asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
+                 packet_size, packet_count, server);
 
        if (verbose)
                printf ("%s\n", command_line);
@@ -93,21 +92,22 @@ main (int argc, char **argv)
        while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
                if (verbose)
                        printf ("%s", input_buffer);
-               status = max (status, textscan (input_buffer));
+               status = max_state (status, textscan (input_buffer));
        }
 
        /* If we get anything on STDERR, at least set warning */
        while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
-               status = max (status, STATE_WARNING);
+               status = max_state (status, STATE_WARNING);
                if (verbose)
                        printf ("%s", input_buffer);
-               status = max (status, textscan (input_buffer));
+               status = max_state (status, textscan (input_buffer));
        }
        (void) fclose (child_stderr);
 
        /* close the pipe */
        if (spclose (child_process))
-               status = max (status, STATE_WARNING);
+               /* need to use max_state not max */
+               status = max_state (status, STATE_WARNING);
 
        printf ("FPING %s - %s\n", state_text (status), server_name);
 
@@ -165,9 +165,28 @@ textscan (char *buf)
                terminate (status, "FPING %s - %s (loss=%f%%, rta=%f ms)\n",
                                                         state_text (status), server_name, loss, rta);
 
+       }
+       else if(strstr (buf, "xmt/rcv/%loss") ) {
+               /* no min/max/avg if host was unreachable in fping v2.2.b1 */
+               losstr = strstr (buf, "=");
+               losstr = 1 + strstr (losstr, "/");
+               losstr = 1 + strstr (losstr, "/");
+               loss = strtod (losstr, NULL);
+               if (loss == 100)
+                       status = STATE_CRITICAL;
+               else if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
+                       status = STATE_CRITICAL;
+               else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
+                       status = STATE_WARNING;
+               else
+                       status = STATE_OK;
+               
+               terminate (status, "FPING %s - %s (loss=%f%% )\n",
+                                                        state_text (status), server_name, loss );              
+       
        }
        else {
-               status = max (status, STATE_WARNING);
+               status = max_state (status, STATE_WARNING);
        }
 
        return status;
@@ -183,7 +202,6 @@ process_arguments (int argc, char **argv)
        int c;
        char *rv[2];
 
-#ifdef HAVE_GETOPT_H
        int option_index = 0;
        static struct option long_options[] = {
                {"hostname", required_argument, 0, 'H'},
@@ -196,7 +214,6 @@ process_arguments (int argc, char **argv)
                {"help", no_argument, 0, 'h'},
                {0, 0, 0, 0}
        };
-#endif
 
        rv[PL] = NULL;
        rv[RTA] = NULL;
@@ -212,26 +229,21 @@ process_arguments (int argc, char **argv)
        }
 
        while (1) {
-#ifdef HAVE_GETOPT_H
-               c =
-                       getopt_long (argc, argv, "+hVvH:c:w:b:n:", long_options, &option_index);
-#else
-               c = getopt (argc, argv, "+hVvH:c:w:b:n:");
-#endif
+               c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", long_options, &option_index);
 
                if (c == -1 || c == EOF || c == 1)
                        break;
 
                switch (c) {
                case '?':                                                                       /* print short usage statement if args not parsable */
-                       printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
+                       printf ("%s: Unknown argument: %s\n\n", progname, optarg);
                        print_usage ();
                        exit (STATE_UNKNOWN);
                case 'h':                                                                       /* help */
                        print_help ();
                        exit (STATE_OK);
                case 'V':                                                                       /* version */
-                       print_revision (my_basename (argv[0]), "$Revision$");
+                       print_revision (progname, "$Revision$");
                        exit (STATE_OK);
                case 'v':                                                                       /* verbose mode */
                        verbose = TRUE;
@@ -306,12 +318,12 @@ get_threshold (char *arg, char *rv[2])
                arg1[strcspn (arg1, ",:")] = 0;
                if (strstr (arg1, "%") && strstr (arg2, "%"))
                        terminate (STATE_UNKNOWN,
-                                                                "%s: Only one threshold may be packet loss (%s)\n", PROGNAME,
+                                                                "%s: Only one threshold may be packet loss (%s)\n", progname,
                                                                 arg);
                if (!strstr (arg1, "%") && !strstr (arg2, "%"))
                        terminate (STATE_UNKNOWN,
                                                                 "%s: Only one threshold must be packet loss (%s)\n",
-                                                                PROGNAME, arg);
+                                                                progname, arg);
        }
 
        if (arg2 && strstr (arg2, "%")) {
@@ -339,7 +351,7 @@ get_threshold (char *arg, char *rv[2])
 void
 print_usage (void)
 {
-       printf ("Usage: %s <host_address>\n", PROGNAME);
+       printf ("Usage: %s <host_address>\n", progname);
 }
 
 
@@ -350,7 +362,7 @@ void
 print_help (void)
 {
 
-       print_revision (PROGNAME, "$Revision$");
+       print_revision (progname, "$Revision$");
 
        printf
                ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n\n"