Code

817d647aa4aa14757a49647b13a657cf4f0d2e49
[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-2003";
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         int status = STATE_UNKNOWN;
61         char *server = NULL;
62         char *command_line = NULL;
63         char *input_buffer = NULL;
64         input_buffer = malloc (MAX_INPUT_BUFFER);
66         setlocale (LC_ALL, "");
67         bindtextdomain (PACKAGE, LOCALEDIR);
68         textdomain (PACKAGE);
70         if (process_arguments (argc, argv) != TRUE)
71                 usage4 (_("Could not parse arguments"));
73         server = strscpy (server, server_name);
75         /* compose the command */
76         asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
77                   packet_size, packet_count, server);
79         if (verbose)
80                 printf ("%s\n", command_line);
82         /* run the command */
83         child_process = spopen (command_line);
84         if (child_process == NULL) {
85                 printf (_("Could not open pipe: %s\n"), command_line);
86                 return STATE_UNKNOWN;
87         }
89         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
90         if (child_stderr == NULL) {
91                 printf (_("Could not open stderr for %s\n"), command_line);
92         }
94         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
95                 if (verbose)
96                         printf ("%s", input_buffer);
97                 status = max_state (status, textscan (input_buffer));
98         }
100         /* If we get anything on STDERR, at least set warning */
101         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
102                 status = max_state (status, STATE_WARNING);
103                 if (verbose)
104                         printf ("%s", input_buffer);
105                 status = max_state (status, textscan (input_buffer));
106         }
107         (void) fclose (child_stderr);
109         /* close the pipe */
110         if (spclose (child_process))
111                 /* need to use max_state not max */
112                 status = max_state (status, STATE_WARNING);
114         printf ("FPING %s - %s\n", state_text (status), server_name);
116         return status;
121 int
122 textscan (char *buf)
124         char *rtastr = NULL;
125         char *losstr = NULL;
126         double loss;
127         double rta;
128         int status = STATE_UNKNOWN;
130         if (strstr (buf, "not found")) {
131                 die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
133         }
134         else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
135                 die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
136                                                          "host");
138         }
139         else if (strstr (buf, "is down")) {
140                 die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
142         }
143         else if (strstr (buf, "is alive")) {
144                 status = STATE_OK;
146         }
147         else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
148                 losstr = strstr (buf, "=");
149                 losstr = 1 + strstr (losstr, "/");
150                 losstr = 1 + strstr (losstr, "/");
151                 rtastr = strstr (buf, "min/avg/max");
152                 rtastr = strstr (rtastr, "=");
153                 rtastr = 1 + index (rtastr, '/');
154                 loss = strtod (losstr, NULL);
155                 rta = strtod (rtastr, NULL);
156                 if (cpl_p == TRUE && loss > cpl)
157                         status = STATE_CRITICAL;
158                 else if (crta_p == TRUE  && rta > crta)
159                         status = STATE_CRITICAL;
160                 else if (wpl_p == TRUE && loss > wpl)
161                         status = STATE_WARNING;
162                 else if (wrta_p == TRUE && rta > wrta)
163                         status = STATE_WARNING;
164                 else
165                         status = STATE_OK;
166                 die (status,
167                       _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
168                                  state_text (status), server_name, loss, rta,
169                      perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
170                      fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
172         }
173         else if(strstr (buf, "xmt/rcv/%loss") ) {
174                 /* no min/max/avg if host was unreachable in fping v2.2.b1 */
175                 losstr = strstr (buf, "=");
176                 losstr = 1 + strstr (losstr, "/");
177                 losstr = 1 + strstr (losstr, "/");
178                 loss = strtod (losstr, NULL);
179                 if (atoi(losstr) == 100)
180                         status = STATE_CRITICAL;
181                 else if (cpl_p == TRUE && loss > cpl)
182                         status = STATE_CRITICAL;
183                 else if (wpl_p == TRUE && loss > wpl)
184                         status = STATE_WARNING;
185                 else
186                         status = STATE_OK;
187                 /* loss=%.0f%%;%d;%d;0;100 */
188                 die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
189                      state_text (status), server_name, loss ,
190                      perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
191         
192         }
193         else {
194                 status = max_state (status, STATE_WARNING);
195         }
197         return status;
202 /* process command-line arguments */
203 int
204 process_arguments (int argc, char **argv)
206         int c;
207         char *rv[2];
209         int option = 0;
210         static struct option longopts[] = {
211                 {"hostname", required_argument, 0, 'H'},
212                 {"critical", required_argument, 0, 'c'},
213                 {"warning", required_argument, 0, 'w'},
214                 {"bytes", required_argument, 0, 'b'},
215                 {"number", required_argument, 0, 'n'},
216                 {"verbose", no_argument, 0, 'v'},
217                 {"version", no_argument, 0, 'V'},
218                 {"help", no_argument, 0, 'h'},
219                 {0, 0, 0, 0}
220         };
222         rv[PL] = NULL;
223         rv[RTA] = NULL;
225         if (argc < 2)
226                 return ERROR;
228         if (!is_option (argv[1])) {
229                 server_name = argv[1];
230                 argv[1] = argv[0];
231                 argv = &argv[1];
232                 argc--;
233         }
235         while (1) {
236                 c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", longopts, &option);
238                 if (c == -1 || c == EOF || c == 1)
239                         break;
241                 switch (c) {
242                 case '?':                                                                       /* print short usage statement if args not parsable */
243                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
244                         print_usage ();
245                         exit (STATE_UNKNOWN);
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         }
303         if (server_name == NULL)
304                 usage (_("Hostname was not supplied\n\n"));
306         return OK;
311 int
312 get_threshold (char *arg, char *rv[2])
314         char *arg1 = NULL;
315         char *arg2 = NULL;
317         arg1 = strscpy (arg1, arg);
318         if (strpbrk (arg1, ",:"))
319                 arg2 = 1 + strpbrk (arg1, ",:");
321         if (arg2) {
322                 arg1[strcspn (arg1, ",:")] = 0;
323                 if (strstr (arg1, "%") && strstr (arg2, "%"))
324                         die (STATE_UNKNOWN,
325                                                                  _("%s: Only one threshold may be packet loss (%s)\n"), progname,
326                                                                  arg);
327                 if (!strstr (arg1, "%") && !strstr (arg2, "%"))
328                         die (STATE_UNKNOWN,
329                                                                  _("%s: Only one threshold must be packet loss (%s)\n"),
330                                                                  progname, arg);
331         }
333         if (arg2 && strstr (arg2, "%")) {
334                 rv[PL] = arg2;
335                 rv[RTA] = arg1;
336         }
337         else if (arg2) {
338                 rv[PL] = arg1;
339                 rv[RTA] = arg2;
340         }
341         else if (strstr (arg1, "%")) {
342                 rv[PL] = arg1;
343         }
344         else {
345                 rv[RTA] = arg1;
346         }
348         return OK;
353 void
354 print_help (void)
357         print_revision (progname, "$Revision$");
359         printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
360         printf (COPYRIGHT, copyright, email);
362         printf (_("\
363 This plugin will use the /bin/fping command (from saint) to ping the\n\
364 specified host for a fast check if the host is alive. Note that it is\n\
365 necessary to set the suid flag on fping.\n\n"));
367         print_usage ();
369         printf (_(UT_HELP_VRSN));
371         printf (_("\
372  -H, --hostname=HOST\n\
373     Name or IP Address of host to ping (IP Address bypasses name lookup,\n\
374     reducing system load)\n\
375  -w, --warning=THRESHOLD\n\
376     warning threshold pair\n\
377  -c, --critical=THRESHOLD\n\
378     critical threshold pair\n\
379  -b, --bytes=INTEGER\n\
380     Size of ICMP packet (default: %d)\n\
381  -n, --number=INTEGER\n\
382     Number of ICMP packets to send (default: %d)\n"),
383                 PACKET_SIZE, PACKET_COUNT);
385         printf (_(UT_VERBOSE));
387         printf (_("\n\
388 THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
389 time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
390 percentage of packet loss to trigger an alarm state.\n"));
392         printf (_(UT_SUPPORT));
397 void
398 print_usage (void)
400         printf (_("Usage: %s <host_address>\n"), progname);
401         printf (_(UT_HLP_VRS), progname, progname);