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 $Id$
19 ******************************************************************************/
21 const char *progname = "check_ping";
22 const char *revision = "$Revision$";
23 const char *copyright = "2000-2004";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "netutils.h"
28 #include "popen.h"
29 #include "utils.h"
31 #define WARN_DUPLICATES "DUPLICATES FOUND! "
32 #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
34 enum {
35 UNKNOWN_PACKET_LOSS = 200, /* 200% */
36 DEFAULT_MAX_PACKETS = 5 /* default no. of ICMP ECHO packets */
37 };
39 int process_arguments (int, char **);
40 int get_threshold (char *, float *, int *);
41 int validate_arguments (void);
42 int run_ping (const char *cmd, const char *addr);
43 int error_scan (char buf[MAX_INPUT_BUFFER], const char *addr);
44 void print_usage (void);
45 void print_help (void);
47 int display_html = FALSE;
48 int wpl = UNKNOWN_PACKET_LOSS;
49 int cpl = UNKNOWN_PACKET_LOSS;
50 float wrta = UNKNOWN_TRIP_TIME;
51 float crta = UNKNOWN_TRIP_TIME;
52 char **addresses = NULL;
53 int n_addresses;
54 int max_addr = 1;
55 int max_packets = -1;
56 int verbose = FALSE;
58 float rta = UNKNOWN_TRIP_TIME;
59 int pl = UNKNOWN_PACKET_LOSS;
61 char *warn_text;
65 int
66 main (int argc, char **argv)
67 {
68 char *cmd = NULL;
69 char *rawcmd = NULL;
70 int result = STATE_UNKNOWN;
71 int this_result = STATE_UNKNOWN;
72 int i;
74 setlocale (LC_ALL, "");
75 bindtextdomain (PACKAGE, LOCALEDIR);
76 textdomain (PACKAGE);
78 addresses = malloc (sizeof(char*) * max_addr);
79 addresses[0] = NULL;
81 if (process_arguments (argc, argv) == ERROR)
82 usage4 (_("Could not parse arguments"));
84 /* Set signal handling and alarm */
85 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
86 usage4 (_("Cannot catch SIGALRM"));
87 }
89 /* handle timeouts gracefully */
90 alarm (timeout_interval);
92 for (i = 0 ; i < n_addresses ; i++) {
94 #ifdef PING6_COMMAND
95 if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
96 rawcmd = strdup(PING6_COMMAND);
97 else
98 rawcmd = strdup(PING_COMMAND);
99 #else
100 rawcmd = strdup(PING_COMMAND);
101 #endif
103 /* does the host address of number of packets argument come first? */
104 #ifdef PING_PACKETS_FIRST
105 # ifdef PING_HAS_TIMEOUT
106 asprintf (&cmd, rawcmd, timeout_interval, max_packets, addresses[i]);
107 # else
108 asprintf (&cmd, rawcmd, max_packets, addresses[i]);
109 # endif
110 #else
111 asprintf (&cmd, rawcmd, addresses[i], max_packets);
112 #endif
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 _("CRITICAL - 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);
152 free (rawcmd);
153 free (cmd);
154 }
156 return result;
157 }
161 /* process command-line arguments */
162 int
163 process_arguments (int argc, char **argv)
164 {
165 int c = 1;
166 char *ptr;
168 int option = 0;
169 static struct option longopts[] = {
170 STD_LONG_OPTS,
171 {"packets", required_argument, 0, 'p'},
172 {"nohtml", no_argument, 0, 'n'},
173 {"link", no_argument, 0, 'L'},
174 {"use-ipv4", no_argument, 0, '4'},
175 {"use-ipv6", no_argument, 0, '6'},
176 {0, 0, 0, 0}
177 };
179 if (argc < 2)
180 return ERROR;
182 for (c = 1; c < argc; c++) {
183 if (strcmp ("-to", argv[c]) == 0)
184 strcpy (argv[c], "-t");
185 if (strcmp ("-nohtml", argv[c]) == 0)
186 strcpy (argv[c], "-n");
187 }
189 while (1) {
190 c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
192 if (c == -1 || c == EOF)
193 break;
195 switch (c) {
196 case '?': /* usage */
197 usage2 (_("Unknown argument"), optarg);
198 case 'h': /* help */
199 print_help ();
200 exit (STATE_OK);
201 break;
202 case 'V': /* version */
203 print_revision (progname, revision);
204 exit (STATE_OK);
205 break;
206 case 't': /* timeout period */
207 timeout_interval = atoi (optarg);
208 break;
209 case 'v': /* verbose mode */
210 verbose = TRUE;
211 break;
212 case '4': /* IPv4 only */
213 address_family = AF_INET;
214 break;
215 case '6': /* IPv6 only */
216 #ifdef USE_IPV6
217 address_family = AF_INET6;
218 #else
219 usage (_("IPv6 support not available\n"));
220 #endif
221 break;
222 case 'H': /* hostname */
223 ptr=optarg;
224 while (1) {
225 n_addresses++;
226 if (n_addresses > max_addr) {
227 max_addr *= 2;
228 addresses = realloc (addresses, sizeof(char*) * max_addr);
229 if (addresses == NULL)
230 die (STATE_UNKNOWN, _("Could not realloc() addresses\n"));
231 }
232 addresses[n_addresses-1] = ptr;
233 if ((ptr = index (ptr, ','))) {
234 strcpy (ptr, "");
235 ptr += sizeof(char);
236 } else {
237 break;
238 }
239 }
240 break;
241 case 'p': /* number of packets to send */
242 if (is_intnonneg (optarg))
243 max_packets = atoi (optarg);
244 else
245 usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg);
246 break;
247 case 'n': /* no HTML */
248 display_html = FALSE;
249 break;
250 case 'L': /* show HTML */
251 display_html = TRUE;
252 break;
253 case 'c':
254 get_threshold (optarg, &crta, &cpl);
255 break;
256 case 'w':
257 get_threshold (optarg, &wrta, &wpl);
258 break;
259 }
260 }
262 c = optind;
263 if (c == argc)
264 return validate_arguments ();
266 if (addresses[0] == NULL) {
267 if (is_host (argv[c]) == FALSE) {
268 usage2 (_("Invalid hostname/address"), argv[c]);
269 } else {
270 addresses[0] = argv[c++];
271 n_addresses++;
272 if (c == argc)
273 return validate_arguments ();
274 }
275 }
277 if (wpl == UNKNOWN_PACKET_LOSS) {
278 if (is_intpercent (argv[c]) == FALSE) {
279 printf (_("<wpl> (%s) must be an integer percentage\n"), argv[c]);
280 return ERROR;
281 } else {
282 wpl = atoi (argv[c++]);
283 if (c == argc)
284 return validate_arguments ();
285 }
286 }
288 if (cpl == UNKNOWN_PACKET_LOSS) {
289 if (is_intpercent (argv[c]) == FALSE) {
290 printf (_("<cpl> (%s) must be an integer percentage\n"), argv[c]);
291 return ERROR;
292 } else {
293 cpl = atoi (argv[c++]);
294 if (c == argc)
295 return validate_arguments ();
296 }
297 }
299 if (wrta < 0.0) {
300 if (is_negative (argv[c])) {
301 printf (_("<wrta> (%s) must be a non-negative number\n"), argv[c]);
302 return ERROR;
303 } else {
304 wrta = atof (argv[c++]);
305 if (c == argc)
306 return validate_arguments ();
307 }
308 }
310 if (crta < 0.0) {
311 if (is_negative (argv[c])) {
312 printf (_("<crta> (%s) must be a non-negative number\n"), argv[c]);
313 return ERROR;
314 } else {
315 crta = atof (argv[c++]);
316 if (c == argc)
317 return validate_arguments ();
318 }
319 }
321 if (max_packets == -1) {
322 if (is_intnonneg (argv[c])) {
323 max_packets = atoi (argv[c++]);
324 } else {
325 printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]);
326 return ERROR;
327 }
328 }
330 return validate_arguments ();
331 }
335 int
336 get_threshold (char *arg, float *trta, int *tpl)
337 {
338 if (is_intnonneg (arg) && sscanf (arg, "%f", trta) == 1)
339 return OK;
340 else if (strpbrk (arg, ",:") && strstr (arg, "%") && sscanf (arg, "%f%*[:,]%d%%", trta, tpl) == 2)
341 return OK;
342 else if (strstr (arg, "%") && sscanf (arg, "%d%%", tpl) == 1)
343 return OK;
345 usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
346 return STATE_UNKNOWN;
347 }
351 int
352 validate_arguments ()
353 {
354 float max_seconds;
355 int i;
357 if (wrta < 0.0) {
358 printf (_("<wrta> was not set\n"));
359 return ERROR;
360 }
361 else if (crta < 0.0) {
362 printf (_("<crta> was not set\n"));
363 return ERROR;
364 }
365 else if (wpl == UNKNOWN_PACKET_LOSS) {
366 printf (_("<wpl> was not set\n"));
367 return ERROR;
368 }
369 else if (cpl == UNKNOWN_PACKET_LOSS) {
370 printf (_("<cpl> was not set\n"));
371 return ERROR;
372 }
373 else if (wrta > crta) {
374 printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta, crta);
375 return ERROR;
376 }
377 else if (wpl > cpl) {
378 printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl, cpl);
379 return ERROR;
380 }
382 if (max_packets == -1)
383 max_packets = DEFAULT_MAX_PACKETS;
385 max_seconds = crta / 1000.0 * max_packets + max_packets;
386 if (max_seconds > timeout_interval)
387 timeout_interval = (int)max_seconds;
389 for (i=0; i<n_addresses; i++) {
390 if (is_host(addresses[i]) == FALSE)
391 usage2 (_("Invalid hostname/address"), addresses[i]);
392 }
394 return OK;
395 }
399 int
400 run_ping (const char *cmd, const char *addr)
401 {
402 char buf[MAX_INPUT_BUFFER];
403 int result = STATE_UNKNOWN;
405 if ((child_process = spopen (cmd)) == NULL)
406 die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd);
408 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
409 if (child_stderr == NULL)
410 printf (_("Cannot open stderr for %s\n"), cmd);
412 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
414 result = max_state (result, error_scan (buf, addr));
416 /* get the percent loss statistics */
417 if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 ||
418 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 ||
419 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 ||
420 sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1 ||
421 sscanf(buf,"%*d packets transmitted, %*d received, %d%% packet loss, time", &pl)==1 ||
422 sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl) == 1)
423 continue;
425 /* get the round trip average */
426 else
427 if(sscanf(buf,"round-trip min/avg/max = %*f/%f/%*f",&rta)==1 ||
428 sscanf(buf,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta)==1 ||
429 sscanf(buf,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta)==1 ||
430 sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
431 sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta)==1 ||
432 sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta)==1 ||
433 sscanf(buf,"round-trip (ms) min/avg/max/stddev = %*f/%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 }
462 int
463 error_scan (char buf[MAX_INPUT_BUFFER], const char *addr)
464 {
465 if (strstr (buf, "Network is unreachable"))
466 die (STATE_CRITICAL, _("CRITICAL - Network unreachable (%s)"), addr);
467 else if (strstr (buf, "Destination Host Unreachable"))
468 die (STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)"), addr);
469 else if (strstr (buf, "unknown host" ))
470 die (STATE_CRITICAL, _("CRITICAL - Host not found (%s)"), addr);
471 else if (strstr (buf, "Time to live exceeded"))
472 die (STATE_CRITICAL, _("CRITICAL - Time to live exceeded (%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 }
488 void
489 print_help (void)
490 {
491 print_revision (progname, revision);
493 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>");
494 printf (COPYRIGHT, copyright, email);
496 printf (_("Use ping to check connection statistics for a remote host.\n\n"));
498 print_usage ();
500 printf (_(UT_HELP_VRSN));
502 printf (_(UT_IPv46));
504 printf (_("\
505 -H, --hostname=HOST\n\
506 host to ping\n\
507 -w, --warning=THRESHOLD\n\
508 warning threshold pair\n\
509 -c, --critical=THRESHOLD\n\
510 critical threshold pair\n\
511 -p, --packets=INTEGER\n\
512 number of ICMP ECHO packets to send (Default: %d)\n\
513 -L, --link\n\
514 show HTML in the plugin output (obsoleted by urlize)\n"),
515 DEFAULT_MAX_PACKETS);
517 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
519 printf (_("\
520 THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
521 time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
522 percentage of packet loss to trigger an alarm state.\n\n"));
524 printf (_("\
525 This plugin uses the ping command to probe the specified host for packet loss\n\
526 (percentage) and round trip average (milliseconds). It can produce HTML output\n\
527 linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in\n\
528 the contrib area of the downloads section at http://www.nagios.org\n\n"));
530 printf (_(UT_SUPPORT));
531 }
533 void
534 print_usage (void)
535 {
536 printf ("Usage: %s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n\
537 [-p packets] [-t timeout] [-L] [-4|-6]\n", progname);
538 }