Fixed bug in tools/setup if docbook was run
[monitoring-plugins.git] / plugins / check_ping.c
blob0f3292d0feebc68556e83e8e9eb4a66f02d1789f
1 /*****************************************************************************
2 *
3 * Nagios check_ping plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
12 * This file contains the check_ping plugin
14 * Use the ping program to check connection statistics for a remote host.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * $Id$
32 *****************************************************************************/
34 const char *progname = "check_ping";
35 const char *revision = "$Revision$";
36 const char *copyright = "2000-2007";
37 const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 #include "common.h"
40 #include "netutils.h"
41 #include "popen.h"
42 #include "utils.h"
44 #define WARN_DUPLICATES "DUPLICATES FOUND! "
45 #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
47 enum {
48 UNKNOWN_PACKET_LOSS = 200, /* 200% */
49 DEFAULT_MAX_PACKETS = 5 /* default no. of ICMP ECHO packets */
52 int process_arguments (int, char **);
53 int get_threshold (char *, float *, int *);
54 int validate_arguments (void);
55 int run_ping (const char *cmd, const char *addr);
56 int error_scan (char buf[MAX_INPUT_BUFFER], const char *addr);
57 void print_usage (void);
58 void print_help (void);
60 int display_html = FALSE;
61 int wpl = UNKNOWN_PACKET_LOSS;
62 int cpl = UNKNOWN_PACKET_LOSS;
63 float wrta = UNKNOWN_TRIP_TIME;
64 float crta = UNKNOWN_TRIP_TIME;
65 char **addresses = NULL;
66 int n_addresses = 0;
67 int max_addr = 1;
68 int max_packets = -1;
69 int verbose = 0;
71 float rta = UNKNOWN_TRIP_TIME;
72 int pl = UNKNOWN_PACKET_LOSS;
74 char *warn_text;
78 int
79 main (int argc, char **argv)
81 char *cmd = NULL;
82 char *rawcmd = NULL;
83 int result = STATE_UNKNOWN;
84 int this_result = STATE_UNKNOWN;
85 int i;
87 setlocale (LC_ALL, "");
88 setlocale (LC_NUMERIC, "C");
89 bindtextdomain (PACKAGE, LOCALEDIR);
90 textdomain (PACKAGE);
92 addresses = malloc (sizeof(char*) * max_addr);
93 addresses[0] = NULL;
95 /* Parse extra opts if any */
96 argv=np_extra_opts (&argc, argv, progname);
98 if (process_arguments (argc, argv) == ERROR)
99 usage4 (_("Could not parse arguments"));
101 /* Set signal handling and alarm */
102 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
103 usage4 (_("Cannot catch SIGALRM"));
106 /* If ./configure finds ping has timeout values, set plugin alarm slightly
107 * higher so that we can use response from command line ping */
108 #if defined(PING_PACKETS_FIRST) && defined(PING_HAS_TIMEOUT)
109 alarm (timeout_interval + 1);
110 #else
111 alarm (timeout_interval);
112 #endif
114 for (i = 0 ; i < n_addresses ; i++) {
116 #ifdef PING6_COMMAND
117 if (address_family != AF_INET && is_inet6_addr(addresses[i]))
118 rawcmd = strdup(PING6_COMMAND);
119 else
120 rawcmd = strdup(PING_COMMAND);
121 #else
122 rawcmd = strdup(PING_COMMAND);
123 #endif
125 /* does the host address of number of packets argument come first? */
126 #ifdef PING_PACKETS_FIRST
127 # ifdef PING_HAS_TIMEOUT
128 asprintf (&cmd, rawcmd, timeout_interval, max_packets, addresses[i]);
129 # else
130 asprintf (&cmd, rawcmd, max_packets, addresses[i]);
131 # endif
132 #else
133 asprintf (&cmd, rawcmd, addresses[i], max_packets);
134 #endif
136 if (verbose >= 2)
137 printf ("CMD: %s\n", cmd);
139 /* run the command */
140 this_result = run_ping (cmd, addresses[i]);
142 if (pl == UNKNOWN_PACKET_LOSS || rta < 0.0) {
143 printf ("%s\n", cmd);
144 die (STATE_UNKNOWN,
145 _("CRITICAL - Could not interpret output from ping command\n"));
148 if (pl >= cpl || rta >= crta || rta < 0)
149 this_result = STATE_CRITICAL;
150 else if (pl >= wpl || rta >= wrta)
151 this_result = STATE_WARNING;
152 else if (pl >= 0 && rta >= 0)
153 this_result = max_state (STATE_OK, this_result);
155 if (n_addresses > 1 && this_result != STATE_UNKNOWN)
156 die (STATE_OK, "%s is alive\n", addresses[i]);
158 if (display_html == TRUE)
159 printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]);
160 if (pl == 100)
161 printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text,
162 pl);
163 else
164 printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"),
165 state_text (this_result), warn_text, pl, rta);
166 if (display_html == TRUE)
167 printf ("</A>");
169 /* Print performance data */
170 printf("|%s", fperfdata ("rta", (double) rta, "ms",
171 wrta>0?TRUE:FALSE, wrta,
172 crta>0?TRUE:FALSE, crta,
173 TRUE, 0, FALSE, 0));
174 printf(" %s\n", perfdata ("pl", (long) pl, "%",
175 wpl>0?TRUE:FALSE, wpl,
176 cpl>0?TRUE:FALSE, cpl,
177 TRUE, 0, FALSE, 0));
179 if (verbose >= 2)
180 printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
182 result = max_state (result, this_result);
183 free (rawcmd);
184 free (cmd);
187 return result;
192 /* process command-line arguments */
194 process_arguments (int argc, char **argv)
196 int c = 1;
197 char *ptr;
199 int option = 0;
200 static struct option longopts[] = {
201 STD_LONG_OPTS,
202 {"packets", required_argument, 0, 'p'},
203 {"nohtml", no_argument, 0, 'n'},
204 {"link", no_argument, 0, 'L'},
205 {"use-ipv4", no_argument, 0, '4'},
206 {"use-ipv6", no_argument, 0, '6'},
207 {0, 0, 0, 0}
210 if (argc < 2)
211 return ERROR;
213 for (c = 1; c < argc; c++) {
214 if (strcmp ("-to", argv[c]) == 0)
215 strcpy (argv[c], "-t");
216 if (strcmp ("-nohtml", argv[c]) == 0)
217 strcpy (argv[c], "-n");
220 while (1) {
221 c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
223 if (c == -1 || c == EOF)
224 break;
226 switch (c) {
227 case '?': /* usage */
228 usage5 ();
229 case 'h': /* help */
230 print_help ();
231 exit (STATE_OK);
232 break;
233 case 'V': /* version */
234 print_revision (progname, revision);
235 exit (STATE_OK);
236 break;
237 case 't': /* timeout period */
238 timeout_interval = atoi (optarg);
239 break;
240 case 'v': /* verbose mode */
241 verbose++;
242 break;
243 case '4': /* IPv4 only */
244 address_family = AF_INET;
245 break;
246 case '6': /* IPv6 only */
247 #ifdef USE_IPV6
248 address_family = AF_INET6;
249 #else
250 usage (_("IPv6 support not available\n"));
251 #endif
252 break;
253 case 'H': /* hostname */
254 ptr=optarg;
255 while (1) {
256 n_addresses++;
257 if (n_addresses > max_addr) {
258 max_addr *= 2;
259 addresses = realloc (addresses, sizeof(char*) * max_addr);
260 if (addresses == NULL)
261 die (STATE_UNKNOWN, _("Could not realloc() addresses\n"));
263 addresses[n_addresses-1] = ptr;
264 if ((ptr = index (ptr, ','))) {
265 strcpy (ptr, "");
266 ptr += sizeof(char);
267 } else {
268 break;
271 break;
272 case 'p': /* number of packets to send */
273 if (is_intnonneg (optarg))
274 max_packets = atoi (optarg);
275 else
276 usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg);
277 break;
278 case 'n': /* no HTML */
279 display_html = FALSE;
280 break;
281 case 'L': /* show HTML */
282 display_html = TRUE;
283 break;
284 case 'c':
285 get_threshold (optarg, &crta, &cpl);
286 break;
287 case 'w':
288 get_threshold (optarg, &wrta, &wpl);
289 break;
293 c = optind;
294 if (c == argc)
295 return validate_arguments ();
297 if (addresses[0] == NULL) {
298 if (is_host (argv[c]) == FALSE) {
299 usage2 (_("Invalid hostname/address"), argv[c]);
300 } else {
301 addresses[0] = argv[c++];
302 n_addresses++;
303 if (c == argc)
304 return validate_arguments ();
308 if (wpl == UNKNOWN_PACKET_LOSS) {
309 if (is_intpercent (argv[c]) == FALSE) {
310 printf (_("<wpl> (%s) must be an integer percentage\n"), argv[c]);
311 return ERROR;
312 } else {
313 wpl = atoi (argv[c++]);
314 if (c == argc)
315 return validate_arguments ();
319 if (cpl == UNKNOWN_PACKET_LOSS) {
320 if (is_intpercent (argv[c]) == FALSE) {
321 printf (_("<cpl> (%s) must be an integer percentage\n"), argv[c]);
322 return ERROR;
323 } else {
324 cpl = atoi (argv[c++]);
325 if (c == argc)
326 return validate_arguments ();
330 if (wrta < 0.0) {
331 if (is_negative (argv[c])) {
332 printf (_("<wrta> (%s) must be a non-negative number\n"), argv[c]);
333 return ERROR;
334 } else {
335 wrta = atof (argv[c++]);
336 if (c == argc)
337 return validate_arguments ();
341 if (crta < 0.0) {
342 if (is_negative (argv[c])) {
343 printf (_("<crta> (%s) must be a non-negative number\n"), argv[c]);
344 return ERROR;
345 } else {
346 crta = atof (argv[c++]);
347 if (c == argc)
348 return validate_arguments ();
352 if (max_packets == -1) {
353 if (is_intnonneg (argv[c])) {
354 max_packets = atoi (argv[c++]);
355 } else {
356 printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]);
357 return ERROR;
361 return validate_arguments ();
367 get_threshold (char *arg, float *trta, int *tpl)
369 if (is_intnonneg (arg) && sscanf (arg, "%f", trta) == 1)
370 return OK;
371 else if (strpbrk (arg, ",:") && strstr (arg, "%") && sscanf (arg, "%f%*[:,]%d%%", trta, tpl) == 2)
372 return OK;
373 else if (strstr (arg, "%") && sscanf (arg, "%d%%", tpl) == 1)
374 return OK;
376 usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
377 return STATE_UNKNOWN;
383 validate_arguments ()
385 float max_seconds;
386 int i;
388 if (wrta < 0.0) {
389 printf (_("<wrta> was not set\n"));
390 return ERROR;
392 else if (crta < 0.0) {
393 printf (_("<crta> was not set\n"));
394 return ERROR;
396 else if (wpl == UNKNOWN_PACKET_LOSS) {
397 printf (_("<wpl> was not set\n"));
398 return ERROR;
400 else if (cpl == UNKNOWN_PACKET_LOSS) {
401 printf (_("<cpl> was not set\n"));
402 return ERROR;
404 else if (wrta > crta) {
405 printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta, crta);
406 return ERROR;
408 else if (wpl > cpl) {
409 printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl, cpl);
410 return ERROR;
413 if (max_packets == -1)
414 max_packets = DEFAULT_MAX_PACKETS;
416 max_seconds = crta / 1000.0 * max_packets + max_packets;
417 if (max_seconds > timeout_interval)
418 timeout_interval = (int)max_seconds;
420 for (i=0; i<n_addresses; i++) {
421 if (is_host(addresses[i]) == FALSE)
422 usage2 (_("Invalid hostname/address"), addresses[i]);
425 if (n_addresses == 0) {
426 usage (_("You must specify a server address or host name"));
429 return OK;
435 run_ping (const char *cmd, const char *addr)
437 char buf[MAX_INPUT_BUFFER];
438 int result = STATE_UNKNOWN;
440 if ((child_process = spopen (cmd)) == NULL)
441 die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd);
443 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
444 if (child_stderr == NULL)
445 printf (_("Cannot open stderr for %s\n"), cmd);
447 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
449 if (verbose >= 3)
450 printf("Output: %s", buf);
452 result = max_state (result, error_scan (buf, addr));
454 /* get the percent loss statistics */
455 if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 ||
456 sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss", &pl) == 1 ||
457 sscanf(buf,"%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss", &pl) == 1 ||
458 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 ||
459 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 ||
460 sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1 ||
461 sscanf(buf,"%*d packets transmitted, %*d received, %d%% packet loss, time", &pl)==1 ||
462 sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl) == 1 ||
463 sscanf(buf,"%*d packets transmitted %*d received, +%*d errors, %d%% packet loss", &pl) == 1
465 continue;
467 /* get the round trip average */
468 else
469 if(sscanf(buf,"round-trip min/avg/max = %*f/%f/%*f",&rta)==1 ||
470 sscanf(buf,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta)==1 ||
471 sscanf(buf,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta)==1 ||
472 sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
473 sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta)==1 ||
474 sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta)==1 ||
475 sscanf(buf,"round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
476 sscanf(buf,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta)==1)
477 continue;
480 /* this is needed because there is no rta if all packets are lost */
481 if (pl == 100)
482 rta = crta;
484 /* check stderr, setting at least WARNING if there is output here */
485 /* Add warning into warn_text */
486 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) {
487 if (! strstr(buf,"WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP")) {
488 if (verbose >= 3) {
489 printf("Got stderr: %s", buf);
491 if ((result=error_scan(buf, addr)) == STATE_OK) {
492 result = STATE_WARNING;
493 if (warn_text == NULL) {
494 warn_text = strdup(_("System call sent warnings to stderr "));
495 } else {
496 asprintf(&warn_text, "%s %s", warn_text, _("System call sent warnings to stderr "));
502 (void) fclose (child_stderr);
505 /* close the pipe - WARNING if status is set */
506 if (spclose (child_process))
507 result = max_state (result, STATE_WARNING);
509 if (warn_text == NULL)
510 warn_text = strdup("");
512 return result;
518 error_scan (char buf[MAX_INPUT_BUFFER], const char *addr)
520 if (strstr (buf, "Network is unreachable") ||
521 strstr (buf, "Destination Net Unreachable")
523 die (STATE_CRITICAL, _("CRITICAL - Network Unreachable (%s)"), addr);
524 else if (strstr (buf, "Destination Host Unreachable"))
525 die (STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)"), addr);
526 else if (strstr (buf, "Destination Port Unreachable"))
527 die (STATE_CRITICAL, _("CRITICAL - Bogus ICMP: Port Unreachable (%s)"), addr);
528 else if (strstr (buf, "Destination Protocol Unreachable"))
529 die (STATE_CRITICAL, _("CRITICAL - Bogus ICMP: Protocol Unreachable (%s)"), addr);
530 else if (strstr (buf, "Destination Net Prohibited"))
531 die (STATE_CRITICAL, _("CRITICAL - Network Prohibited (%s)"), addr);
532 else if (strstr (buf, "Destination Host Prohibited"))
533 die (STATE_CRITICAL, _("CRITICAL - Host Prohibited (%s)"), addr);
534 else if (strstr (buf, "Packet filtered"))
535 die (STATE_CRITICAL, _("CRITICAL - Packet Filtered (%s)"), addr);
536 else if (strstr (buf, "unknown host" ))
537 die (STATE_CRITICAL, _("CRITICAL - Host not found (%s)"), addr);
538 else if (strstr (buf, "Time to live exceeded"))
539 die (STATE_CRITICAL, _("CRITICAL - Time to live exceeded (%s)"), addr);
541 if (strstr (buf, "(DUP!)") || strstr (buf, "DUPLICATES FOUND")) {
542 if (warn_text == NULL)
543 warn_text = strdup (_(WARN_DUPLICATES));
544 else if (! strstr (warn_text, _(WARN_DUPLICATES)) &&
545 asprintf (&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1)
546 die (STATE_UNKNOWN, _("Unable to realloc warn_text"));
547 return (STATE_WARNING);
550 return (STATE_OK);
555 void
556 print_help (void)
558 print_revision (progname, revision);
560 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
561 printf (COPYRIGHT, copyright, email);
563 printf (_("Use ping to check connection statistics for a remote host."));
565 printf ("\n\n");
567 print_usage ();
569 printf (_(UT_HELP_VRSN));
570 printf (_(UT_EXTRA_OPTS));
572 printf (_(UT_IPv46));
574 printf (" %s\n", "-H, --hostname=HOST");
575 printf (" %s\n", _("host to ping"));
576 printf (" %s\n", "-w, --warning=THRESHOLD");
577 printf (" %s\n", _("warning threshold pair"));
578 printf (" %s\n", "-c, --critical=THRESHOLD");
579 printf (" %s\n", _("critical threshold pair"));
580 printf (" %s\n", "-p, --packets=INTEGER");
581 printf (" %s ", _("number of ICMP ECHO packets to send"));
582 printf (_("(Default: %d)\n"), DEFAULT_MAX_PACKETS);
583 printf (" %s\n", "-L, --link");
584 printf (" %s\n", _("show HTML in the plugin output (obsoleted by urlize)"));
586 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
588 printf ("\n");
589 printf ("%s\n", _("THRESHOLD is <rta>,<pl>% where <rta> is the round trip average travel"));
590 printf ("%s\n", _("time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the"));
591 printf ("%s\n", _("percentage of packet loss to trigger an alarm state."));
593 printf ("\n");
594 printf ("%s\n", _("This plugin uses the ping command to probe the specified host for packet loss"));
595 printf ("%s\n", _("(percentage) and round trip average (milliseconds). It can produce HTML output"));
596 printf ("%s\n", _("linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in"));
597 printf ("%s\n", _("the contrib area of the downloads section at http://www.nagios.org/"));
599 #ifdef NP_EXTRA_OPTS
600 printf ("\n");
601 printf ("%s\n", _("Notes:"));
602 printf (_(UT_EXTRA_OPTS_NOTES));
603 #endif
605 printf (_(UT_SUPPORT));
608 void
609 print_usage (void)
611 printf (_("Usage:"));
612 printf ("%s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n", progname);
613 printf (" [-p packets] [-t timeout] [-4|-6]\n");