Code

afs checking
[nagiosplug.git] / plugins / check_fping.c
1 /******************************************************************************
2 *
3 * CHECK_FPING.C
4 *
5 * Program: Fping plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
8 * $Id$
9 *
10 * Modifications:
11 *
12 * 08-24-1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
13 *            Intial Coding
14 * 09-11-1999 Karl DeBisschop (kdebiss@alum.mit.edu)
15 *            Change to spopen
16 *            Fix so that state unknown is returned by default
17 *            (formerly would give state ok if no fping specified)
18 *            Add server_name to output
19 *            Reformat to 80-character standard screen
20 * 11-18-1999 Karl DeBisschop (kdebiss@alum.mit.edu)
21 *            set STATE_WARNING of stderr written or nonzero status returned
22 *
23 * Description:
24 *
25 * This plugin will use the /bin/fping command (form saint) to ping
26 * the specified host for a fast check if the host is alive. Note that
27 * it is necessary to set the suid flag on fping.
28 ******************************************************************************/
30 #include "config.h"
31 #include "common.h"
32 #include "popen.h"
33 #include "utils.h"
35 const char *progname = "check_fping";
36 #define PACKET_COUNT 1
37 #define PACKET_SIZE 56
38 #define UNKNOWN_PACKET_LOSS 200 /* 200% */
39 #define UNKNOWN_TRIP_TIME -1.0  /* -1 seconds */
41 #define PL 0
42 #define RTA 1
44 int textscan (char *buf);
45 int process_arguments (int, char **);
46 int get_threshold (char *arg, char *rv[2]);
47 void print_usage (void);
48 void print_help (void);
50 char *server_name = NULL;
51 int cpl = UNKNOWN_PACKET_LOSS;
52 int wpl = UNKNOWN_PACKET_LOSS;
53 double crta = UNKNOWN_TRIP_TIME;
54 double wrta = UNKNOWN_TRIP_TIME;
55 int packet_size = PACKET_SIZE;
56 int packet_count = PACKET_COUNT;
57 int verbose = FALSE;
59 int
60 main (int argc, char **argv)
61 {
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         if (process_arguments (argc, argv) == ERROR)
69                 usage ("Could not parse arguments\n");
71         server = strscpy (server, server_name);
73         /* compose the command */
74         asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
75                   packet_size, packet_count, server);
77         if (verbose)
78                 printf ("%s\n", command_line);
80         /* run the command */
81         child_process = spopen (command_line);
82         if (child_process == NULL) {
83                 printf ("Unable to open pipe: %s\n", command_line);
84                 return STATE_UNKNOWN;
85         }
87         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
88         if (child_stderr == NULL) {
89                 printf ("Could not open stderr for %s\n", command_line);
90         }
92         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
93                 if (verbose)
94                         printf ("%s", input_buffer);
95                 status = max_state (status, textscan (input_buffer));
96         }
98         /* If we get anything on STDERR, at least set warning */
99         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
100                 status = max_state (status, STATE_WARNING);
101                 if (verbose)
102                         printf ("%s", input_buffer);
103                 status = max_state (status, textscan (input_buffer));
104         }
105         (void) fclose (child_stderr);
107         /* close the pipe */
108         if (spclose (child_process))
109                 /* need to use max_state not max */
110                 status = max_state (status, STATE_WARNING);
112         printf ("FPING %s - %s\n", state_text (status), server_name);
114         return status;
120 int
121 textscan (char *buf)
123         char *rtastr = NULL;
124         char *losstr = NULL;
125         double loss;
126         double rta;
127         int status = STATE_UNKNOWN;
129         if (strstr (buf, "not found")) {
130                 terminate (STATE_CRITICAL, "FPING unknown - %s not found\n", server_name);
132         }
133         else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
134                 terminate (STATE_CRITICAL, "FPING critical - %s is unreachable\n",
135                                                          "host");
137         }
138         else if (strstr (buf, "is down")) {
139                 terminate (STATE_CRITICAL, "FPING critical - %s is down\n", server_name);
141         }
142         else if (strstr (buf, "is alive")) {
143                 status = STATE_OK;
145         }
146         else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
147                 losstr = strstr (buf, "=");
148                 losstr = 1 + strstr (losstr, "/");
149                 losstr = 1 + strstr (losstr, "/");
150                 rtastr = strstr (buf, "min/avg/max");
151                 rtastr = strstr (rtastr, "=");
152                 rtastr = 1 + index (rtastr, '/');
153                 loss = strtod (losstr, NULL);
154                 rta = strtod (rtastr, NULL);
155                 if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
156                         status = STATE_CRITICAL;
157                 else if (crta != UNKNOWN_TRIP_TIME && rta > crta)
158                         status = STATE_CRITICAL;
159                 else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
160                         status = STATE_WARNING;
161                 else if (wrta != UNKNOWN_TRIP_TIME && rta > wrta)
162                         status = STATE_WARNING;
163                 else
164                         status = STATE_OK;
165                 terminate (status, "FPING %s - %s (loss=%f%%, rta=%f ms)\n",
166                                                          state_text (status), server_name, loss, rta);
168         }
169         else if(strstr (buf, "xmt/rcv/%loss") ) {
170                 /* no min/max/avg if host was unreachable in fping v2.2.b1 */
171                 losstr = strstr (buf, "=");
172                 losstr = 1 + strstr (losstr, "/");
173                 losstr = 1 + strstr (losstr, "/");
174                 loss = strtod (losstr, NULL);
175                 if (loss == 100)
176                         status = STATE_CRITICAL;
177                 else if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
178                         status = STATE_CRITICAL;
179                 else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
180                         status = STATE_WARNING;
181                 else
182                         status = STATE_OK;
183                 
184                 terminate (status, "FPING %s - %s (loss=%f%% )\n",
185                                                          state_text (status), server_name, loss );              
186         
187         }
188         else {
189                 status = max_state (status, STATE_WARNING);
190         }
192         return status;
198 /* process command-line arguments */
199 int
200 process_arguments (int argc, char **argv)
202         int c;
203         char *rv[2];
205         int option_index = 0;
206         static struct option long_options[] = {
207                 {"hostname", required_argument, 0, 'H'},
208                 {"critical", required_argument, 0, 'c'},
209                 {"warning", required_argument, 0, 'w'},
210                 {"bytes", required_argument, 0, 'b'},
211                 {"number", required_argument, 0, 'n'},
212                 {"verbose", no_argument, 0, 'v'},
213                 {"version", no_argument, 0, 'V'},
214                 {"help", no_argument, 0, 'h'},
215                 {0, 0, 0, 0}
216         };
218         rv[PL] = NULL;
219         rv[RTA] = NULL;
221         if (argc < 2)
222                 return ERROR;
224         if (!is_option (argv[1])) {
225                 server_name = argv[1];
226                 argv[1] = argv[0];
227                 argv = &argv[1];
228                 argc--;
229         }
231         while (1) {
232                 c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", long_options, &option_index);
234                 if (c == -1 || c == EOF || c == 1)
235                         break;
237                 switch (c) {
238                 case '?':                                                                       /* print short usage statement if args not parsable */
239                         printf ("%s: Unknown argument: %s\n\n", progname, optarg);
240                         print_usage ();
241                         exit (STATE_UNKNOWN);
242                 case 'h':                                                                       /* help */
243                         print_help ();
244                         exit (STATE_OK);
245                 case 'V':                                                                       /* version */
246                         print_revision (progname, "$Revision$");
247                         exit (STATE_OK);
248                 case 'v':                                                                       /* verbose mode */
249                         verbose = TRUE;
250                         break;
251                 case 'H':                                                                       /* hostname */
252                         if (is_host (optarg) == FALSE) {
253                                 printf ("Invalid host name/address\n\n");
254                                 print_usage ();
255                                 exit (STATE_UNKNOWN);
256                         }
257                         server_name = strscpy (server_name, optarg);
258                         break;
259                 case 'c':
260                         get_threshold (optarg, rv);
261                         if (rv[RTA]) {
262                                 crta = strtod (rv[RTA], NULL);
263                                 rv[RTA] = NULL;
264                         }
265                         if (rv[PL]) {
266                                 cpl = atoi (rv[PL]);
267                                 rv[PL] = NULL;
268                         }
269                         break;
270                 case 'w':
271                         get_threshold (optarg, rv);
272                         if (rv[RTA]) {
273                                 wrta = strtod (rv[RTA], NULL);
274                                 rv[RTA] = NULL;
275                         }
276                         if (rv[PL]) {
277                                 wpl = atoi (rv[PL]);
278                                 rv[PL] = NULL;
279                         }
280                         break;
281                 case 'b':                                                                       /* bytes per packet */
282                         if (is_intpos (optarg))
283                                 packet_size = atoi (optarg);
284                         else
285                                 usage ("Packet size must be a positive integer");
286                         break;
287                 case 'n':                                                                       /* number of packets */
288                         if (is_intpos (optarg))
289                                 packet_count = atoi (optarg);
290                         else
291                                 usage ("Packet count must be a positive integer");
292                         break;
293                 }
294         }
297         if (server_name == NULL)
298                 usage ("Host name was not supplied\n\n");
300         return OK;
307 int
308 get_threshold (char *arg, char *rv[2])
310         char *arg1 = NULL;
311         char *arg2 = NULL;
313         arg1 = strscpy (arg1, arg);
314         if (strpbrk (arg1, ",:"))
315                 arg2 = 1 + strpbrk (arg1, ",:");
317         if (arg2) {
318                 arg1[strcspn (arg1, ",:")] = 0;
319                 if (strstr (arg1, "%") && strstr (arg2, "%"))
320                         terminate (STATE_UNKNOWN,
321                                                                  "%s: Only one threshold may be packet loss (%s)\n", progname,
322                                                                  arg);
323                 if (!strstr (arg1, "%") && !strstr (arg2, "%"))
324                         terminate (STATE_UNKNOWN,
325                                                                  "%s: Only one threshold must be packet loss (%s)\n",
326                                                                  progname, arg);
327         }
329         if (arg2 && strstr (arg2, "%")) {
330                 rv[PL] = arg2;
331                 rv[RTA] = arg1;
332         }
333         else if (arg2) {
334                 rv[PL] = arg1;
335                 rv[RTA] = arg2;
336         }
337         else if (strstr (arg1, "%")) {
338                 rv[PL] = arg1;
339         }
340         else {
341                 rv[RTA] = arg1;
342         }
344         return OK;
351 void
352 print_usage (void)
354         printf ("Usage: %s <host_address>\n", progname);
361 void
362 print_help (void)
365         print_revision (progname, "$Revision$");
367         printf
368                 ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n\n"
369                  "This plugin will use the /bin/fping command (from saint) to ping the\n"
370                  "specified host for a fast check if the host is alive. Note that it is\n"
371                  "necessary to set the suid flag on fping.\n\n");
373         print_usage ();
375         printf
376                 ("\nOptions:\n"
377                  "-H, --hostname=HOST\n"
378                  "   Name or IP Address of host to ping (IP Address bypasses name lookup,\n"
379                  "   reducing system load)\n"
380                  "-w, --warning=THRESHOLD\n"
381                  "   warning threshold pair\n"
382                  "-c, --critical=THRESHOLD\n"
383                  "   critical threshold pair\n"
384                  "-b, --bytes=INTEGER\n"
385                  "   Size of ICMP packet (default: %d)\n"
386                  "-n, --number=INTEGER\n"
387                  "   Number of ICMP packets to send (default: %d)\n"
388                  "-v, --verbose\n"
389                  "   Show details for command-line debugging (do not use with nagios server)\n"
390                  "-h, --help\n"
391                  "   Print this help screen\n"
392                  "-V, --version\n"
393                  "   Print version information\n"
394                  "THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n"
395                  "time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n"
396                  "percentage of packet loss to trigger an alarm state.\n",
397                  PACKET_SIZE, PACKET_COUNT);