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 /* 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);
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
)
169 /* Print performance data */
170 printf("|%s", fperfdata ("rta", (double) rta
, "ms",
171 wrta
>0?TRUE
:FALSE
, wrta
,
172 crta
>0?TRUE
:FALSE
, crta
,
174 printf(" %s\n", perfdata ("pl", (long) pl
, "%",
175 wpl
>0?TRUE
:FALSE
, wpl
,
176 cpl
>0?TRUE
:FALSE
, cpl
,
180 printf ("%f:%d%% %f:%d%%\n", wrta
, wpl
, crta
, cpl
);
182 result
= max_state (result
, this_result
);
192 /* process command-line arguments */
194 process_arguments (int argc
, char **argv
)
200 static struct option longopts
[] = {
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'},
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");
221 c
= getopt_long (argc
, argv
, "VvhnL46t:c:w:H:p:", longopts
, &option
);
223 if (c
== -1 || c
== EOF
)
227 case '?': /* usage */
233 case 'V': /* version */
234 print_revision (progname
, revision
);
237 case 't': /* timeout period */
238 timeout_interval
= atoi (optarg
);
240 case 'v': /* verbose mode */
243 case '4': /* IPv4 only */
244 address_family
= AF_INET
;
246 case '6': /* IPv6 only */
248 address_family
= AF_INET6
;
250 usage (_("IPv6 support not available\n"));
253 case 'H': /* hostname */
257 if (n_addresses
> max_addr
) {
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
, ','))) {
272 case 'p': /* number of packets to send */
273 if (is_intnonneg (optarg
))
274 max_packets
= atoi (optarg
);
276 usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg
);
278 case 'n': /* no HTML */
279 display_html
= FALSE
;
281 case 'L': /* show HTML */
285 get_threshold (optarg
, &crta
, &cpl
);
288 get_threshold (optarg
, &wrta
, &wpl
);
295 return validate_arguments ();
297 if (addresses
[0] == NULL
) {
298 if (is_host (argv
[c
]) == FALSE
) {
299 usage2 (_("Invalid hostname/address"), argv
[c
]);
301 addresses
[0] = argv
[c
++];
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
]);
313 wpl
= atoi (argv
[c
++]);
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
]);
324 cpl
= atoi (argv
[c
++]);
326 return validate_arguments ();
331 if (is_negative (argv
[c
])) {
332 printf (_("<wrta> (%s) must be a non-negative number\n"), argv
[c
]);
335 wrta
= atof (argv
[c
++]);
337 return validate_arguments ();
342 if (is_negative (argv
[c
])) {
343 printf (_("<crta> (%s) must be a non-negative number\n"), argv
[c
]);
346 crta
= atof (argv
[c
++]);
348 return validate_arguments ();
352 if (max_packets
== -1) {
353 if (is_intnonneg (argv
[c
])) {
354 max_packets
= atoi (argv
[c
++]);
356 printf (_("<max_packets> (%s) must be a non-negative number\n"), argv
[c
]);
361 return validate_arguments ();
367 get_threshold (char *arg
, float *trta
, int *tpl
)
369 if (is_intnonneg (arg
) && sscanf (arg
, "%f", trta
) == 1)
371 else if (strpbrk (arg
, ",:") && strstr (arg
, "%") && sscanf (arg
, "%f%*[:,]%d%%", trta
, tpl
) == 2)
373 else if (strstr (arg
, "%") && sscanf (arg
, "%d%%", tpl
) == 1)
376 usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg
);
377 return STATE_UNKNOWN
;
383 validate_arguments ()
389 printf (_("<wrta> was not set\n"));
392 else if (crta
< 0.0) {
393 printf (_("<crta> was not set\n"));
396 else if (wpl
== UNKNOWN_PACKET_LOSS
) {
397 printf (_("<wpl> was not set\n"));
400 else if (cpl
== UNKNOWN_PACKET_LOSS
) {
401 printf (_("<cpl> was not set\n"));
404 else if (wrta
> crta
) {
405 printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta
, crta
);
408 else if (wpl
> cpl
) {
409 printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl
, cpl
);
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"));
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
)) {
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
467 /* get the round trip average */
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)
480 /* this is needed because there is no rta if all packets are lost */
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")) {
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 "));
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("");
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
);
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."));
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
);
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."));
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/"));
601 printf ("%s\n", _("Notes:"));
602 printf (_(UT_EXTRA_OPTS_NOTES
));
605 printf (_(UT_SUPPORT
));
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");