Code

reverting my changes from !=TRUE to == ERROR, that's not good ;-( sorry
[nagiosplug.git] / plugins / check_fping.c
index 9a2dd5570ba2b3b73f41fc5ec6ac775c6b341c05..7f05d4d53038ab297245a7eebc36bd409caf7285 100644 (file)
@@ -1,79 +1,82 @@
 /******************************************************************************
-*
-* CHECK_FPING.C
-*
-* Program: Fping plugin for Nagios
-* License: GPL
-* Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
-* $Id$
-*
-* Modifications:
-*
-* 08-24-1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
-*            Intial Coding
-* 09-11-1999 Karl DeBisschop (kdebiss@alum.mit.edu)
-*            Change to spopen
-*            Fix so that state unknown is returned by default
-*            (formerly would give state ok if no fping specified)
-*            Add server_name to output
-*            Reformat to 80-character standard screen
-* 11-18-1999 Karl DeBisschop (kdebiss@alum.mit.edu)
-*            set STATE_WARNING of stderr written or nonzero status returned
-*
-* Description:
-*
-* This plugin will use the /bin/fping command (form saint) to ping
-* the specified host for a fast check if the host is alive. Note that
-* it is necessary to set the suid flag on fping.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+ $Id$
 ******************************************************************************/
 
-#include "config.h"
+const char *progname = "check_fping";
+const char *revision = "$Revision$";
+const char *copyright = "2000-2004";
+const char *email = "nagiosplug-devel@lists.sourceforge.net";
+
 #include "common.h"
 #include "popen.h"
+#include "netutils.h"
 #include "utils.h"
 
-#define PROGNAME "check_fping"
-#define PACKET_COUNT 1
-#define PACKET_SIZE 56
-#define UNKNOWN_PACKET_LOSS 200        /* 200% */
-#define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
-
-#define PL 0
-#define RTA 1
+enum {
+       PACKET_COUNT = 1,
+       PACKET_SIZE = 56,
+       PL = 0,
+       RTA = 1
+};
 
 int textscan (char *buf);
 int process_arguments (int, char **);
 int get_threshold (char *arg, char *rv[2]);
-void print_usage (void);
 void print_help (void);
+void print_usage (void);
 
 char *server_name = NULL;
-int cpl = UNKNOWN_PACKET_LOSS;
-int wpl = UNKNOWN_PACKET_LOSS;
-double crta = UNKNOWN_TRIP_TIME;
-double wrta = UNKNOWN_TRIP_TIME;
 int packet_size = PACKET_SIZE;
 int packet_count = PACKET_COUNT;
 int verbose = FALSE;
+int cpl;
+int wpl;
+double crta;
+double wrta;
+int cpl_p = FALSE;
+int wpl_p = FALSE;
+int crta_p = FALSE;
+int wrta_p = FALSE;
 
 int
 main (int argc, char **argv)
 {
+/* normaly should be   int result = STATE_UNKNOWN; */
+
        int status = STATE_UNKNOWN;
        char *server = NULL;
        char *command_line = NULL;
        char *input_buffer = NULL;
        input_buffer = malloc (MAX_INPUT_BUFFER);
 
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+
        if (process_arguments (argc, argv) == ERROR)
-               usage ("Could not parse arguments\n");
+               usage4 (_("Could not parse arguments"));
 
        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);
@@ -81,13 +84,13 @@ main (int argc, char **argv)
        /* run the command */
        child_process = spopen (command_line);
        if (child_process == NULL) {
-               printf ("Unable to open pipe: %s\n", command_line);
+               printf (_("Could not open pipe: %s\n"), command_line);
                return STATE_UNKNOWN;
        }
 
        child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
        if (child_stderr == NULL) {
-               printf ("Could not open stderr for %s\n", command_line);
+               printf (_("Could not open stderr for %s\n"), command_line);
        }
 
        while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
@@ -117,7 +120,6 @@ main (int argc, char **argv)
 
 
 
-
 int
 textscan (char *buf)
 {
@@ -128,16 +130,16 @@ textscan (char *buf)
        int status = STATE_UNKNOWN;
 
        if (strstr (buf, "not found")) {
-               terminate (STATE_CRITICAL, "FPING unknown - %s not found\n", server_name);
+               die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
 
        }
        else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
-               terminate (STATE_CRITICAL, "FPING critical - %s is unreachable\n",
+               die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
                                                         "host");
 
        }
        else if (strstr (buf, "is down")) {
-               terminate (STATE_CRITICAL, "FPING critical - %s is down\n", server_name);
+               die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
 
        }
        else if (strstr (buf, "is alive")) {
@@ -153,18 +155,21 @@ textscan (char *buf)
                rtastr = 1 + index (rtastr, '/');
                loss = strtod (losstr, NULL);
                rta = strtod (rtastr, NULL);
-               if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
+               if (cpl_p == TRUE && loss > cpl)
                        status = STATE_CRITICAL;
-               else if (crta != UNKNOWN_TRIP_TIME && rta > crta)
+               else if (crta_p == TRUE  && rta > crta)
                        status = STATE_CRITICAL;
-               else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
+               else if (wpl_p == TRUE && loss > wpl)
                        status = STATE_WARNING;
-               else if (wrta != UNKNOWN_TRIP_TIME && rta > wrta)
+               else if (wrta_p == TRUE && rta > wrta)
                        status = STATE_WARNING;
                else
                        status = STATE_OK;
-               terminate (status, "FPING %s - %s (loss=%f%%, rta=%f ms)\n",
-                                                        state_text (status), server_name, loss, rta);
+               die (status,
+                     _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
+                                state_text (status), server_name, loss, rta,
+                    perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
+                    fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
 
        }
        else if(strstr (buf, "xmt/rcv/%loss") ) {
@@ -173,17 +178,18 @@ textscan (char *buf)
                losstr = 1 + strstr (losstr, "/");
                losstr = 1 + strstr (losstr, "/");
                loss = strtod (losstr, NULL);
-               if (loss == 100)
+               if (atoi(losstr) == 100)
                        status = STATE_CRITICAL;
-               else if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
+               else if (cpl_p == TRUE && loss > cpl)
                        status = STATE_CRITICAL;
-               else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
+               else if (wpl_p == TRUE && loss > wpl)
                        status = STATE_WARNING;
                else
                        status = STATE_OK;
-               
-               terminate (status, "FPING %s - %s (loss=%f%% )\n",
-                                                        state_text (status), server_name, loss );              
+               /* loss=%.0f%%;%d;%d;0;100 */
+               die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
+                    state_text (status), server_name, loss ,
+                    perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
        
        }
        else {
@@ -195,7 +201,6 @@ textscan (char *buf)
 
 
 
-
 /* process command-line arguments */
 int
 process_arguments (int argc, char **argv)
@@ -203,9 +208,8 @@ process_arguments (int argc, char **argv)
        int c;
        char *rv[2];
 
-#ifdef HAVE_GETOPT_H
-       int option_index = 0;
-       static struct option long_options[] = {
+       int option = 0;
+       static struct option longopts[] = {
                {"hostname", required_argument, 0, 'H'},
                {"critical", required_argument, 0, 'c'},
                {"warning", required_argument, 0, 'w'},
@@ -216,7 +220,6 @@ process_arguments (int argc, char **argv)
                {"help", no_argument, 0, 'h'},
                {0, 0, 0, 0}
        };
-#endif
 
        rv[PL] = NULL;
        rv[RTA] = NULL;
@@ -232,35 +235,28 @@ 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:", longopts, &option);
 
                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;
                        break;
                case 'H':                                                                       /* hostname */
                        if (is_host (optarg) == FALSE) {
-                               printf ("Invalid host name/address\n\n");
-                               print_usage ();
-                               exit (STATE_UNKNOWN);
+                               usage2 (_("Invalid hostname/address"), optarg);
                        }
                        server_name = strscpy (server_name, optarg);
                        break;
@@ -268,10 +264,12 @@ process_arguments (int argc, char **argv)
                        get_threshold (optarg, rv);
                        if (rv[RTA]) {
                                crta = strtod (rv[RTA], NULL);
+                               crta_p = TRUE;
                                rv[RTA] = NULL;
                        }
                        if (rv[PL]) {
                                cpl = atoi (rv[PL]);
+                               cpl_p = TRUE;
                                rv[PL] = NULL;
                        }
                        break;
@@ -279,10 +277,12 @@ process_arguments (int argc, char **argv)
                        get_threshold (optarg, rv);
                        if (rv[RTA]) {
                                wrta = strtod (rv[RTA], NULL);
+                               wrta_p = TRUE;
                                rv[RTA] = NULL;
                        }
                        if (rv[PL]) {
                                wpl = atoi (rv[PL]);
+                               wpl_p = TRUE;
                                rv[PL] = NULL;
                        }
                        break;
@@ -290,28 +290,26 @@ process_arguments (int argc, char **argv)
                        if (is_intpos (optarg))
                                packet_size = atoi (optarg);
                        else
-                               usage ("Packet size must be a positive integer");
+                               usage (_("Packet size must be a positive integer"));
                        break;
                case 'n':                                                                       /* number of packets */
                        if (is_intpos (optarg))
                                packet_count = atoi (optarg);
                        else
-                               usage ("Packet count must be a positive integer");
+                               usage (_("Packet count must be a positive integer"));
                        break;
                }
        }
 
 
        if (server_name == NULL)
-               usage ("Host name was not supplied\n\n");
+               usage (_("Hostname was not supplied\n\n"));
 
        return OK;
 }
 
 
 
-
-
 int
 get_threshold (char *arg, char *rv[2])
 {
@@ -325,13 +323,13 @@ get_threshold (char *arg, char *rv[2])
        if (arg2) {
                arg1[strcspn (arg1, ",:")] = 0;
                if (strstr (arg1, "%") && strstr (arg2, "%"))
-                       terminate (STATE_UNKNOWN,
-                                                                "%s: Only one threshold may be packet loss (%s)\n", PROGNAME,
+                       die (STATE_UNKNOWN,
+                                                                _("%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);
+                       die (STATE_UNKNOWN,
+                                                                _("%s: Only one threshold must be packet loss (%s)\n"),
+                                                                progname, arg);
        }
 
        if (arg2 && strstr (arg2, "%")) {
@@ -354,53 +352,52 @@ get_threshold (char *arg, char *rv[2])
 
 
 
-
-
 void
-print_usage (void)
+print_help (void)
 {
-       printf ("Usage: %s <host_address>\n", PROGNAME);
-}
 
+       print_revision (progname, revision);
 
+       printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
+       printf (COPYRIGHT, copyright, email);
 
+       printf (_("\
+This plugin will use the /bin/fping command to ping the specified host\n\
+for a fast check if the host is alive.\n\
+Note that it is necessary to set the suid flag on fping.\n\n"));
 
+       print_usage ();
 
-void
-print_help (void)
-{
-
-       print_revision (PROGNAME, "$Revision$");
+       printf (_(UT_HELP_VRSN));
+
+       printf (_("\
+ -H, --hostname=HOST\n\
+    Name or IP Address of host to ping (IP Address bypasses name lookup,\n\
+    reducing system load)\n\
+ -w, --warning=THRESHOLD\n\
+    warning threshold pair\n\
+ -c, --critical=THRESHOLD\n\
+    critical threshold pair\n\
+ -b, --bytes=INTEGER\n\
+    Size of ICMP packet (default: %d)\n\
+ -n, --number=INTEGER\n\
+    Number of ICMP packets to send (default: %d)\n"),
+               PACKET_SIZE, PACKET_COUNT);
+
+       printf (_(UT_VERBOSE));
+
+       printf (_("\n\
+THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
+time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
+percentage of packet loss to trigger an alarm state.\n"));
+
+       printf (_(UT_SUPPORT));
+}
 
-       printf
-               ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n\n"
-                "This plugin will use the /bin/fping command (from saint) to ping the\n"
-                "specified host for a fast check if the host is alive. Note that it is\n"
-                "necessary to set the suid flag on fping.\n\n");
 
-       print_usage ();
 
-       printf
-               ("\nOptions:\n"
-                "-H, --hostname=HOST\n"
-                "   Name or IP Address of host to ping (IP Address bypasses name lookup,\n"
-                "   reducing system load)\n"
-                "-w, --warning=THRESHOLD\n"
-                "   warning threshold pair\n"
-                "-c, --critical=THRESHOLD\n"
-                "   critical threshold pair\n"
-                "-b, --bytes=INTEGER\n"
-                "   Size of ICMP packet (default: %d)\n"
-                "-n, --number=INTEGER\n"
-                "   Number of ICMP packets to send (default: %d)\n"
-                "-v, --verbose\n"
-                "   Show details for command-line debugging (do not use with nagios server)\n"
-                "-h, --help\n"
-                "   Print this help screen\n"
-                "-V, --version\n"
-                "   Print version information\n"
-                "THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n"
-                "time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n"
-                "percentage of packet loss to trigger an alarm state.\n",
-                PACKET_SIZE, PACKET_COUNT);
+void
+print_usage (void)
+{
+       printf ("Usage: %s <host_address>\n", progname);
 }