0753cf3d1ab8122be6091dd41104c1a8ef587ccb
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 setlocale (LC_ALL, "");
73 bindtextdomain (PACKAGE, LOCALEDIR);
74 textdomain (PACKAGE);
76 if (process_arguments (argc, argv) == ERROR)
77 usage (_("Could not parse arguments\n"));
79 server = strscpy (server, server_name);
81 /* compose the command */
82 asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
83 packet_size, packet_count, server);
85 if (verbose)
86 printf ("%s\n", command_line);
88 /* run the command */
89 child_process = spopen (command_line);
90 if (child_process == NULL) {
91 printf (_("Unable to open pipe: %s\n"), command_line);
92 return STATE_UNKNOWN;
93 }
95 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
96 if (child_stderr == NULL) {
97 printf (_("Could not open stderr for %s\n"), command_line);
98 }
100 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
101 if (verbose)
102 printf ("%s", input_buffer);
103 status = max_state (status, textscan (input_buffer));
104 }
106 /* If we get anything on STDERR, at least set warning */
107 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
108 status = max_state (status, STATE_WARNING);
109 if (verbose)
110 printf ("%s", input_buffer);
111 status = max_state (status, textscan (input_buffer));
112 }
113 (void) fclose (child_stderr);
115 /* close the pipe */
116 if (spclose (child_process))
117 /* need to use max_state not max */
118 status = max_state (status, STATE_WARNING);
120 printf ("FPING %s - %s\n", state_text (status), server_name);
122 return status;
123 }
128 int
129 textscan (char *buf)
130 {
131 char *rtastr = NULL;
132 char *losstr = NULL;
133 double loss;
134 double rta;
135 int status = STATE_UNKNOWN;
137 if (strstr (buf, "not found")) {
138 die (STATE_CRITICAL, _("FPING unknown - %s not found\n"), server_name);
140 }
141 else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
142 die (STATE_CRITICAL, _("FPING critical - %s is unreachable\n"),
143 "host");
145 }
146 else if (strstr (buf, "is down")) {
147 die (STATE_CRITICAL, _("FPING critical - %s is down\n"), server_name);
149 }
150 else if (strstr (buf, "is alive")) {
151 status = STATE_OK;
153 }
154 else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
155 losstr = strstr (buf, "=");
156 losstr = 1 + strstr (losstr, "/");
157 losstr = 1 + strstr (losstr, "/");
158 rtastr = strstr (buf, "min/avg/max");
159 rtastr = strstr (rtastr, "=");
160 rtastr = 1 + index (rtastr, '/');
161 loss = strtod (losstr, NULL);
162 rta = strtod (rtastr, NULL);
163 if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
164 status = STATE_CRITICAL;
165 else if (crta <= 0 && rta > crta)
166 status = STATE_CRITICAL;
167 else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
168 status = STATE_WARNING;
169 else if (wrta >= 0 && rta > wrta)
170 status = STATE_WARNING;
171 else
172 status = STATE_OK;
173 die (status, _("FPING %s - %s (loss=%f%%, rta=%f ms)\n"),
174 state_text (status), server_name, loss, rta);
176 }
177 else if(strstr (buf, "xmt/rcv/%loss") ) {
178 /* no min/max/avg if host was unreachable in fping v2.2.b1 */
179 losstr = strstr (buf, "=");
180 losstr = 1 + strstr (losstr, "/");
181 losstr = 1 + strstr (losstr, "/");
182 loss = strtod (losstr, NULL);
183 if (atoi(losstr) == 100)
184 status = STATE_CRITICAL;
185 else if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
186 status = STATE_CRITICAL;
187 else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
188 status = STATE_WARNING;
189 else
190 status = STATE_OK;
192 die (status, _("FPING %s - %s (loss=%f%% )\n"),
193 state_text (status), server_name, loss );
195 }
196 else {
197 status = max_state (status, STATE_WARNING);
198 }
200 return status;
201 }
202 \f
206 /* process command-line arguments */
207 int
208 process_arguments (int argc, char **argv)
209 {
210 int c;
211 char *rv[2];
213 int option = 0;
214 static struct option longopts[] = {
215 {"hostname", required_argument, 0, 'H'},
216 {"critical", required_argument, 0, 'c'},
217 {"warning", required_argument, 0, 'w'},
218 {"bytes", required_argument, 0, 'b'},
219 {"number", required_argument, 0, 'n'},
220 {"verbose", no_argument, 0, 'v'},
221 {"version", no_argument, 0, 'V'},
222 {"help", no_argument, 0, 'h'},
223 {0, 0, 0, 0}
224 };
226 rv[PL] = NULL;
227 rv[RTA] = NULL;
229 if (argc < 2)
230 return ERROR;
232 if (!is_option (argv[1])) {
233 server_name = argv[1];
234 argv[1] = argv[0];
235 argv = &argv[1];
236 argc--;
237 }
239 while (1) {
240 c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", longopts, &option);
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"), progname, 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 (progname, 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 die (STATE_UNKNOWN,
329 _("%s: Only one threshold may be packet loss (%s)\n"), progname,
330 arg);
331 if (!strstr (arg1, "%") && !strstr (arg2, "%"))
332 die (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 \f
360 void
361 print_help (void)
362 {
364 print_revision (progname, "$Revision$");
366 printf (_("\
367 Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n\n\
368 This plugin will use the /bin/fping command (from saint) to ping the\n\
369 specified host for a fast check if the host is alive. Note that it is\n\
370 necessary to set the suid flag on fping.\n\n"));
372 print_usage ();
374 printf (_(UT_HELP_VRSN));
376 printf (_("\
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 PACKET_SIZE, PACKET_COUNT);
390 printf (_(UT_VERBOSE));
392 printf (_("\n\
393 THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
394 time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
395 percentage of packet loss to trigger an alarm state.\n"));
397 printf (_(UT_SUPPORT));
398 }
403 void
404 print_usage (void)
405 {
406 printf (_("Usage: %s <host_address>\n"), progname);
407 printf (_(UT_HLP_VRS), progname, progname);
408 }