Back to index

nagios-plugins  1.4.16
check_fping.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios check_fping plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2000-2007 Nagios Plugins Development Team
00007 * 
00008 * Description:
00009 * 
00010 * This file contains the check_disk plugin
00011 * 
00012 * This plugin will use the fping command to ping the specified host for a
00013 * fast check
00014 * 
00015 * 
00016 * This program is free software: you can redistribute it and/or modify
00017 * it under the terms of the GNU General Public License as published by
00018 * the Free Software Foundation, either version 3 of the License, or
00019 * (at your option) any later version.
00020 * 
00021 * This program is distributed in the hope that it will be useful,
00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 * GNU General Public License for more details.
00025 * 
00026 * You should have received a copy of the GNU General Public License
00027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00028 * 
00029 * 
00030 *****************************************************************************/
00031 
00032 const char *progname = "check_fping";
00033 const char *copyright = "2000-2007";
00034 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00035 
00036 #include "common.h"
00037 #include "popen.h"
00038 #include "netutils.h"
00039 #include "utils.h"
00040 
00041 enum {
00042   PACKET_COUNT = 1,
00043   PACKET_SIZE = 56,
00044   PL = 0,
00045   RTA = 1
00046 };
00047 
00048 int textscan (char *buf);
00049 int process_arguments (int, char **);
00050 int get_threshold (char *arg, char *rv[2]);
00051 void print_help (void);
00052 void print_usage (void);
00053 
00054 char *server_name = NULL;
00055 int packet_size = PACKET_SIZE;
00056 int packet_count = PACKET_COUNT;
00057 int target_timeout = 0;
00058 int packet_interval = 0;
00059 int verbose = FALSE;
00060 int cpl;
00061 int wpl;
00062 double crta;
00063 double wrta;
00064 int cpl_p = FALSE;
00065 int wpl_p = FALSE;
00066 int crta_p = FALSE;
00067 int wrta_p = FALSE;
00068 
00069 int
00070 main (int argc, char **argv)
00071 {
00072 /* normaly should be  int result = STATE_UNKNOWN; */
00073 
00074   int status = STATE_UNKNOWN;
00075   char *server = NULL;
00076   char *command_line = NULL;
00077   char *input_buffer = NULL;
00078   char *option_string = "";
00079   input_buffer = malloc (MAX_INPUT_BUFFER);
00080 
00081   setlocale (LC_ALL, "");
00082   bindtextdomain (PACKAGE, LOCALEDIR);
00083   textdomain (PACKAGE);
00084 
00085   /* Parse extra opts if any */
00086   argv=np_extra_opts (&argc, argv, progname);
00087 
00088   if (process_arguments (argc, argv) == ERROR)
00089     usage4 (_("Could not parse arguments"));
00090 
00091   server = strscpy (server, server_name);
00092 
00093   /* compose the command */
00094   if (target_timeout)
00095     asprintf(&option_string, "%s-t %d ", option_string, target_timeout);
00096   if (packet_interval)
00097     asprintf(&option_string, "%s-p %d ", option_string, packet_interval);
00098 
00099   asprintf (&command_line, "%s %s-b %d -c %d %s", PATH_TO_FPING,
00100             option_string, packet_size, packet_count, server);
00101 
00102   if (verbose)
00103     printf ("%s\n", command_line);
00104 
00105   /* run the command */
00106   child_process = spopen (command_line);
00107   if (child_process == NULL) {
00108     printf (_("Could not open pipe: %s\n"), command_line);
00109     return STATE_UNKNOWN;
00110   }
00111 
00112   child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
00113   if (child_stderr == NULL) {
00114     printf (_("Could not open stderr for %s\n"), command_line);
00115   }
00116 
00117   while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
00118     if (verbose)
00119       printf ("%s", input_buffer);
00120     status = max_state (status, textscan (input_buffer));
00121   }
00122 
00123   /* If we get anything on STDERR, at least set warning */
00124   while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
00125     status = max_state (status, STATE_WARNING);
00126     if (verbose)
00127       printf ("%s", input_buffer);
00128     status = max_state (status, textscan (input_buffer));
00129   }
00130   (void) fclose (child_stderr);
00131 
00132   /* close the pipe */
00133   if (spclose (child_process))
00134     /* need to use max_state not max */
00135     status = max_state (status, STATE_WARNING);
00136 
00137   printf ("FPING %s - %s\n", state_text (status), server_name);
00138 
00139   return status;
00140 }
00141 
00142 
00143 
00144 int
00145 textscan (char *buf)
00146 {
00147   char *rtastr = NULL;
00148   char *losstr = NULL;
00149   double loss;
00150   double rta;
00151   int status = STATE_UNKNOWN;
00152 
00153   if (strstr (buf, "not found")) {
00154     die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
00155 
00156   }
00157   else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
00158     die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
00159                "host");
00160 
00161   }
00162   else if (strstr (buf, "is down")) {
00163     die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
00164 
00165   }
00166   else if (strstr (buf, "is alive")) {
00167     status = STATE_OK;
00168 
00169   }
00170   else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
00171     losstr = strstr (buf, "=");
00172     losstr = 1 + strstr (losstr, "/");
00173     losstr = 1 + strstr (losstr, "/");
00174     rtastr = strstr (buf, "min/avg/max");
00175     rtastr = strstr (rtastr, "=");
00176     rtastr = 1 + index (rtastr, '/');
00177     loss = strtod (losstr, NULL);
00178     rta = strtod (rtastr, NULL);
00179     if (cpl_p == TRUE && loss > cpl)
00180       status = STATE_CRITICAL;
00181     else if (crta_p == TRUE  && rta > crta)
00182       status = STATE_CRITICAL;
00183     else if (wpl_p == TRUE && loss > wpl)
00184       status = STATE_WARNING;
00185     else if (wrta_p == TRUE && rta > wrta)
00186       status = STATE_WARNING;
00187     else
00188       status = STATE_OK;
00189     die (status,
00190           _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
00191          state_text (status), server_name, loss, rta,
00192          perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
00193          fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
00194 
00195   }
00196   else if(strstr (buf, "xmt/rcv/%loss") ) {
00197     /* no min/max/avg if host was unreachable in fping v2.2.b1 */
00198     losstr = strstr (buf, "=");
00199     losstr = 1 + strstr (losstr, "/");
00200     losstr = 1 + strstr (losstr, "/");
00201     loss = strtod (losstr, NULL);
00202     if (atoi(losstr) == 100)
00203       status = STATE_CRITICAL;
00204     else if (cpl_p == TRUE && loss > cpl)
00205       status = STATE_CRITICAL;
00206     else if (wpl_p == TRUE && loss > wpl)
00207       status = STATE_WARNING;
00208     else
00209       status = STATE_OK;
00210     /* loss=%.0f%%;%d;%d;0;100 */
00211     die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
00212          state_text (status), server_name, loss ,
00213          perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
00214 
00215   }
00216   else {
00217     status = max_state (status, STATE_WARNING);
00218   }
00219 
00220   return status;
00221 }
00222 
00223 
00224 
00225 /* process command-line arguments */
00226 int
00227 process_arguments (int argc, char **argv)
00228 {
00229   int c;
00230   char *rv[2];
00231 
00232   int option = 0;
00233   static struct option longopts[] = {
00234     {"hostname", required_argument, 0, 'H'},
00235     {"critical", required_argument, 0, 'c'},
00236     {"warning", required_argument, 0, 'w'},
00237     {"bytes", required_argument, 0, 'b'},
00238     {"number", required_argument, 0, 'n'},
00239     {"target-timeout", required_argument, 0, 'T'},
00240     {"interval", required_argument, 0, 'i'},
00241     {"verbose", no_argument, 0, 'v'},
00242     {"version", no_argument, 0, 'V'},
00243     {"help", no_argument, 0, 'h'},
00244     {0, 0, 0, 0}
00245   };
00246 
00247   rv[PL] = NULL;
00248   rv[RTA] = NULL;
00249 
00250   if (argc < 2)
00251     return ERROR;
00252 
00253   if (!is_option (argv[1])) {
00254     server_name = argv[1];
00255     argv[1] = argv[0];
00256     argv = &argv[1];
00257     argc--;
00258   }
00259 
00260   while (1) {
00261     c = getopt_long (argc, argv, "+hVvH:c:w:b:n:T:i:", longopts, &option);
00262 
00263     if (c == -1 || c == EOF || c == 1)
00264       break;
00265 
00266     switch (c) {
00267     case '?':                 /* print short usage statement if args not parsable */
00268       usage5 ();
00269     case 'h':                 /* help */
00270       print_help ();
00271       exit (STATE_OK);
00272     case 'V':                 /* version */
00273       print_revision (progname, NP_VERSION);
00274       exit (STATE_OK);
00275     case 'v':                 /* verbose mode */
00276       verbose = TRUE;
00277       break;
00278     case 'H':                 /* hostname */
00279       if (is_host (optarg) == FALSE) {
00280         usage2 (_("Invalid hostname/address"), optarg);
00281       }
00282       server_name = strscpy (server_name, optarg);
00283       break;
00284     case 'c':
00285       get_threshold (optarg, rv);
00286       if (rv[RTA]) {
00287         crta = strtod (rv[RTA], NULL);
00288         crta_p = TRUE;
00289         rv[RTA] = NULL;
00290       }
00291       if (rv[PL]) {
00292         cpl = atoi (rv[PL]);
00293         cpl_p = TRUE;
00294         rv[PL] = NULL;
00295       }
00296       break;
00297     case 'w':
00298       get_threshold (optarg, rv);
00299       if (rv[RTA]) {
00300         wrta = strtod (rv[RTA], NULL);
00301         wrta_p = TRUE;
00302         rv[RTA] = NULL;
00303       }
00304       if (rv[PL]) {
00305         wpl = atoi (rv[PL]);
00306         wpl_p = TRUE;
00307         rv[PL] = NULL;
00308       }
00309       break;
00310     case 'b':                 /* bytes per packet */
00311       if (is_intpos (optarg))
00312         packet_size = atoi (optarg);
00313       else
00314         usage (_("Packet size must be a positive integer"));
00315       break;
00316     case 'n':                 /* number of packets */
00317       if (is_intpos (optarg))
00318         packet_count = atoi (optarg);
00319       else
00320         usage (_("Packet count must be a positive integer"));
00321       break;
00322     case 'T':                 /* timeout in msec */
00323       if (is_intpos (optarg))
00324         target_timeout = atoi (optarg);
00325       else
00326         usage (_("Target timeout must be a positive integer"));
00327       break;
00328     case 'i':                 /* interval in msec */
00329       if (is_intpos (optarg))
00330         packet_interval = atoi (optarg);
00331       else
00332         usage (_("Interval must be a positive integer"));
00333       break;
00334     }
00335   }
00336 
00337   if (server_name == NULL)
00338     usage4 (_("Hostname was not supplied"));
00339 
00340   return OK;
00341 }
00342 
00343 
00344 int
00345 get_threshold (char *arg, char *rv[2])
00346 {
00347   char *arg1 = NULL;
00348   char *arg2 = NULL;
00349 
00350   arg1 = strscpy (arg1, arg);
00351   if (strpbrk (arg1, ",:"))
00352     arg2 = 1 + strpbrk (arg1, ",:");
00353 
00354   if (arg2) {
00355     arg1[strcspn (arg1, ",:")] = 0;
00356     if (strstr (arg1, "%") && strstr (arg2, "%"))
00357       die (STATE_UNKNOWN,
00358                  _("%s: Only one threshold may be packet loss (%s)\n"), progname,
00359                  arg);
00360     if (!strstr (arg1, "%") && !strstr (arg2, "%"))
00361       die (STATE_UNKNOWN,
00362                  _("%s: Only one threshold must be packet loss (%s)\n"),
00363                  progname, arg);
00364   }
00365 
00366   if (arg2 && strstr (arg2, "%")) {
00367     rv[PL] = arg2;
00368     rv[RTA] = arg1;
00369   }
00370   else if (arg2) {
00371     rv[PL] = arg1;
00372     rv[RTA] = arg2;
00373   }
00374   else if (strstr (arg1, "%")) {
00375     rv[PL] = arg1;
00376   }
00377   else {
00378     rv[RTA] = arg1;
00379   }
00380 
00381   return OK;
00382 }
00383 
00384 
00385 void
00386 print_help (void)
00387 {
00388 
00389   print_revision (progname, NP_VERSION);
00390 
00391   printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
00392   printf (COPYRIGHT, copyright, email);
00393 
00394   printf ("%s\n", _("This plugin will use the fping command to ping the specified host for a fast check"));
00395 
00396   printf ("%s\n", _("Note that it is necessary to set the suid flag on fping."));
00397 
00398   printf ("\n\n");
00399 
00400   print_usage ();
00401 
00402   printf (UT_HELP_VRSN);
00403   printf (UT_EXTRA_OPTS);
00404 
00405   printf (" %s\n", "-H, --hostname=HOST");
00406   printf ("    %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)"));
00407   printf (" %s\n", "-w, --warning=THRESHOLD");
00408   printf ("    %s\n", _("warning threshold pair"));
00409   printf (" %s\n", "-c, --critical=THRESHOLD");
00410   printf ("    %s\n", _("critical threshold pair"));
00411   printf (" %s\n", "-b, --bytes=INTEGER");
00412   printf ("    %s (default: %d)\n", _("size of ICMP packet"),PACKET_SIZE);
00413   printf (" %s\n", "-n, --number=INTEGER");
00414   printf ("    %s (default: %d)\n", _("number of ICMP packets to send"),PACKET_COUNT);
00415   printf (" %s\n", "-T, --target-timeout=INTEGER");
00416   printf ("    %s (default: fping's default for -t)\n", _("Target timeout (ms)"),PACKET_COUNT);
00417   printf (" %s\n", "-i, --interval=INTEGER");
00418   printf ("    %s (default: fping's default for -p)\n", _("Interval (ms) between sending packets"),PACKET_COUNT);
00419   printf (UT_VERBOSE);
00420   printf ("\n");
00421   printf (" %s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)"));
00422   printf (" %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of"));
00423   printf (" %s\n", _("packet loss to trigger an alarm state."));
00424 
00425   printf (UT_SUPPORT);
00426 }
00427 
00428 
00429 void
00430 print_usage (void)
00431 {
00432   printf ("%s\n", _("Usage:"));
00433   printf (" %s <host_address> -w limit -c limit [-b size] [-n number] [-T number] [-i number]\n", progname);
00434 }