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 (from 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 const char *progname = "check_fping";
31 const char *revision = "$Revision$";
32 const char *copyright = "1999-2003";
33 const char *email = "nagiosplug-devel@lists.sourceforge.net";
35 #include "common.h"
36 #include "popen.h"
37 #include "netutils.h"
38 #include "utils.h"
40 #define PACKET_COUNT 1
41 #define PACKET_SIZE 56
42 #define UNKNOWN_PACKET_LOSS 200 /* 200% */
43 #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
45 #define PL 0
46 #define RTA 1
48 int textscan (char *buf);
49 int process_arguments (int, char **);
50 int get_threshold (char *arg, char *rv[2]);
51 void print_help (void);
52 void print_usage (void);
54 char *server_name = NULL;
55 int cpl = UNKNOWN_PACKET_LOSS;
56 int wpl = UNKNOWN_PACKET_LOSS;
57 double crta = UNKNOWN_TRIP_TIME;
58 double wrta = UNKNOWN_TRIP_TIME;
59 int packet_size = PACKET_SIZE;
60 int packet_count = PACKET_COUNT;
61 int verbose = FALSE;
63 int
64 main (int argc, char **argv)
65 {
66 int status = STATE_UNKNOWN;
67 char *server = NULL;
68 char *command_line = NULL;
69 char *input_buffer = NULL;
70 input_buffer = malloc (MAX_INPUT_BUFFER);
72 if (process_arguments (argc, argv) == ERROR)
73 usage (_("Could not parse arguments\n"));
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 (_("Unable to 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;
119 }
124 int
125 textscan (char *buf)
126 {
127 char *rtastr = NULL;
128 char *losstr = NULL;
129 double loss;
130 double rta;
131 int status = STATE_UNKNOWN;
133 if (strstr (buf, "not found")) {
134 die (STATE_CRITICAL, _("FPING unknown - %s not found\n"), server_name);
136 }
137 else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
138 die (STATE_CRITICAL, _("FPING critical - %s is unreachable\n"),
139 "host");
141 }
142 else if (strstr (buf, "is down")) {
143 die (STATE_CRITICAL, _("FPING critical - %s is down\n"), server_name);
145 }
146 else if (strstr (buf, "is alive")) {
147 status = STATE_OK;
149 }
150 else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
151 losstr = strstr (buf, "=");
152 losstr = 1 + strstr (losstr, "/");
153 losstr = 1 + strstr (losstr, "/");
154 rtastr = strstr (buf, "min/avg/max");
155 rtastr = strstr (rtastr, "=");
156 rtastr = 1 + index (rtastr, '/');
157 loss = strtod (losstr, NULL);
158 rta = strtod (rtastr, NULL);
159 if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
160 status = STATE_CRITICAL;
161 else if (crta <= 0 && rta > crta)
162 status = STATE_CRITICAL;
163 else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
164 status = STATE_WARNING;
165 else if (wrta >= 0 && rta > wrta)
166 status = STATE_WARNING;
167 else
168 status = STATE_OK;
169 die (status, _("FPING %s - %s (loss=%f%%, rta=%f ms)\n"),
170 state_text (status), server_name, loss, rta);
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 != UNKNOWN_PACKET_LOSS && loss > cpl)
182 status = STATE_CRITICAL;
183 else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
184 status = STATE_WARNING;
185 else
186 status = STATE_OK;
188 die (status, _("FPING %s - %s (loss=%f%% )\n"),
189 state_text (status), server_name, loss );
191 }
192 else {
193 status = max_state (status, STATE_WARNING);
194 }
196 return status;
197 }
198 \f
202 /* process command-line arguments */
203 int
204 process_arguments (int argc, char **argv)
205 {
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 printf (_("Invalid host name/address\n\n"));
258 print_usage ();
259 exit (STATE_UNKNOWN);
260 }
261 server_name = strscpy (server_name, optarg);
262 break;
263 case 'c':
264 get_threshold (optarg, rv);
265 if (rv[RTA]) {
266 crta = strtod (rv[RTA], NULL);
267 rv[RTA] = NULL;
268 }
269 if (rv[PL]) {
270 cpl = atoi (rv[PL]);
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 rv[RTA] = NULL;
279 }
280 if (rv[PL]) {
281 wpl = atoi (rv[PL]);
282 rv[PL] = NULL;
283 }
284 break;
285 case 'b': /* bytes per packet */
286 if (is_intpos (optarg))
287 packet_size = atoi (optarg);
288 else
289 usage (_("Packet size must be a positive integer"));
290 break;
291 case 'n': /* number of packets */
292 if (is_intpos (optarg))
293 packet_count = atoi (optarg);
294 else
295 usage (_("Packet count must be a positive integer"));
296 break;
297 }
298 }
301 if (server_name == NULL)
302 usage (_("Host name was not supplied\n\n"));
304 return OK;
305 }
311 int
312 get_threshold (char *arg, char *rv[2])
313 {
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;
349 }
355 \f
356 void
357 print_help (void)
358 {
360 print_revision (progname, "$Revision$");
362 printf (_("\
363 Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n\n\
364 This plugin will use the /bin/fping command (from saint) to ping the\n\
365 specified host for a fast check if the host is alive. Note that it is\n\
366 necessary to set the suid flag on fping.\n\n"));
368 print_usage ();
370 printf (_(UT_HELP_VRSN));
372 printf (_("\
373 -H, --hostname=HOST\n\
374 Name or IP Address of host to ping (IP Address bypasses name lookup,\n\
375 reducing system load)\n\
376 -w, --warning=THRESHOLD\n\
377 warning threshold pair\n\
378 -c, --critical=THRESHOLD\n\
379 critical threshold pair\n\
380 -b, --bytes=INTEGER\n\
381 Size of ICMP packet (default: %d)\n\
382 -n, --number=INTEGER\n\
383 Number of ICMP packets to send (default: %d)\n"),
384 PACKET_SIZE, PACKET_COUNT);
386 printf (_(UT_VERBOSE));
388 printf (_("\n\
389 THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
390 time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
391 percentage of packet loss to trigger an alarm state.\n"));
393 printf (_(UT_SUPPORT));
394 }
399 void
400 print_usage (void)
401 {
402 printf (_("Usage: %s <host_address>\n"), progname);
403 printf (_(UT_HLP_VRS), progname, progname);
404 }