1 /******************************************************************************
3 * Nagios check_ping plugin
6 * Copyright (c) 2000-2006 nagios-plugins 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 * License Information:
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 ******************************************************************************/
37 const char *progname
= "check_ping";
38 const char *revision
= "$Revision$";
39 const char *copyright
= "2000-2006";
40 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
47 #define WARN_DUPLICATES "DUPLICATES FOUND! "
48 #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
51 UNKNOWN_PACKET_LOSS
= 200, /* 200% */
52 DEFAULT_MAX_PACKETS
= 5 /* default no. of ICMP ECHO packets */
55 int process_arguments (int, char **);
56 int get_threshold (char *, float *, int *);
57 int validate_arguments (void);
58 int run_ping (const char *cmd
, const char *addr
);
59 int error_scan (char buf
[MAX_INPUT_BUFFER
], const char *addr
);
60 void print_usage (void);
61 void print_help (void);
63 int display_html
= FALSE
;
64 int wpl
= UNKNOWN_PACKET_LOSS
;
65 int cpl
= UNKNOWN_PACKET_LOSS
;
66 float wrta
= UNKNOWN_TRIP_TIME
;
67 float crta
= UNKNOWN_TRIP_TIME
;
68 char **addresses
= NULL
;
74 float rta
= UNKNOWN_TRIP_TIME
;
75 int pl
= UNKNOWN_PACKET_LOSS
;
82 main (int argc
, char **argv
)
86 int result
= STATE_UNKNOWN
;
87 int this_result
= STATE_UNKNOWN
;
90 setlocale (LC_ALL
, "");
91 setlocale (LC_NUMERIC
, "C");
92 bindtextdomain (PACKAGE
, LOCALEDIR
);
95 addresses
= malloc (sizeof(char*) * max_addr
);
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);
111 alarm (timeout_interval
);
114 for (i
= 0 ; i
< n_addresses
; i
++) {
117 if (address_family
!= AF_INET
&& is_inet6_addr(addresses
[i
]))
118 rawcmd
= strdup(PING6_COMMAND
);
120 rawcmd
= strdup(PING_COMMAND
);
122 rawcmd
= strdup(PING_COMMAND
);
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
]);
130 asprintf (&cmd
, rawcmd
, max_packets
, addresses
[i
]);
133 asprintf (&cmd
, rawcmd
, addresses
[i
], max_packets
);
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
);
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
]);
161 printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result
), warn_text
,
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
)
171 printf ("%f:%d%% %f:%d%%\n", wrta
, wpl
, crta
, cpl
);
173 result
= max_state (result
, this_result
);
183 /* process command-line arguments */
185 process_arguments (int argc
, char **argv
)
191 static struct option longopts
[] = {
193 {"packets", required_argument
, 0, 'p'},
194 {"nohtml", no_argument
, 0, 'n'},
195 {"link", no_argument
, 0, 'L'},
196 {"use-ipv4", no_argument
, 0, '4'},
197 {"use-ipv6", no_argument
, 0, '6'},
204 for (c
= 1; c
< argc
; c
++) {
205 if (strcmp ("-to", argv
[c
]) == 0)
206 strcpy (argv
[c
], "-t");
207 if (strcmp ("-nohtml", argv
[c
]) == 0)
208 strcpy (argv
[c
], "-n");
212 c
= getopt_long (argc
, argv
, "VvhnL46t:c:w:H:p:", longopts
, &option
);
214 if (c
== -1 || c
== EOF
)
218 case '?': /* usage */
224 case 'V': /* version */
225 print_revision (progname
, revision
);
228 case 't': /* timeout period */
229 timeout_interval
= atoi (optarg
);
231 case 'v': /* verbose mode */
234 case '4': /* IPv4 only */
235 address_family
= AF_INET
;
237 case '6': /* IPv6 only */
239 address_family
= AF_INET6
;
241 usage (_("IPv6 support not available\n"));
244 case 'H': /* hostname */
248 if (n_addresses
> max_addr
) {
250 addresses
= realloc (addresses
, sizeof(char*) * max_addr
);
251 if (addresses
== NULL
)
252 die (STATE_UNKNOWN
, _("Could not realloc() addresses\n"));
254 addresses
[n_addresses
-1] = ptr
;
255 if ((ptr
= index (ptr
, ','))) {
263 case 'p': /* number of packets to send */
264 if (is_intnonneg (optarg
))
265 max_packets
= atoi (optarg
);
267 usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg
);
269 case 'n': /* no HTML */
270 display_html
= FALSE
;
272 case 'L': /* show HTML */
276 get_threshold (optarg
, &crta
, &cpl
);
279 get_threshold (optarg
, &wrta
, &wpl
);
286 return validate_arguments ();
288 if (addresses
[0] == NULL
) {
289 if (is_host (argv
[c
]) == FALSE
) {
290 usage2 (_("Invalid hostname/address"), argv
[c
]);
292 addresses
[0] = argv
[c
++];
295 return validate_arguments ();
299 if (wpl
== UNKNOWN_PACKET_LOSS
) {
300 if (is_intpercent (argv
[c
]) == FALSE
) {
301 printf (_("<wpl> (%s) must be an integer percentage\n"), argv
[c
]);
304 wpl
= atoi (argv
[c
++]);
306 return validate_arguments ();
310 if (cpl
== UNKNOWN_PACKET_LOSS
) {
311 if (is_intpercent (argv
[c
]) == FALSE
) {
312 printf (_("<cpl> (%s) must be an integer percentage\n"), argv
[c
]);
315 cpl
= atoi (argv
[c
++]);
317 return validate_arguments ();
322 if (is_negative (argv
[c
])) {
323 printf (_("<wrta> (%s) must be a non-negative number\n"), argv
[c
]);
326 wrta
= atof (argv
[c
++]);
328 return validate_arguments ();
333 if (is_negative (argv
[c
])) {
334 printf (_("<crta> (%s) must be a non-negative number\n"), argv
[c
]);
337 crta
= atof (argv
[c
++]);
339 return validate_arguments ();
343 if (max_packets
== -1) {
344 if (is_intnonneg (argv
[c
])) {
345 max_packets
= atoi (argv
[c
++]);
347 printf (_("<max_packets> (%s) must be a non-negative number\n"), argv
[c
]);
352 return validate_arguments ();
358 get_threshold (char *arg
, float *trta
, int *tpl
)
360 if (is_intnonneg (arg
) && sscanf (arg
, "%f", trta
) == 1)
362 else if (strpbrk (arg
, ",:") && strstr (arg
, "%") && sscanf (arg
, "%f%*[:,]%d%%", trta
, tpl
) == 2)
364 else if (strstr (arg
, "%") && sscanf (arg
, "%d%%", tpl
) == 1)
367 usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg
);
368 return STATE_UNKNOWN
;
374 validate_arguments ()
380 printf (_("<wrta> was not set\n"));
383 else if (crta
< 0.0) {
384 printf (_("<crta> was not set\n"));
387 else if (wpl
== UNKNOWN_PACKET_LOSS
) {
388 printf (_("<wpl> was not set\n"));
391 else if (cpl
== UNKNOWN_PACKET_LOSS
) {
392 printf (_("<cpl> was not set\n"));
395 else if (wrta
> crta
) {
396 printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta
, crta
);
399 else if (wpl
> cpl
) {
400 printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl
, cpl
);
404 if (max_packets
== -1)
405 max_packets
= DEFAULT_MAX_PACKETS
;
407 max_seconds
= crta
/ 1000.0 * max_packets
+ max_packets
;
408 if (max_seconds
> timeout_interval
)
409 timeout_interval
= (int)max_seconds
;
411 for (i
=0; i
<n_addresses
; i
++) {
412 if (is_host(addresses
[i
]) == FALSE
)
413 usage2 (_("Invalid hostname/address"), addresses
[i
]);
416 if (n_addresses
== 0) {
417 usage (_("You must specify a server address or host name"));
426 run_ping (const char *cmd
, const char *addr
)
428 char buf
[MAX_INPUT_BUFFER
];
429 int result
= STATE_UNKNOWN
;
431 if ((child_process
= spopen (cmd
)) == NULL
)
432 die (STATE_UNKNOWN
, _("Could not open pipe: %s\n"), cmd
);
434 child_stderr
= fdopen (child_stderr_array
[fileno (child_process
)], "r");
435 if (child_stderr
== NULL
)
436 printf (_("Cannot open stderr for %s\n"), cmd
);
438 while (fgets (buf
, MAX_INPUT_BUFFER
- 1, child_process
)) {
441 printf("Output: %s", buf
);
443 result
= max_state (result
, error_scan (buf
, addr
));
445 /* get the percent loss statistics */
446 if(sscanf(buf
,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl
)==1 ||
447 sscanf(buf
,"%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss", &pl
) == 1 ||
448 sscanf(buf
,"%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss", &pl
) == 1 ||
449 sscanf(buf
,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl
)==1 ||
450 sscanf(buf
,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl
)==1 ||
451 sscanf(buf
,"%*d packets transmitted, %*d received, %d%% loss, time", &pl
)==1 ||
452 sscanf(buf
,"%*d packets transmitted, %*d received, %d%% packet loss, time", &pl
)==1 ||
453 sscanf(buf
,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl
) == 1 ||
454 sscanf(buf
,"%*d packets transmitted %*d received, +%*d errors, %d%% packet loss", &pl
) == 1
458 /* get the round trip average */
460 if(sscanf(buf
,"round-trip min/avg/max = %*f/%f/%*f",&rta
)==1 ||
461 sscanf(buf
,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta
)==1 ||
462 sscanf(buf
,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta
)==1 ||
463 sscanf(buf
,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta
)==1 ||
464 sscanf(buf
,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta
)==1 ||
465 sscanf(buf
,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta
)==1 ||
466 sscanf(buf
,"round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f",&rta
)==1 ||
467 sscanf(buf
,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta
)==1)
471 /* this is needed because there is no rta if all packets are lost */
475 /* check stderr, setting at least WARNING if there is output here */
476 /* Add warning into warn_text */
477 while (fgets (buf
, MAX_INPUT_BUFFER
- 1, child_stderr
)) {
478 if (! strstr(buf
,"WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP")) {
480 printf("Got stderr: %s", buf
);
482 if ((result
=error_scan(buf
, addr
)) == STATE_OK
) {
483 result
= STATE_WARNING
;
484 if (warn_text
== NULL
) {
485 warn_text
= strdup(_("System call sent warnings to stderr "));
487 asprintf(&warn_text
, "%s %s", warn_text
, _("System call sent warnings to stderr "));
493 (void) fclose (child_stderr
);
496 /* close the pipe - WARNING if status is set */
497 if (spclose (child_process
))
498 result
= max_state (result
, STATE_WARNING
);
500 if (warn_text
== NULL
)
501 warn_text
= strdup("");
509 error_scan (char buf
[MAX_INPUT_BUFFER
], const char *addr
)
511 if (strstr (buf
, "Network is unreachable") ||
512 strstr (buf
, "Destination Net Unreachable")
514 die (STATE_CRITICAL
, _("CRITICAL - Network Unreachable (%s)"), addr
);
515 else if (strstr (buf
, "Destination Host Unreachable"))
516 die (STATE_CRITICAL
, _("CRITICAL - Host Unreachable (%s)"), addr
);
517 else if (strstr (buf
, "Destination Port Unreachable"))
518 die (STATE_CRITICAL
, _("CRITICAL - Bogus ICMP: Port Unreachable (%s)"), addr
);
519 else if (strstr (buf
, "Destination Protocol Unreachable"))
520 die (STATE_CRITICAL
, _("CRITICAL - Bogus ICMP: Protocol Unreachable (%s)"), addr
);
521 else if (strstr (buf
, "Destination Net Prohibited"))
522 die (STATE_CRITICAL
, _("CRITICAL - Network Prohibited (%s)"), addr
);
523 else if (strstr (buf
, "Destination Host Prohibited"))
524 die (STATE_CRITICAL
, _("CRITICAL - Host Prohibited (%s)"), addr
);
525 else if (strstr (buf
, "Packet filtered"))
526 die (STATE_CRITICAL
, _("CRITICAL - Packet Filtered (%s)"), addr
);
527 else if (strstr (buf
, "unknown host" ))
528 die (STATE_CRITICAL
, _("CRITICAL - Host not found (%s)"), addr
);
529 else if (strstr (buf
, "Time to live exceeded"))
530 die (STATE_CRITICAL
, _("CRITICAL - Time to live exceeded (%s)"), addr
);
532 if (strstr (buf
, "(DUP!)") || strstr (buf
, "DUPLICATES FOUND")) {
533 if (warn_text
== NULL
)
534 warn_text
= strdup (_(WARN_DUPLICATES
));
535 else if (! strstr (warn_text
, _(WARN_DUPLICATES
)) &&
536 asprintf (&warn_text
, "%s %s", warn_text
, _(WARN_DUPLICATES
)) == -1)
537 die (STATE_UNKNOWN
, _("Unable to realloc warn_text"));
538 return (STATE_WARNING
);
549 print_revision (progname
, revision
);
551 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>");
552 printf (COPYRIGHT
, copyright
, email
);
554 printf (_("Use ping to check connection statistics for a remote host."));
560 printf (_(UT_HELP_VRSN
));
562 printf (_(UT_IPv46
));
564 printf (" %s\n", "-H, --hostname=HOST");
565 printf (" %s\n", _("host to ping"));
566 printf (" %s\n", "-w, --warning=THRESHOLD");
567 printf (" %s\n", _("warning threshold pair"));
568 printf (" %s\n", "-c, --critical=THRESHOLD");
569 printf (" %s\n", _("critical threshold pair"));
570 printf (" %s\n", "-p, --packets=INTEGER");
571 printf (" %s ", _("number of ICMP ECHO packets to send"));
572 printf (_("(Default: %d)\n"), DEFAULT_MAX_PACKETS
);
573 printf (" %s\n", "-L, --link");
574 printf (" %s\n", _("show HTML in the plugin output (obsoleted by urlize)"));
576 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
578 printf ("%s\n", _("THRESHOLD is <rta>,<pl>% where <rta> is the round trip average travel"));
579 printf ("%s\n", _("time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the"));
580 printf ("%s\n", _("percentage of packet loss to trigger an alarm state."));
584 printf ("%s\n", _("This plugin uses the ping command to probe the specified host for packet loss"));
585 printf ("%s\n", _("(percentage) and round trip average (milliseconds). It can produce HTML output"));
586 printf ("%s\n", _("linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in"));
587 printf ("%s\n", _("the contrib area of the downloads section at http://www.nagios.org/"));
591 printf (_(UT_SUPPORT
));
597 printf (_("Usage:"));
598 printf ("%s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n", progname
);
599 printf (" [-p packets] [-t timeout] [-4|-6]\n");