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 ((size_t)max_addr);
79 if (process_arguments (argc, argv) == ERROR)
80 usage (_("Could not parse arguments"));
82 /* Set signal handling and alarm */
83 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
84 printf (_("Cannot catch SIGALRM"));
85 return STATE_UNKNOWN;
86 }
88 /* handle timeouts gracefully */
89 alarm (timeout_interval);
91 for (i = 0 ; i < n_addresses ; i++) {
93 /* does the host address of number of packets argument come first? */
94 #ifdef PING6_COMMAND
95 # ifdef PING_PACKETS_FIRST
96 if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
97 asprintf (&cmd, PING6_COMMAND, max_packets, addresses[i]);
98 else
99 asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]);
100 # else
101 if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
102 asprintf (&cmd, PING6_COMMAND, addresses[i], max_packets);
103 else
104 asprintf (&cmd, PING_COMMAND, addresses[i], max_packets);
105 # endif
106 #else /* USE_IPV6 */
107 # ifdef PING_PACKETS_FIRST
108 asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]);
109 # else
110 asprintf (&cmd, PING_COMMAND, addresses[i], max_packets);
111 # endif
112 #endif /* USE_IPV6 */
114 if (verbose)
115 printf ("%s ==> ", cmd);
117 /* run the command */
118 this_result = run_ping (cmd, addresses[i]);
120 if (pl == UNKNOWN_PACKET_LOSS || rta < 0.0) {
121 printf ("%s\n", cmd);
122 die (STATE_UNKNOWN,
123 _("Error: Could not interpret output from ping command\n"));
124 }
126 if (pl >= cpl || rta >= crta || rta < 0)
127 this_result = STATE_CRITICAL;
128 else if (pl >= wpl || rta >= wrta)
129 this_result = STATE_WARNING;
130 else if (pl >= 0 && rta >= 0)
131 this_result = max_state (STATE_OK, this_result);
133 if (n_addresses > 1 && this_result != STATE_UNKNOWN)
134 die (STATE_OK, "%s is alive\n", addresses[i]);
136 if (display_html == TRUE)
137 printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]);
138 if (pl == 100)
139 printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text,
140 pl);
141 else
142 printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"),
143 state_text (this_result), warn_text, pl, rta);
144 if (display_html == TRUE)
145 printf ("</A>");
146 printf ("\n");
148 if (verbose)
149 printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
151 result = max_state (result, this_result);
153 }
155 return result;
156 }
162 \f
163 /* process command-line arguments */
164 int
165 process_arguments (int argc, char **argv)
166 {
167 int c = 1;
168 char *ptr;
170 int option = 0;
171 static struct option longopts[] = {
172 STD_LONG_OPTS,
173 {"packets", required_argument, 0, 'p'},
174 {"nohtml", no_argument, 0, 'n'},
175 {"link", no_argument, 0, 'L'},
176 {"use-ipv4", no_argument, 0, '4'},
177 {"use-ipv6", no_argument, 0, '6'},
178 {0, 0, 0, 0}
179 };
181 if (argc < 2)
182 return ERROR;
184 for (c = 1; c < argc; c++) {
185 if (strcmp ("-to", argv[c]) == 0)
186 strcpy (argv[c], "-t");
187 if (strcmp ("-nohtml", argv[c]) == 0)
188 strcpy (argv[c], "-n");
189 }
191 while (1) {
192 c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
194 if (c == -1 || c == EOF)
195 break;
197 switch (c) {
198 case '?': /* usage */
199 usage3 (_("Unknown argument"), optopt);
200 break;
201 case 'h': /* help */
202 print_help ();
203 exit (STATE_OK);
204 break;
205 case 'V': /* version */
206 print_revision (progname, revision);
207 exit (STATE_OK);
208 break;
209 case 't': /* timeout period */
210 timeout_interval = atoi (optarg);
211 break;
212 case 'v': /* verbose mode */
213 verbose = TRUE;
214 break;
215 case '4': /* IPv4 only */
216 address_family = AF_INET;
217 break;
218 case '6': /* IPv6 only */
219 #ifdef USE_IPV6
220 address_family = AF_INET6;
221 #else
222 usage (_("IPv6 support not available\n"));
223 #endif
224 break;
225 case 'H': /* hostname */
226 ptr=optarg;
227 while (1) {
228 n_addresses++;
229 if (n_addresses > max_addr) {
230 max_addr *= 2;
231 addresses = realloc (addresses, (size_t)max_addr);
232 if (addresses == NULL)
233 die (STATE_UNKNOWN, _("Could not realloc() addresses\n"));
234 }
235 addresses[n_addresses-1] = ptr;
236 if ((ptr = index (ptr, ','))) {
237 strcpy (ptr, "");
238 ptr += sizeof(char);
239 } else {
240 break;
241 }
242 }
243 break;
244 case 'p': /* number of packets to send */
245 if (is_intnonneg (optarg))
246 max_packets = atoi (optarg);
247 else
248 usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg);
249 break;
250 case 'n': /* no HTML */
251 display_html = FALSE;
252 break;
253 case 'L': /* show HTML */
254 display_html = TRUE;
255 break;
256 case 'c':
257 get_threshold (optarg, &crta, &cpl);
258 break;
259 case 'w':
260 get_threshold (optarg, &wrta, &wpl);
261 break;
262 }
263 }
265 c = optind;
266 if (c == argc)
267 return validate_arguments ();
269 if (addresses[0] == NULL) {
270 if (is_host (argv[c]) == FALSE) {
271 printf (_("Invalid host name/address: %s\n\n"), argv[c]);
272 return ERROR;
273 } else {
274 addresses[0] = argv[c++];
275 if (c == argc)
276 return validate_arguments ();
277 }
278 }
280 if (wpl == UNKNOWN_PACKET_LOSS) {
281 if (is_intpercent (argv[c]) == FALSE) {
282 printf (_("<wpl> (%s) must be an integer percentage\n"), argv[c]);
283 return ERROR;
284 } else {
285 wpl = atoi (argv[c++]);
286 if (c == argc)
287 return validate_arguments ();
288 }
289 }
291 if (cpl == UNKNOWN_PACKET_LOSS) {
292 if (is_intpercent (argv[c]) == FALSE) {
293 printf (_("<cpl> (%s) must be an integer percentage\n"), argv[c]);
294 return ERROR;
295 } else {
296 cpl = atoi (argv[c++]);
297 if (c == argc)
298 return validate_arguments ();
299 }
300 }
302 if (wrta < 0.0) {
303 if (is_negative (argv[c])) {
304 printf (_("<wrta> (%s) must be a non-negative number\n"), argv[c]);
305 return ERROR;
306 } else {
307 wrta = atof (argv[c++]);
308 if (c == argc)
309 return validate_arguments ();
310 }
311 }
313 if (crta < 0.0) {
314 if (is_negative (argv[c])) {
315 printf (_("<crta> (%s) must be a non-negative number\n"), argv[c]);
316 return ERROR;
317 } else {
318 crta = atof (argv[c++]);
319 if (c == argc)
320 return validate_arguments ();
321 }
322 }
324 if (max_packets == -1) {
325 if (is_intnonneg (argv[c])) {
326 max_packets = atoi (argv[c++]);
327 } else {
328 printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]);
329 return ERROR;
330 }
331 }
333 return validate_arguments ();
334 }
336 int
337 get_threshold (char *arg, float *trta, int *tpl)
338 {
339 if (is_intnonneg (arg) && sscanf (arg, "%f", trta) == 1)
340 return OK;
341 else if (strpbrk (arg, ",:") && strstr (arg, "%") && sscanf (arg, "%f%*[:,]%d%%", trta, tpl) == 2)
342 return OK;
343 else if (strstr (arg, "%") && sscanf (arg, "%d%%", tpl) == 1)
344 return OK;
346 usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
347 return STATE_UNKNOWN;
348 }
350 int
351 validate_arguments ()
352 {
353 float max_seconds;
354 int i;
356 if (wrta < 0.0) {
357 printf (_("<wrta> was not set\n"));
358 return ERROR;
359 }
360 else if (crta < 0.0) {
361 printf (_("<crta> was not set\n"));
362 return ERROR;
363 }
364 else if (wpl == UNKNOWN_PACKET_LOSS) {
365 printf (_("<wpl> was not set\n"));
366 return ERROR;
367 }
368 else if (cpl == UNKNOWN_PACKET_LOSS) {
369 printf (_("<cpl> was not set\n"));
370 return ERROR;
371 }
372 else if (wrta > crta) {
373 printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta, crta);
374 return ERROR;
375 }
376 else if (wpl > cpl) {
377 printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl, cpl);
378 return ERROR;
379 }
381 if (max_packets == -1)
382 max_packets = DEFAULT_MAX_PACKETS;
384 max_seconds = crta / 1000.0 * max_packets + max_packets;
385 if (max_seconds > timeout_interval)
386 timeout_interval = (int)max_seconds;
388 for (i=0; i<n_addresses; i++) {
389 if (is_host(addresses[i]) == FALSE)
390 usage2 (_("Invalid host name/address"), addresses[i]);
391 }
393 return OK;
394 }
400 \f
401 int
402 run_ping (const char *cmd, const char *addr)
403 {
404 char buf[MAX_INPUT_BUFFER];
405 int result = STATE_UNKNOWN;
407 if ((child_process = spopen (cmd)) == NULL)
408 die (STATE_UNKNOWN, _("Cannot open pipe: %s"), cmd);
410 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
411 if (child_stderr == NULL)
412 printf (_("Cannot open stderr for %s\n"), cmd);
414 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
416 result = max_state (result, error_scan (buf, addr));
418 /* get the percent loss statistics */
419 if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 ||
420 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 ||
421 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 ||
422 sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1 ||
423 sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl) == 1)
424 continue;
426 /* get the round trip average */
427 else
428 if(sscanf(buf,"round-trip min/avg/max = %*f/%f/%*f",&rta)==1 ||
429 sscanf(buf,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta)==1 ||
430 sscanf(buf,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta)==1 ||
431 sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
432 sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta)==1 ||
433 sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta)==1 ||
434 sscanf(buf,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta)==1)
435 continue;
436 }
438 /* this is needed because there is no rta if all packets are lost */
439 if (pl == 100)
440 rta = crta;
442 /* check stderr, setting at least WARNING if there is output here */
443 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
444 if (! strstr(buf,"Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP"))
445 result = max_state (STATE_WARNING, error_scan (buf, addr));
447 (void) fclose (child_stderr);
450 /* close the pipe - WARNING if status is set */
451 if (spclose (child_process))
452 result = max_state (result, STATE_WARNING);
454 if (warn_text == NULL)
455 warn_text = strdup("");
457 return result;
458 }
464 int
465 error_scan (char buf[MAX_INPUT_BUFFER], const char *addr)
466 {
467 if (strstr (buf, "Network is unreachable"))
468 die (STATE_CRITICAL, _("PING CRITICAL - Network unreachable (%s)"), addr);
469 else if (strstr (buf, "Destination Host Unreachable"))
470 die (STATE_CRITICAL, _("PING CRITICAL - Host Unreachable (%s)"), addr);
471 else if (strstr (buf, "unknown host" ))
472 die (STATE_CRITICAL, _("PING CRITICAL - Host not found (%s)"), addr);
474 if (strstr (buf, "(DUP!)") || strstr (buf, "DUPLICATES FOUND")) {
475 if (warn_text == NULL)
476 warn_text = strdup (_(WARN_DUPLICATES));
477 else if (! strstr (warn_text, _(WARN_DUPLICATES)) &&
478 asprintf (&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1)
479 die (STATE_UNKNOWN, _("unable to realloc warn_text"));
480 return (STATE_WARNING);
481 }
483 return (STATE_OK);
484 }
490 \f
491 void
492 print_usage (void)
493 {
494 printf (\
495 "Usage: %s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n\
496 [-p packets] [-t timeout] [-L] [-4|-6]\n", progname);
497 printf (_(UT_HLP_VRS), progname, progname);
498 }
500 void
501 print_help (void)
502 {
503 print_revision (progname, revision);
505 printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>"));
506 printf (_(COPYRIGHT), copyright, email);
508 printf (_("Use ping to check connection statistics for a remote host.\n\n"));
510 print_usage ();
512 printf (_(UT_HELP_VRSN));
514 printf (_(UT_IPv46));
516 printf (_("\
517 -H, --hostname=HOST\n\
518 host to ping\n\
519 -w, --warning=THRESHOLD\n\
520 warning threshold pair\n\
521 -c, --critical=THRESHOLD\n\
522 critical threshold pair\n\
523 -p, --packets=INTEGER\n\
524 number of ICMP ECHO packets to send (Default: %d)\n\
525 -L, --link\n\
526 show HTML in the plugin output (obsoleted by urlize)\n"),
527 DEFAULT_MAX_PACKETS);
529 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
531 printf (_("\
532 THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
533 time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
534 percentage of packet loss to trigger an alarm state.\n\n"));
536 printf (_("\
537 This plugin uses the ping command to probe the specified host for packet loss\n\
538 (percentage) and round trip average (milliseconds). It can produce HTML output\n\
539 linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in\n\
540 the contrib area of the downloads section at http://www.nagios.org\n\n"));
542 printf (_(UT_SUPPORT));
543 }