Code

Fix test failure on poseidon
[nagiosplug.git] / plugins / check_fping.c
1 /******************************************************************************
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; either version 2 of the License, or
6  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  GNU General Public License for more details.
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  $Id$
18  
19 ******************************************************************************/
21 const char *progname = "check_fping";
22 const char *revision = "$Revision$";
23 const char *copyright = "2000-2004";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "popen.h"
28 #include "netutils.h"
29 #include "utils.h"
31 enum {
32   PACKET_COUNT = 1,
33   PACKET_SIZE = 56,
34   PL = 0,
35   RTA = 1
36 };
38 int textscan (char *buf);
39 int process_arguments (int, char **);
40 int get_threshold (char *arg, char *rv[2]);
41 void print_help (void);
42 void print_usage (void);
44 char *server_name = NULL;
45 int packet_size = PACKET_SIZE;
46 int packet_count = PACKET_COUNT;
47 int verbose = FALSE;
48 int cpl;
49 int wpl;
50 double crta;
51 double wrta;
52 int cpl_p = FALSE;
53 int wpl_p = FALSE;
54 int crta_p = FALSE;
55 int wrta_p = FALSE;
57 int
58 main (int argc, char **argv)
59 {
60 /* normaly should be  int result = STATE_UNKNOWN; */
62   int status = STATE_UNKNOWN;
63   char *server = NULL;
64   char *command_line = NULL;
65   char *input_buffer = NULL;
66   input_buffer = malloc (MAX_INPUT_BUFFER);
68   setlocale (LC_ALL, "");
69   bindtextdomain (PACKAGE, LOCALEDIR);
70   textdomain (PACKAGE);
72   if (process_arguments (argc, argv) == ERROR)
73     usage4 (_("Could not parse arguments"));
75   server = strscpy (server, server_name);
77   /* compose the command */
78   asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
79             packet_size, packet_count, server);
81   if (verbose)
82     printf ("%s\n", command_line);
84   /* run the command */
85   child_process = spopen (command_line);
86   if (child_process == NULL) {
87     printf (_("Could not open pipe: %s\n"), command_line);
88     return STATE_UNKNOWN;
89   }
91   child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
92   if (child_stderr == NULL) {
93     printf (_("Could not open stderr for %s\n"), command_line);
94   }
96   while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
97     if (verbose)
98       printf ("%s", input_buffer);
99     status = max_state (status, textscan (input_buffer));
100   }
102   /* If we get anything on STDERR, at least set warning */
103   while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
104     status = max_state (status, STATE_WARNING);
105     if (verbose)
106       printf ("%s", input_buffer);
107     status = max_state (status, textscan (input_buffer));
108   }
109   (void) fclose (child_stderr);
111   /* close the pipe */
112   if (spclose (child_process))
113     /* need to use max_state not max */
114     status = max_state (status, STATE_WARNING);
116   printf ("FPING %s - %s\n", state_text (status), server_name);
118   return status;
123 int
124 textscan (char *buf)
126   char *rtastr = NULL;
127   char *losstr = NULL;
128   double loss;
129   double rta;
130   int status = STATE_UNKNOWN;
132   if (strstr (buf, "not found")) {
133     die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
135   }
136   else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
137     die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
138                "host");
140   }
141   else if (strstr (buf, "is down")) {
142     die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
144   }
145   else if (strstr (buf, "is alive")) {
146     status = STATE_OK;
148   }
149   else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
150     losstr = strstr (buf, "=");
151     losstr = 1 + strstr (losstr, "/");
152     losstr = 1 + strstr (losstr, "/");
153     rtastr = strstr (buf, "min/avg/max");
154     rtastr = strstr (rtastr, "=");
155     rtastr = 1 + index (rtastr, '/');
156     loss = strtod (losstr, NULL);
157     rta = strtod (rtastr, NULL);
158     if (cpl_p == TRUE && loss > cpl)
159       status = STATE_CRITICAL;
160     else if (crta_p == TRUE  && rta > crta)
161       status = STATE_CRITICAL;
162     else if (wpl_p == TRUE && loss > wpl)
163       status = STATE_WARNING;
164     else if (wrta_p == TRUE && rta > wrta)
165       status = STATE_WARNING;
166     else
167       status = STATE_OK;
168     die (status,
169           _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
170          state_text (status), server_name, loss, rta,
171          perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
172          fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
174   }
175   else if(strstr (buf, "xmt/rcv/%loss") ) {
176     /* no min/max/avg if host was unreachable in fping v2.2.b1 */
177     losstr = strstr (buf, "=");
178     losstr = 1 + strstr (losstr, "/");
179     losstr = 1 + strstr (losstr, "/");
180     loss = strtod (losstr, NULL);
181     if (atoi(losstr) == 100)
182       status = STATE_CRITICAL;
183     else if (cpl_p == TRUE && loss > cpl)
184       status = STATE_CRITICAL;
185     else if (wpl_p == TRUE && loss > wpl)
186       status = STATE_WARNING;
187     else
188       status = STATE_OK;
189     /* loss=%.0f%%;%d;%d;0;100 */
190     die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
191          state_text (status), server_name, loss ,
192          perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
193   
194   }
195   else {
196     status = max_state (status, STATE_WARNING);
197   }
199   return status;
204 /* process command-line arguments */
205 int
206 process_arguments (int argc, char **argv)
208   int c;
209   char *rv[2];
211   int option = 0;
212   static struct option longopts[] = {
213     {"hostname", required_argument, 0, 'H'},
214     {"critical", required_argument, 0, 'c'},
215     {"warning", required_argument, 0, 'w'},
216     {"bytes", required_argument, 0, 'b'},
217     {"number", required_argument, 0, 'n'},
218     {"verbose", no_argument, 0, 'v'},
219     {"version", no_argument, 0, 'V'},
220     {"help", no_argument, 0, 'h'},
221     {0, 0, 0, 0}
222   };
224   rv[PL] = NULL;
225   rv[RTA] = NULL;
227   if (argc < 2)
228     return ERROR;
230   if (!is_option (argv[1])) {
231     server_name = argv[1];
232     argv[1] = argv[0];
233     argv = &argv[1];
234     argc--;
235   }
237   while (1) {
238     c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", longopts, &option);
240     if (c == -1 || c == EOF || c == 1)
241       break;
243     switch (c) {
244     case '?':                 /* print short usage statement if args not parsable */
245       usage2 (_("Unknown argument"), optarg);
246     case 'h':                 /* help */
247       print_help ();
248       exit (STATE_OK);
249     case 'V':                 /* version */
250       print_revision (progname, revision);
251       exit (STATE_OK);
252     case 'v':                 /* verbose mode */
253       verbose = TRUE;
254       break;
255     case 'H':                 /* hostname */
256       if (is_host (optarg) == FALSE) {
257         usage2 (_("Invalid hostname/address"), optarg);
258       }
259       server_name = strscpy (server_name, optarg);
260       break;
261     case 'c':
262       get_threshold (optarg, rv);
263       if (rv[RTA]) {
264         crta = strtod (rv[RTA], NULL);
265         crta_p = TRUE;
266         rv[RTA] = NULL;
267       }
268       if (rv[PL]) {
269         cpl = atoi (rv[PL]);
270         cpl_p = TRUE;
271         rv[PL] = NULL;
272       }
273       break;
274     case 'w':
275       get_threshold (optarg, rv);
276       if (rv[RTA]) {
277         wrta = strtod (rv[RTA], NULL);
278         wrta_p = TRUE;
279         rv[RTA] = NULL;
280       }
281       if (rv[PL]) {
282         wpl = atoi (rv[PL]);
283         wpl_p = TRUE;
284         rv[PL] = NULL;
285       }
286       break;
287     case 'b':                 /* bytes per packet */
288       if (is_intpos (optarg))
289         packet_size = atoi (optarg);
290       else
291         usage (_("Packet size must be a positive integer"));
292       break;
293     case 'n':                 /* number of packets */
294       if (is_intpos (optarg))
295         packet_count = atoi (optarg);
296       else
297         usage (_("Packet count must be a positive integer"));
298       break;
299     }
300   }
302   if (server_name == NULL)
303     usage4 (_("Hostname was not supplied"));
305   return OK;
309 int
310 get_threshold (char *arg, char *rv[2])
312   char *arg1 = NULL;
313   char *arg2 = NULL;
315   arg1 = strscpy (arg1, arg);
316   if (strpbrk (arg1, ",:"))
317     arg2 = 1 + strpbrk (arg1, ",:");
319   if (arg2) {
320     arg1[strcspn (arg1, ",:")] = 0;
321     if (strstr (arg1, "%") && strstr (arg2, "%"))
322       die (STATE_UNKNOWN,
323                  _("%s: Only one threshold may be packet loss (%s)\n"), progname,
324                  arg);
325     if (!strstr (arg1, "%") && !strstr (arg2, "%"))
326       die (STATE_UNKNOWN,
327                  _("%s: Only one threshold must be packet loss (%s)\n"),
328                  progname, arg);
329   }
331   if (arg2 && strstr (arg2, "%")) {
332     rv[PL] = arg2;
333     rv[RTA] = arg1;
334   }
335   else if (arg2) {
336     rv[PL] = arg1;
337     rv[RTA] = arg2;
338   }
339   else if (strstr (arg1, "%")) {
340     rv[PL] = arg1;
341   }
342   else {
343     rv[RTA] = arg1;
344   }
346   return OK;
350 void
351 print_help (void)
354   print_revision (progname, revision);
356   printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
357   printf (COPYRIGHT, copyright, email);
359   printf ("%s\n", _("This plugin will use the fping command to ping the specified host for a fast check"));
360   
361   printf ("%s\n", _("Note that it is necessary to set the suid flag on fping."));
363   printf ("\n\n");
364   
365   print_usage ();
367   printf (_(UT_HELP_VRSN));
369   printf (" %s\n", "-H, --hostname=HOST");
370   printf ("    %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)"));
371   printf (" %s\n", "-w, --warning=THRESHOLD");
372   printf ("    %s\n", _("warning threshold pair"));
373   printf (" %s\n", "-c, --critical=THRESHOLD");
374   printf ("    %s\n", _("critical threshold pair"));
375   printf (" %s\n", "-b, --bytes=INTEGER");
376   printf ("    %s\n", _("size of ICMP packet (default: %d)"),PACKET_SIZE);
377   printf (" %s\n", "-n, --number=INTEGER");
378   printf ("    %s\n", _("number of ICMP packets to send (default: %d)"),PACKET_COUNT);
379   printf (_(UT_VERBOSE));
380   printf ("\n");
381   printf ("    %s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)"));
382   printf ("    %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of"));
383   printf ("    %s\n", _("packet loss to trigger an alarm state."));
384   printf (_(UT_SUPPORT));
388 void
389 print_usage (void)
391   printf (_("Usage:"));
392   printf (" %s <host_address> -w limit -c limit [-b size] [-n number]\n", progname);