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 #define 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 command_line = ssprintf
75 (command_line, "%s -b %d -c %d %s",
76 PATH_TO_FPING, packet_size, packet_count, server);
78 if (verbose)
79 printf ("%s\n", command_line);
81 /* run the command */
82 child_process = spopen (command_line);
83 if (child_process == NULL) {
84 printf ("Unable to open pipe: %s\n", command_line);
85 return STATE_UNKNOWN;
86 }
88 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
89 if (child_stderr == NULL) {
90 printf ("Could not open stderr for %s\n", command_line);
91 }
93 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
94 if (verbose)
95 printf ("%s", input_buffer);
96 status = max_state (status, textscan (input_buffer));
97 }
99 /* If we get anything on STDERR, at least set warning */
100 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
101 status = max_state (status, STATE_WARNING);
102 if (verbose)
103 printf ("%s", input_buffer);
104 status = max_state (status, textscan (input_buffer));
105 }
106 (void) fclose (child_stderr);
108 /* close the pipe */
109 if (spclose (child_process))
110 /* need to use max_state not max */
111 status = max_state (status, STATE_WARNING);
113 printf ("FPING %s - %s\n", state_text (status), server_name);
115 return status;
116 }
121 int
122 textscan (char *buf)
123 {
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 terminate (STATE_CRITICAL, "FPING unknown - %s not found\n", server_name);
133 }
134 else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
135 terminate (STATE_CRITICAL, "FPING critical - %s is unreachable\n",
136 "host");
138 }
139 else if (strstr (buf, "is down")) {
140 terminate (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 != UNKNOWN_PACKET_LOSS && loss > cpl)
157 status = STATE_CRITICAL;
158 else if (crta != UNKNOWN_TRIP_TIME && rta > crta)
159 status = STATE_CRITICAL;
160 else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
161 status = STATE_WARNING;
162 else if (wrta != UNKNOWN_TRIP_TIME && rta > wrta)
163 status = STATE_WARNING;
164 else
165 status = STATE_OK;
166 terminate (status, "FPING %s - %s (loss=%f%%, rta=%f ms)\n",
167 state_text (status), server_name, loss, rta);
169 }
170 else if(strstr (buf, "xmt/rcv/%loss") ) {
171 /* no min/max/avg if host was unreachable in fping v2.2.b1 */
172 losstr = strstr (buf, "=");
173 losstr = 1 + strstr (losstr, "/");
174 losstr = 1 + strstr (losstr, "/");
175 loss = strtod (losstr, NULL);
176 if (loss == 100)
177 status = STATE_CRITICAL;
178 else if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
179 status = STATE_CRITICAL;
180 else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
181 status = STATE_WARNING;
182 else
183 status = STATE_OK;
185 terminate (status, "FPING %s - %s (loss=%f%% )\n",
186 state_text (status), server_name, loss );
188 }
189 else {
190 status = max_state (status, STATE_WARNING);
191 }
193 return status;
194 }
199 /* process command-line arguments */
200 int
201 process_arguments (int argc, char **argv)
202 {
203 int c;
204 char *rv[2];
206 #ifdef HAVE_GETOPT_H
207 int option_index = 0;
208 static struct option long_options[] = {
209 {"hostname", required_argument, 0, 'H'},
210 {"critical", required_argument, 0, 'c'},
211 {"warning", required_argument, 0, 'w'},
212 {"bytes", required_argument, 0, 'b'},
213 {"number", required_argument, 0, 'n'},
214 {"verbose", no_argument, 0, 'v'},
215 {"version", no_argument, 0, 'V'},
216 {"help", no_argument, 0, 'h'},
217 {0, 0, 0, 0}
218 };
219 #endif
221 rv[PL] = NULL;
222 rv[RTA] = NULL;
224 if (argc < 2)
225 return ERROR;
227 if (!is_option (argv[1])) {
228 server_name = argv[1];
229 argv[1] = argv[0];
230 argv = &argv[1];
231 argc--;
232 }
234 while (1) {
235 #ifdef HAVE_GETOPT_H
236 c =
237 getopt_long (argc, argv, "+hVvH:c:w:b:n:", long_options, &option_index);
238 #else
239 c = getopt (argc, argv, "+hVvH:c:w:b:n:");
240 #endif
242 if (c == -1 || c == EOF || c == 1)
243 break;
245 switch (c) {
246 case '?': /* print short usage statement if args not parsable */
247 printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
248 print_usage ();
249 exit (STATE_UNKNOWN);
250 case 'h': /* help */
251 print_help ();
252 exit (STATE_OK);
253 case 'V': /* version */
254 print_revision (my_basename (argv[0]), "$Revision$");
255 exit (STATE_OK);
256 case 'v': /* verbose mode */
257 verbose = TRUE;
258 break;
259 case 'H': /* hostname */
260 if (is_host (optarg) == FALSE) {
261 printf ("Invalid host name/address\n\n");
262 print_usage ();
263 exit (STATE_UNKNOWN);
264 }
265 server_name = strscpy (server_name, optarg);
266 break;
267 case 'c':
268 get_threshold (optarg, rv);
269 if (rv[RTA]) {
270 crta = strtod (rv[RTA], NULL);
271 rv[RTA] = NULL;
272 }
273 if (rv[PL]) {
274 cpl = atoi (rv[PL]);
275 rv[PL] = NULL;
276 }
277 break;
278 case 'w':
279 get_threshold (optarg, rv);
280 if (rv[RTA]) {
281 wrta = strtod (rv[RTA], NULL);
282 rv[RTA] = NULL;
283 }
284 if (rv[PL]) {
285 wpl = atoi (rv[PL]);
286 rv[PL] = NULL;
287 }
288 break;
289 case 'b': /* bytes per packet */
290 if (is_intpos (optarg))
291 packet_size = atoi (optarg);
292 else
293 usage ("Packet size must be a positive integer");
294 break;
295 case 'n': /* number of packets */
296 if (is_intpos (optarg))
297 packet_count = atoi (optarg);
298 else
299 usage ("Packet count must be a positive integer");
300 break;
301 }
302 }
305 if (server_name == NULL)
306 usage ("Host name was not supplied\n\n");
308 return OK;
309 }
315 int
316 get_threshold (char *arg, char *rv[2])
317 {
318 char *arg1 = NULL;
319 char *arg2 = NULL;
321 arg1 = strscpy (arg1, arg);
322 if (strpbrk (arg1, ",:"))
323 arg2 = 1 + strpbrk (arg1, ",:");
325 if (arg2) {
326 arg1[strcspn (arg1, ",:")] = 0;
327 if (strstr (arg1, "%") && strstr (arg2, "%"))
328 terminate (STATE_UNKNOWN,
329 "%s: Only one threshold may be packet loss (%s)\n", PROGNAME,
330 arg);
331 if (!strstr (arg1, "%") && !strstr (arg2, "%"))
332 terminate (STATE_UNKNOWN,
333 "%s: Only one threshold must be packet loss (%s)\n",
334 PROGNAME, arg);
335 }
337 if (arg2 && strstr (arg2, "%")) {
338 rv[PL] = arg2;
339 rv[RTA] = arg1;
340 }
341 else if (arg2) {
342 rv[PL] = arg1;
343 rv[RTA] = arg2;
344 }
345 else if (strstr (arg1, "%")) {
346 rv[PL] = arg1;
347 }
348 else {
349 rv[RTA] = arg1;
350 }
352 return OK;
353 }
359 void
360 print_usage (void)
361 {
362 printf ("Usage: %s <host_address>\n", PROGNAME);
363 }
369 void
370 print_help (void)
371 {
373 print_revision (PROGNAME, "$Revision$");
375 printf
376 ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n\n"
377 "This plugin will use the /bin/fping command (from saint) to ping the\n"
378 "specified host for a fast check if the host is alive. Note that it is\n"
379 "necessary to set the suid flag on fping.\n\n");
381 print_usage ();
383 printf
384 ("\nOptions:\n"
385 "-H, --hostname=HOST\n"
386 " Name or IP Address of host to ping (IP Address bypasses name lookup,\n"
387 " reducing system load)\n"
388 "-w, --warning=THRESHOLD\n"
389 " warning threshold pair\n"
390 "-c, --critical=THRESHOLD\n"
391 " critical threshold pair\n"
392 "-b, --bytes=INTEGER\n"
393 " Size of ICMP packet (default: %d)\n"
394 "-n, --number=INTEGER\n"
395 " Number of ICMP packets to send (default: %d)\n"
396 "-v, --verbose\n"
397 " Show details for command-line debugging (do not use with nagios server)\n"
398 "-h, --help\n"
399 " Print this help screen\n"
400 "-V, --version\n"
401 " Print version information\n"
402 "THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n"
403 "time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n"
404 "percentage of packet loss to trigger an alarm state.\n",
405 PACKET_SIZE, PACKET_COUNT);
406 }