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-2003";
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 usage (_("check_ping: could not parse arguments\n"));
84 /* Set signal handling and alarm */
85 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
86 printf (_("Cannot catch SIGALRM"));
87 return STATE_UNKNOWN;
88 }
90 /* handle timeouts gracefully */
91 alarm (timeout_interval);
93 for (i = 0 ; i < n_addresses ; i++) {
95 #ifdef PING6_COMMAND
96 if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
97 rawcmd = strdup(PING6_COMMAND);
98 else
99 rawcmd = strdup(PING_COMMAND);
100 #else
101 rawcmd = strdup(PING_COMMAND);
102 #endif
104 /* does the host address of number of packets argument come first? */
105 #ifdef PING_PACKETS_FIRST
106 # ifdef PING_HAS_TIMEOUT
107 asprintf (&cmd, rawcmd, timeout_interval, max_packets, addresses[i]);
108 # else
109 asprintf (&cmd, rawcmd, max_packets, addresses[i]);
110 # endif
111 #else
112 asprintf (&cmd, rawcmd, addresses[i], max_packets);
113 #endif
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);
153 free (rawcmd);
154 free (cmd);
155 }
157 return result;
158 }
162 /* process command-line arguments */
163 int
164 process_arguments (int argc, char **argv)
165 {
166 int c = 1;
167 char *ptr;
169 int option = 0;
170 static struct option longopts[] = {
171 STD_LONG_OPTS,
172 {"packets", required_argument, 0, 'p'},
173 {"nohtml", no_argument, 0, 'n'},
174 {"link", no_argument, 0, 'L'},
175 {"use-ipv4", no_argument, 0, '4'},
176 {"use-ipv6", no_argument, 0, '6'},
177 {0, 0, 0, 0}
178 };
180 if (argc < 2)
181 return ERROR;
183 for (c = 1; c < argc; c++) {
184 if (strcmp ("-to", argv[c]) == 0)
185 strcpy (argv[c], "-t");
186 if (strcmp ("-nohtml", argv[c]) == 0)
187 strcpy (argv[c], "-n");
188 }
190 while (1) {
191 c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
193 if (c == -1 || c == EOF)
194 break;
196 switch (c) {
197 case '?': /* usage */
198 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
199 print_usage ();
200 exit (STATE_UNKNOWN);
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, sizeof(char*) * 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 usage2 (_("Invalid hostname/address"), argv[c]);
272 } else {
273 addresses[0] = argv[c++];
274 n_addresses++;
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 }
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 }
354 int
355 validate_arguments ()
356 {
357 float max_seconds;
358 int i;
360 if (wrta < 0.0) {
361 printf (_("<wrta> was not set\n"));
362 return ERROR;
363 }
364 else if (crta < 0.0) {
365 printf (_("<crta> was not set\n"));
366 return ERROR;
367 }
368 else if (wpl == UNKNOWN_PACKET_LOSS) {
369 printf (_("<wpl> was not set\n"));
370 return ERROR;
371 }
372 else if (cpl == UNKNOWN_PACKET_LOSS) {
373 printf (_("<cpl> was not set\n"));
374 return ERROR;
375 }
376 else if (wrta > crta) {
377 printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta, crta);
378 return ERROR;
379 }
380 else if (wpl > cpl) {
381 printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl, cpl);
382 return ERROR;
383 }
385 if (max_packets == -1)
386 max_packets = DEFAULT_MAX_PACKETS;
388 max_seconds = crta / 1000.0 * max_packets + max_packets;
389 if (max_seconds > timeout_interval)
390 timeout_interval = (int)max_seconds;
392 for (i=0; i<n_addresses; i++) {
393 if (is_host(addresses[i]) == FALSE)
394 usage2 (_("Invalid hostname/address"), addresses[i]);
395 }
397 return OK;
398 }
402 int
403 run_ping (const char *cmd, const char *addr)
404 {
405 char buf[MAX_INPUT_BUFFER];
406 int result = STATE_UNKNOWN;
408 if ((child_process = spopen (cmd)) == NULL)
409 die (STATE_UNKNOWN, _("Cannot open pipe: %s"), cmd);
411 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
412 if (child_stderr == NULL)
413 printf (_("Cannot open stderr for %s\n"), cmd);
415 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
417 result = max_state (result, error_scan (buf, addr));
419 /* get the percent loss statistics */
420 if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 ||
421 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 ||
422 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 ||
423 sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1 ||
424 sscanf(buf,"%*d packets transmitted, %*d received, %d%% packet 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 }
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, _("CRITICAL - Network unreachable (%s)"), addr);
469 else if (strstr (buf, "Destination Host Unreachable"))
470 die (STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)"), addr);
471 else if (strstr (buf, "unknown host" ))
472 die (STATE_CRITICAL, _("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 }
488 void
489 print_usage (void)
490 {
491 printf (\
492 "Usage: %s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n\
493 [-p packets] [-t timeout] [-L] [-4|-6]\n", progname);
494 printf (_(UT_HLP_VRS), progname, progname);
495 }
499 void
500 print_help (void)
501 {
502 print_revision (progname, revision);
504 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>");
505 printf (COPYRIGHT, copyright, email);
507 printf (_("Use ping to check connection statistics for a remote host.\n\n"));
509 print_usage ();
511 printf (_(UT_HELP_VRSN));
513 printf (_(UT_IPv46));
515 printf (_("\
516 -H, --hostname=HOST\n\
517 host to ping\n\
518 -w, --warning=THRESHOLD\n\
519 warning threshold pair\n\
520 -c, --critical=THRESHOLD\n\
521 critical threshold pair\n\
522 -p, --packets=INTEGER\n\
523 number of ICMP ECHO packets to send (Default: %d)\n\
524 -L, --link\n\
525 show HTML in the plugin output (obsoleted by urlize)\n"),
526 DEFAULT_MAX_PACKETS);
528 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
530 printf (_("\
531 THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
532 time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
533 percentage of packet loss to trigger an alarm state.\n\n"));
535 printf (_("\
536 This plugin uses the ping command to probe the specified host for packet loss\n\
537 (percentage) and round trip average (milliseconds). It can produce HTML output\n\
538 linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in\n\
539 the contrib area of the downloads section at http://www.nagios.org\n\n"));
541 printf (_(UT_SUPPORT));
542 }