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 ******************************************************************************/
19 const char *progname = "check_ping";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "netutils.h"
26 #include "popen.h"
27 #include "utils.h"
29 #define WARN_DUPLICATES "DUPLICATES FOUND! "
30 #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
32 enum {
33 UNKNOWN_PACKET_LOSS = 200, /* 200% */
34 DEFAULT_MAX_PACKETS = 5 /* default no. of ICMP ECHO packets */
35 };
37 int process_arguments (int, char **);
38 int get_threshold (char *, float *, int *);
39 int validate_arguments (void);
40 int run_ping (const char *cmd, const char *addr);
41 int error_scan (char buf[MAX_INPUT_BUFFER], const char *addr);
42 void print_usage (void);
43 void print_help (void);
45 int display_html = FALSE;
46 int wpl = UNKNOWN_PACKET_LOSS;
47 int cpl = UNKNOWN_PACKET_LOSS;
48 float wrta = UNKNOWN_TRIP_TIME;
49 float crta = UNKNOWN_TRIP_TIME;
50 char **addresses = NULL;
51 int n_addresses;
52 int max_addr = 1;
53 int max_packets = -1;
54 int verbose = FALSE;
56 float rta = UNKNOWN_TRIP_TIME;
57 int pl = UNKNOWN_PACKET_LOSS;
59 char *warn_text;
64 \f
65 int
66 main (int argc, char **argv)
67 {
68 char *cmd = NULL;
69 int result = STATE_UNKNOWN;
70 int this_result = STATE_UNKNOWN;
71 int i;
73 setlocale (LC_ALL, "");
74 bindtextdomain (PACKAGE, LOCALEDIR);
75 textdomain (PACKAGE);
77 addresses = malloc (sizeof(char*) * max_addr);
78 addresses[0] = NULL;
80 if (process_arguments (argc, argv) == ERROR)
81 usage (_("Could not parse arguments"));
83 /* Set signal handling and alarm */
84 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
85 printf (_("Cannot catch SIGALRM"));
86 return STATE_UNKNOWN;
87 }
89 /* handle timeouts gracefully */
90 alarm (timeout_interval);
92 for (i = 0 ; i < n_addresses ; i++) {
94 /* does the host address of number of packets argument come first? */
95 #ifdef PING6_COMMAND
96 # ifdef PING_PACKETS_FIRST
97 if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
98 asprintf (&cmd, PING6_COMMAND, max_packets, addresses[i]);
99 else
100 asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]);
101 # else
102 if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
103 asprintf (&cmd, PING6_COMMAND, addresses[i], max_packets);
104 else
105 asprintf (&cmd, PING_COMMAND, addresses[i], max_packets);
106 # endif
107 #else /* USE_IPV6 */
108 # ifdef PING_PACKETS_FIRST
109 asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]);
110 # else
111 asprintf (&cmd, PING_COMMAND, addresses[i], max_packets);
112 # endif
113 #endif /* USE_IPV6 */
115 if (verbose)
116 printf ("%s ==> ", cmd);
118 /* run the command */
119 this_result = run_ping (cmd, addresses[i]);
121 if (pl == UNKNOWN_PACKET_LOSS || rta < 0.0) {
122 printf ("%s\n", cmd);
123 die (STATE_UNKNOWN,
124 _("Error: Could not interpret output from ping command\n"));
125 }
127 if (pl >= cpl || rta >= crta || rta < 0)
128 this_result = STATE_CRITICAL;
129 else if (pl >= wpl || rta >= wrta)
130 this_result = STATE_WARNING;
131 else if (pl >= 0 && rta >= 0)
132 this_result = max_state (STATE_OK, this_result);
134 if (n_addresses > 1 && this_result != STATE_UNKNOWN)
135 die (STATE_OK, "%s is alive\n", addresses[i]);
137 if (display_html == TRUE)
138 printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]);
139 if (pl == 100)
140 printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text,
141 pl);
142 else
143 printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"),
144 state_text (this_result), warn_text, pl, rta);
145 if (display_html == TRUE)
146 printf ("</A>");
147 printf ("\n");
149 if (verbose)
150 printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
152 result = max_state (result, this_result);
154 }
156 return result;
157 }
163 \f
164 /* process command-line arguments */
165 int
166 process_arguments (int argc, char **argv)
167 {
168 int c = 1;
169 char *ptr;
171 int option = 0;
172 static struct option longopts[] = {
173 STD_LONG_OPTS,
174 {"packets", required_argument, 0, 'p'},
175 {"nohtml", no_argument, 0, 'n'},
176 {"link", no_argument, 0, 'L'},
177 {"use-ipv4", no_argument, 0, '4'},
178 {"use-ipv6", no_argument, 0, '6'},
179 {0, 0, 0, 0}
180 };
182 if (argc < 2)
183 return ERROR;
185 for (c = 1; c < argc; c++) {
186 if (strcmp ("-to", argv[c]) == 0)
187 strcpy (argv[c], "-t");
188 if (strcmp ("-nohtml", argv[c]) == 0)
189 strcpy (argv[c], "-n");
190 }
192 while (1) {
193 c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
195 if (c == -1 || c == EOF)
196 break;
198 switch (c) {
199 case '?': /* usage */
200 usage3 (_("Unknown argument"), optopt);
201 break;
202 case 'h': /* help */
203 print_help ();
204 exit (STATE_OK);
205 break;
206 case 'V': /* version */
207 print_revision (progname, revision);
208 exit (STATE_OK);
209 break;
210 case 't': /* timeout period */
211 timeout_interval = atoi (optarg);
212 break;
213 case 'v': /* verbose mode */
214 verbose = TRUE;
215 break;
216 case '4': /* IPv4 only */
217 address_family = AF_INET;
218 break;
219 case '6': /* IPv6 only */
220 #ifdef USE_IPV6
221 address_family = AF_INET6;
222 #else
223 usage (_("IPv6 support not available\n"));
224 #endif
225 break;
226 case 'H': /* hostname */
227 ptr=optarg;
228 while (1) {
229 n_addresses++;
230 if (n_addresses > max_addr) {
231 max_addr *= 2;
232 addresses = realloc (addresses, sizeof(char*) * max_addr);
233 if (addresses == NULL)
234 die (STATE_UNKNOWN, _("Could not realloc() addresses\n"));
235 }
236 addresses[n_addresses-1] = ptr;
237 if ((ptr = index (ptr, ','))) {
238 strcpy (ptr, "");
239 ptr += sizeof(char);
240 } else {
241 break;
242 }
243 }
244 break;
245 case 'p': /* number of packets to send */
246 if (is_intnonneg (optarg))
247 max_packets = atoi (optarg);
248 else
249 usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg);
250 break;
251 case 'n': /* no HTML */
252 display_html = FALSE;
253 break;
254 case 'L': /* show HTML */
255 display_html = TRUE;
256 break;
257 case 'c':
258 get_threshold (optarg, &crta, &cpl);
259 break;
260 case 'w':
261 get_threshold (optarg, &wrta, &wpl);
262 break;
263 }
264 }
266 c = optind;
267 if (c == argc)
268 return validate_arguments ();
270 if (addresses[0] == NULL) {
271 if (is_host (argv[c]) == FALSE) {
272 printf (_("Invalid host name/address: %s\n\n"), argv[c]);
273 return ERROR;
274 } else {
275 addresses[0] = argv[c++];
276 n_addresses++;
277 if (c == argc)
278 return validate_arguments ();
279 }
280 }
282 if (wpl == UNKNOWN_PACKET_LOSS) {
283 if (is_intpercent (argv[c]) == FALSE) {
284 printf (_("<wpl> (%s) must be an integer percentage\n"), argv[c]);
285 return ERROR;
286 } else {
287 wpl = atoi (argv[c++]);
288 if (c == argc)
289 return validate_arguments ();
290 }
291 }
293 if (cpl == UNKNOWN_PACKET_LOSS) {
294 if (is_intpercent (argv[c]) == FALSE) {
295 printf (_("<cpl> (%s) must be an integer percentage\n"), argv[c]);
296 return ERROR;
297 } else {
298 cpl = atoi (argv[c++]);
299 if (c == argc)
300 return validate_arguments ();
301 }
302 }
304 if (wrta < 0.0) {
305 if (is_negative (argv[c])) {
306 printf (_("<wrta> (%s) must be a non-negative number\n"), argv[c]);
307 return ERROR;
308 } else {
309 wrta = atof (argv[c++]);
310 if (c == argc)
311 return validate_arguments ();
312 }
313 }
315 if (crta < 0.0) {
316 if (is_negative (argv[c])) {
317 printf (_("<crta> (%s) must be a non-negative number\n"), argv[c]);
318 return ERROR;
319 } else {
320 crta = atof (argv[c++]);
321 if (c == argc)
322 return validate_arguments ();
323 }
324 }
326 if (max_packets == -1) {
327 if (is_intnonneg (argv[c])) {
328 max_packets = atoi (argv[c++]);
329 } else {
330 printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]);
331 return ERROR;
332 }
333 }
335 return validate_arguments ();
336 }
338 int
339 get_threshold (char *arg, float *trta, int *tpl)
340 {
341 if (is_intnonneg (arg) && sscanf (arg, "%f", trta) == 1)
342 return OK;
343 else if (strpbrk (arg, ",:") && strstr (arg, "%") && sscanf (arg, "%f%*[:,]%d%%", trta, tpl) == 2)
344 return OK;
345 else if (strstr (arg, "%") && sscanf (arg, "%d%%", tpl) == 1)
346 return OK;
348 usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
349 return STATE_UNKNOWN;
350 }
352 int
353 validate_arguments ()
354 {
355 float max_seconds;
356 int i;
358 if (wrta < 0.0) {
359 printf (_("<wrta> was not set\n"));
360 return ERROR;
361 }
362 else if (crta < 0.0) {
363 printf (_("<crta> was not set\n"));
364 return ERROR;
365 }
366 else if (wpl == UNKNOWN_PACKET_LOSS) {
367 printf (_("<wpl> was not set\n"));
368 return ERROR;
369 }
370 else if (cpl == UNKNOWN_PACKET_LOSS) {
371 printf (_("<cpl> was not set\n"));
372 return ERROR;
373 }
374 else if (wrta > crta) {
375 printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta, crta);
376 return ERROR;
377 }
378 else if (wpl > cpl) {
379 printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl, cpl);
380 return ERROR;
381 }
383 if (max_packets == -1)
384 max_packets = DEFAULT_MAX_PACKETS;
386 max_seconds = crta / 1000.0 * max_packets + max_packets;
387 if (max_seconds > timeout_interval)
388 timeout_interval = (int)max_seconds;
390 for (i=0; i<n_addresses; i++) {
391 if (is_host(addresses[i]) == FALSE)
392 usage2 (_("Invalid host name/address"), addresses[i]);
393 }
395 return OK;
396 }
402 \f
403 int
404 run_ping (const char *cmd, const char *addr)
405 {
406 char buf[MAX_INPUT_BUFFER];
407 int result = STATE_UNKNOWN;
409 if ((child_process = spopen (cmd)) == NULL)
410 die (STATE_UNKNOWN, _("Cannot open pipe: %s"), cmd);
412 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
413 if (child_stderr == NULL)
414 printf (_("Cannot open stderr for %s\n"), cmd);
416 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
418 result = max_state (result, error_scan (buf, addr));
420 /* get the percent loss statistics */
421 if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 ||
422 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 ||
423 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 ||
424 sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1 ||
425 sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl) == 1)
426 continue;
428 /* get the round trip average */
429 else
430 if(sscanf(buf,"round-trip min/avg/max = %*f/%f/%*f",&rta)==1 ||
431 sscanf(buf,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta)==1 ||
432 sscanf(buf,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta)==1 ||
433 sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
434 sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta)==1 ||
435 sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta)==1 ||
436 sscanf(buf,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta)==1)
437 continue;
438 }
440 /* this is needed because there is no rta if all packets are lost */
441 if (pl == 100)
442 rta = crta;
444 /* check stderr, setting at least WARNING if there is output here */
445 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
446 if (! strstr(buf,"Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP"))
447 result = max_state (STATE_WARNING, error_scan (buf, addr));
449 (void) fclose (child_stderr);
452 /* close the pipe - WARNING if status is set */
453 if (spclose (child_process))
454 result = max_state (result, STATE_WARNING);
456 if (warn_text == NULL)
457 warn_text = strdup("");
459 return result;
460 }
466 int
467 error_scan (char buf[MAX_INPUT_BUFFER], const char *addr)
468 {
469 if (strstr (buf, "Network is unreachable"))
470 die (STATE_CRITICAL, _("PING CRITICAL - Network unreachable (%s)"), addr);
471 else if (strstr (buf, "Destination Host Unreachable"))
472 die (STATE_CRITICAL, _("PING CRITICAL - Host Unreachable (%s)"), addr);
473 else if (strstr (buf, "unknown host" ))
474 die (STATE_CRITICAL, _("PING CRITICAL - Host not found (%s)"), addr);
476 if (strstr (buf, "(DUP!)") || strstr (buf, "DUPLICATES FOUND")) {
477 if (warn_text == NULL)
478 warn_text = strdup (_(WARN_DUPLICATES));
479 else if (! strstr (warn_text, _(WARN_DUPLICATES)) &&
480 asprintf (&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1)
481 die (STATE_UNKNOWN, _("unable to realloc warn_text"));
482 return (STATE_WARNING);
483 }
485 return (STATE_OK);
486 }
492 \f
493 void
494 print_usage (void)
495 {
496 printf (\
497 "Usage: %s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n\
498 [-p packets] [-t timeout] [-L] [-4|-6]\n", progname);
499 printf (_(UT_HLP_VRS), progname, progname);
500 }
502 void
503 print_help (void)
504 {
505 print_revision (progname, revision);
507 printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>"));
508 printf (_(COPYRIGHT), copyright, email);
510 printf (_("Use ping to check connection statistics for a remote host.\n\n"));
512 print_usage ();
514 printf (_(UT_HELP_VRSN));
516 printf (_(UT_IPv46));
518 printf (_("\
519 -H, --hostname=HOST\n\
520 host to ping\n\
521 -w, --warning=THRESHOLD\n\
522 warning threshold pair\n\
523 -c, --critical=THRESHOLD\n\
524 critical threshold pair\n\
525 -p, --packets=INTEGER\n\
526 number of ICMP ECHO packets to send (Default: %d)\n\
527 -L, --link\n\
528 show HTML in the plugin output (obsoleted by urlize)\n"),
529 DEFAULT_MAX_PACKETS);
531 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
533 printf (_("\
534 THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
535 time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
536 percentage of packet loss to trigger an alarm state.\n\n"));
538 printf (_("\
539 This plugin uses the ping command to probe the specified host for packet loss\n\
540 (percentage) and round trip average (milliseconds). It can produce HTML output\n\
541 linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in\n\
542 the contrib area of the downloads section at http://www.nagios.org\n\n"));
544 printf (_(UT_SUPPORT));
545 }