1 /*****************************************************************************
3 * Nagios check_ping plugin
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
8 * Last Modified: $Date$
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/>.
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";
44 #define WARN_DUPLICATES "DUPLICATES FOUND! "
45 #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
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
;
71 float rta
= UNKNOWN_TRIP_TIME
;
72 int pl
= UNKNOWN_PACKET_LOSS
;
79 main (int argc
, char **argv
)
83 int result
= STATE_UNKNOWN
;
84 int this_result
= STATE_UNKNOWN
;
87 setlocale (LC_ALL
, "");
88 setlocale (LC_NUMERIC
, "C");
89 bindtextdomain (PACKAGE
, LOCALEDIR
);
92 addresses
= malloc (sizeof(char*) * max_addr
);
95 if (process_arguments (argc
, argv
) == ERROR
)
96 usage4 (_("Could not parse arguments"));
98 /* Set signal handling and alarm */
99 if (signal (SIGALRM
, popen_timeout_alarm_handler
) == SIG_ERR
) {
100 usage4 (_("Cannot catch SIGALRM"));
103 /* If ./configure finds ping has timeout values, set plugin alarm slightly
104 * higher so that we can use response from command line ping */
105 #if defined(PING_PACKETS_FIRST) && defined(PING_HAS_TIMEOUT)
106 alarm (timeout_interval
+ 1);
108 alarm (timeout_interval
);
111 for (i
= 0 ; i
< n_addresses
; i
++) {
114 if (address_family
!= AF_INET
&& is_inet6_addr(addresses
[i
]))
115 rawcmd
= strdup(PING6_COMMAND
);
117 rawcmd
= strdup(PING_COMMAND
);
119 rawcmd
= strdup(PING_COMMAND
);
122 /* does the host address of number of packets argument come first? */
123 #ifdef PING_PACKETS_FIRST
124 # ifdef PING_HAS_TIMEOUT
125 asprintf (&cmd
, rawcmd
, timeout_interval
, max_packets
, addresses
[i
]);
127 asprintf (&cmd
, rawcmd
, max_packets
, addresses
[i
]);
130 asprintf (&cmd
, rawcmd
, addresses
[i
], max_packets
);
134 printf ("CMD: %s\n", cmd
);
136 /* run the command */
137 this_result
= run_ping (cmd
, addresses
[i
]);
139 if (pl
== UNKNOWN_PACKET_LOSS
|| rta
< 0.0) {
140 printf ("%s\n", cmd
);
142 _("CRITICAL - Could not interpret output from ping command\n"));
145 if (pl
>= cpl
|| rta
>= crta
|| rta
< 0)
146 this_result
= STATE_CRITICAL
;
147 else if (pl
>= wpl
|| rta
>= wrta
)
148 this_result
= STATE_WARNING
;
149 else if (pl
>= 0 && rta
>= 0)
150 this_result
= max_state (STATE_OK
, this_result
);
152 if (n_addresses
> 1 && this_result
!= STATE_UNKNOWN
)
153 die (STATE_OK
, "%s is alive\n", addresses
[i
]);
155 if (display_html
== TRUE
)
156 printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL
, addresses
[i
]);
158 printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result
), warn_text
,
161 printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"),
162 state_text (this_result
), warn_text
, pl
, rta
);
163 if (display_html
== TRUE
)
168 printf ("%f:%d%% %f:%d%%\n", wrta
, wpl
, crta
, cpl
);
170 result
= max_state (result
, this_result
);
180 /* process command-line arguments */
182 process_arguments (int argc
, char **argv
)
188 static struct option longopts
[] = {
190 {"packets", required_argument
, 0, 'p'},
191 {"nohtml", no_argument
, 0, 'n'},
192 {"link", no_argument
, 0, 'L'},
193 {"use-ipv4", no_argument
, 0, '4'},
194 {"use-ipv6", no_argument
, 0, '6'},
201 for (c
= 1; c
< argc
; c
++) {
202 if (strcmp ("-to", argv
[c
]) == 0)
203 strcpy (argv
[c
], "-t");
204 if (strcmp ("-nohtml", argv
[c
]) == 0)
205 strcpy (argv
[c
], "-n");
209 c
= getopt_long (argc
, argv
, "VvhnL46t:c:w:H:p:", longopts
, &option
);
211 if (c
== -1 || c
== EOF
)
215 case '?': /* usage */
221 case 'V': /* version */
222 print_revision (progname
, revision
);
225 case 't': /* timeout period */
226 timeout_interval
= atoi (optarg
);
228 case 'v': /* verbose mode */
231 case '4': /* IPv4 only */
232 address_family
= AF_INET
;
234 case '6': /* IPv6 only */
236 address_family
= AF_INET6
;
238 usage (_("IPv6 support not available\n"));
241 case 'H': /* hostname */
245 if (n_addresses
> max_addr
) {
247 addresses
= realloc (addresses
, sizeof(char*) * max_addr
);
248 if (addresses
== NULL
)
249 die (STATE_UNKNOWN
, _("Could not realloc() addresses\n"));
251 addresses
[n_addresses
-1] = ptr
;
252 if ((ptr
= index (ptr
, ','))) {
260 case 'p': /* number of packets to send */
261 if (is_intnonneg (optarg
))
262 max_packets
= atoi (optarg
);
264 usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg
);
266 case 'n': /* no HTML */
267 display_html
= FALSE
;
269 case 'L': /* show HTML */
273 get_threshold (optarg
, &crta
, &cpl
);
276 get_threshold (optarg
, &wrta
, &wpl
);
283 return validate_arguments ();
285 if (addresses
[0] == NULL
) {
286 if (is_host (argv
[c
]) == FALSE
) {
287 usage2 (_("Invalid hostname/address"), argv
[c
]);
289 addresses
[0] = argv
[c
++];
292 return validate_arguments ();
296 if (wpl
== UNKNOWN_PACKET_LOSS
) {
297 if (is_intpercent (argv
[c
]) == FALSE
) {
298 printf (_("<wpl> (%s) must be an integer percentage\n"), argv
[c
]);
301 wpl
= atoi (argv
[c
++]);
303 return validate_arguments ();
307 if (cpl
== UNKNOWN_PACKET_LOSS
) {
308 if (is_intpercent (argv
[c
]) == FALSE
) {
309 printf (_("<cpl> (%s) must be an integer percentage\n"), argv
[c
]);
312 cpl
= atoi (argv
[c
++]);
314 return validate_arguments ();
319 if (is_negative (argv
[c
])) {
320 printf (_("<wrta> (%s) must be a non-negative number\n"), argv
[c
]);
323 wrta
= atof (argv
[c
++]);
325 return validate_arguments ();
330 if (is_negative (argv
[c
])) {
331 printf (_("<crta> (%s) must be a non-negative number\n"), argv
[c
]);
334 crta
= atof (argv
[c
++]);
336 return validate_arguments ();
340 if (max_packets
== -1) {
341 if (is_intnonneg (argv
[c
])) {
342 max_packets
= atoi (argv
[c
++]);
344 printf (_("<max_packets> (%s) must be a non-negative number\n"), argv
[c
]);
349 return validate_arguments ();
355 get_threshold (char *arg
, float *trta
, int *tpl
)
357 if (is_intnonneg (arg
) && sscanf (arg
, "%f", trta
) == 1)
359 else if (strpbrk (arg
, ",:") && strstr (arg
, "%") && sscanf (arg
, "%f%*[:,]%d%%", trta
, tpl
) == 2)
361 else if (strstr (arg
, "%") && sscanf (arg
, "%d%%", tpl
) == 1)
364 usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg
);
365 return STATE_UNKNOWN
;
371 validate_arguments ()
377 printf (_("<wrta> was not set\n"));
380 else if (crta
< 0.0) {
381 printf (_("<crta> was not set\n"));
384 else if (wpl
== UNKNOWN_PACKET_LOSS
) {
385 printf (_("<wpl> was not set\n"));
388 else if (cpl
== UNKNOWN_PACKET_LOSS
) {
389 printf (_("<cpl> was not set\n"));
392 else if (wrta
> crta
) {
393 printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta
, crta
);
396 else if (wpl
> cpl
) {
397 printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl
, cpl
);
401 if (max_packets
== -1)
402 max_packets
= DEFAULT_MAX_PACKETS
;
404 max_seconds
= crta
/ 1000.0 * max_packets
+ max_packets
;
405 if (max_seconds
> timeout_interval
)
406 timeout_interval
= (int)max_seconds
;
408 for (i
=0; i
<n_addresses
; i
++) {
409 if (is_host(addresses
[i
]) == FALSE
)
410 usage2 (_("Invalid hostname/address"), addresses
[i
]);
413 if (n_addresses
== 0) {
414 usage (_("You must specify a server address or host name"));
423 run_ping (const char *cmd
, const char *addr
)
425 char buf
[MAX_INPUT_BUFFER
];
426 int result
= STATE_UNKNOWN
;
428 if ((child_process
= spopen (cmd
)) == NULL
)
429 die (STATE_UNKNOWN
, _("Could not open pipe: %s\n"), cmd
);
431 child_stderr
= fdopen (child_stderr_array
[fileno (child_process
)], "r");
432 if (child_stderr
== NULL
)
433 printf (_("Cannot open stderr for %s\n"), cmd
);
435 while (fgets (buf
, MAX_INPUT_BUFFER
- 1, child_process
)) {
438 printf("Output: %s", buf
);
440 result
= max_state (result
, error_scan (buf
, addr
));
442 /* get the percent loss statistics */
443 if(sscanf(buf
,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl
)==1 ||
444 sscanf(buf
,"%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss", &pl
) == 1 ||
445 sscanf(buf
,"%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss", &pl
) == 1 ||
446 sscanf(buf
,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl
)==1 ||
447 sscanf(buf
,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl
)==1 ||
448 sscanf(buf
,"%*d packets transmitted, %*d received, %d%% loss, time", &pl
)==1 ||
449 sscanf(buf
,"%*d packets transmitted, %*d received, %d%% packet loss, time", &pl
)==1 ||
450 sscanf(buf
,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl
) == 1 ||
451 sscanf(buf
,"%*d packets transmitted %*d received, +%*d errors, %d%% packet loss", &pl
) == 1
455 /* get the round trip average */
457 if(sscanf(buf
,"round-trip min/avg/max = %*f/%f/%*f",&rta
)==1 ||
458 sscanf(buf
,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta
)==1 ||
459 sscanf(buf
,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta
)==1 ||
460 sscanf(buf
,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta
)==1 ||
461 sscanf(buf
,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta
)==1 ||
462 sscanf(buf
,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta
)==1 ||
463 sscanf(buf
,"round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f",&rta
)==1 ||
464 sscanf(buf
,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta
)==1)
468 /* this is needed because there is no rta if all packets are lost */
472 /* check stderr, setting at least WARNING if there is output here */
473 /* Add warning into warn_text */
474 while (fgets (buf
, MAX_INPUT_BUFFER
- 1, child_stderr
)) {
475 if (! strstr(buf
,"WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP")) {
477 printf("Got stderr: %s", buf
);
479 if ((result
=error_scan(buf
, addr
)) == STATE_OK
) {
480 result
= STATE_WARNING
;
481 if (warn_text
== NULL
) {
482 warn_text
= strdup(_("System call sent warnings to stderr "));
484 asprintf(&warn_text
, "%s %s", warn_text
, _("System call sent warnings to stderr "));
490 (void) fclose (child_stderr
);
493 /* close the pipe - WARNING if status is set */
494 if (spclose (child_process
))
495 result
= max_state (result
, STATE_WARNING
);
497 if (warn_text
== NULL
)
498 warn_text
= strdup("");
506 error_scan (char buf
[MAX_INPUT_BUFFER
], const char *addr
)
508 if (strstr (buf
, "Network is unreachable") ||
509 strstr (buf
, "Destination Net Unreachable")
511 die (STATE_CRITICAL
, _("CRITICAL - Network Unreachable (%s)"), addr
);
512 else if (strstr (buf
, "Destination Host Unreachable"))
513 die (STATE_CRITICAL
, _("CRITICAL - Host Unreachable (%s)"), addr
);
514 else if (strstr (buf
, "Destination Port Unreachable"))
515 die (STATE_CRITICAL
, _("CRITICAL - Bogus ICMP: Port Unreachable (%s)"), addr
);
516 else if (strstr (buf
, "Destination Protocol Unreachable"))
517 die (STATE_CRITICAL
, _("CRITICAL - Bogus ICMP: Protocol Unreachable (%s)"), addr
);
518 else if (strstr (buf
, "Destination Net Prohibited"))
519 die (STATE_CRITICAL
, _("CRITICAL - Network Prohibited (%s)"), addr
);
520 else if (strstr (buf
, "Destination Host Prohibited"))
521 die (STATE_CRITICAL
, _("CRITICAL - Host Prohibited (%s)"), addr
);
522 else if (strstr (buf
, "Packet filtered"))
523 die (STATE_CRITICAL
, _("CRITICAL - Packet Filtered (%s)"), addr
);
524 else if (strstr (buf
, "unknown host" ))
525 die (STATE_CRITICAL
, _("CRITICAL - Host not found (%s)"), addr
);
526 else if (strstr (buf
, "Time to live exceeded"))
527 die (STATE_CRITICAL
, _("CRITICAL - Time to live exceeded (%s)"), addr
);
529 if (strstr (buf
, "(DUP!)") || strstr (buf
, "DUPLICATES FOUND")) {
530 if (warn_text
== NULL
)
531 warn_text
= strdup (_(WARN_DUPLICATES
));
532 else if (! strstr (warn_text
, _(WARN_DUPLICATES
)) &&
533 asprintf (&warn_text
, "%s %s", warn_text
, _(WARN_DUPLICATES
)) == -1)
534 die (STATE_UNKNOWN
, _("Unable to realloc warn_text"));
535 return (STATE_WARNING
);
546 print_revision (progname
, revision
);
548 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
549 printf (COPYRIGHT
, copyright
, email
);
551 printf (_("Use ping to check connection statistics for a remote host."));
557 printf (_(UT_HELP_VRSN
));
559 printf (_(UT_IPv46
));
561 printf (" %s\n", "-H, --hostname=HOST");
562 printf (" %s\n", _("host to ping"));
563 printf (" %s\n", "-w, --warning=THRESHOLD");
564 printf (" %s\n", _("warning threshold pair"));
565 printf (" %s\n", "-c, --critical=THRESHOLD");
566 printf (" %s\n", _("critical threshold pair"));
567 printf (" %s\n", "-p, --packets=INTEGER");
568 printf (" %s ", _("number of ICMP ECHO packets to send"));
569 printf (_("(Default: %d)\n"), DEFAULT_MAX_PACKETS
);
570 printf (" %s\n", "-L, --link");
571 printf (" %s\n", _("show HTML in the plugin output (obsoleted by urlize)"));
573 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
575 printf ("%s\n", _("THRESHOLD is <rta>,<pl>% where <rta> is the round trip average travel"));
576 printf ("%s\n", _("time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the"));
577 printf ("%s\n", _("percentage of packet loss to trigger an alarm state."));
581 printf ("%s\n", _("This plugin uses the ping command to probe the specified host for packet loss"));
582 printf ("%s\n", _("(percentage) and round trip average (milliseconds). It can produce HTML output"));
583 printf ("%s\n", _("linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in"));
584 printf ("%s\n", _("the contrib area of the downloads section at http://www.nagios.org/"));
588 printf (_(UT_SUPPORT
));
594 printf (_("Usage:"));
595 printf ("%s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n", progname
);
596 printf (" [-p packets] [-t timeout] [-4|-6]\n");