Reverted check_procs for solaris back to using pst3 due to truncation
[monitoring-plugins.git] / plugins / check_ping.c
blob214e23b01dfdce8ffd74475418f0113683fa6aa0
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 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);
107 #else
108 alarm (timeout_interval);
109 #endif
111 for (i = 0 ; i < n_addresses ; i++) {
113 #ifdef PING6_COMMAND
114 if (address_family != AF_INET && is_inet6_addr(addresses[i]))
115 rawcmd = strdup(PING6_COMMAND);
116 else
117 rawcmd = strdup(PING_COMMAND);
118 #else
119 rawcmd = strdup(PING_COMMAND);
120 #endif
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]);
126 # else
127 asprintf (&cmd, rawcmd, max_packets, addresses[i]);
128 # endif
129 #else
130 asprintf (&cmd, rawcmd, addresses[i], max_packets);
131 #endif
133 if (verbose >= 2)
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);
141 die (STATE_UNKNOWN,
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]);
157 if (pl == 100)
158 printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text,
159 pl);
160 else
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)
164 printf ("</A>");
165 printf ("\n");
167 if (verbose >= 2)
168 printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
170 result = max_state (result, this_result);
171 free (rawcmd);
172 free (cmd);
175 return result;
180 /* process command-line arguments */
182 process_arguments (int argc, char **argv)
184 int c = 1;
185 char *ptr;
187 int option = 0;
188 static struct option longopts[] = {
189 STD_LONG_OPTS,
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'},
195 {0, 0, 0, 0}
198 if (argc < 2)
199 return ERROR;
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");
208 while (1) {
209 c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
211 if (c == -1 || c == EOF)
212 break;
214 switch (c) {
215 case '?': /* usage */
216 usage5 ();
217 case 'h': /* help */
218 print_help ();
219 exit (STATE_OK);
220 break;
221 case 'V': /* version */
222 print_revision (progname, revision);
223 exit (STATE_OK);
224 break;
225 case 't': /* timeout period */
226 timeout_interval = atoi (optarg);
227 break;
228 case 'v': /* verbose mode */
229 verbose++;
230 break;
231 case '4': /* IPv4 only */
232 address_family = AF_INET;
233 break;
234 case '6': /* IPv6 only */
235 #ifdef USE_IPV6
236 address_family = AF_INET6;
237 #else
238 usage (_("IPv6 support not available\n"));
239 #endif
240 break;
241 case 'H': /* hostname */
242 ptr=optarg;
243 while (1) {
244 n_addresses++;
245 if (n_addresses > max_addr) {
246 max_addr *= 2;
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, ','))) {
253 strcpy (ptr, "");
254 ptr += sizeof(char);
255 } else {
256 break;
259 break;
260 case 'p': /* number of packets to send */
261 if (is_intnonneg (optarg))
262 max_packets = atoi (optarg);
263 else
264 usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg);
265 break;
266 case 'n': /* no HTML */
267 display_html = FALSE;
268 break;
269 case 'L': /* show HTML */
270 display_html = TRUE;
271 break;
272 case 'c':
273 get_threshold (optarg, &crta, &cpl);
274 break;
275 case 'w':
276 get_threshold (optarg, &wrta, &wpl);
277 break;
281 c = optind;
282 if (c == argc)
283 return validate_arguments ();
285 if (addresses[0] == NULL) {
286 if (is_host (argv[c]) == FALSE) {
287 usage2 (_("Invalid hostname/address"), argv[c]);
288 } else {
289 addresses[0] = argv[c++];
290 n_addresses++;
291 if (c == argc)
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]);
299 return ERROR;
300 } else {
301 wpl = atoi (argv[c++]);
302 if (c == argc)
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]);
310 return ERROR;
311 } else {
312 cpl = atoi (argv[c++]);
313 if (c == argc)
314 return validate_arguments ();
318 if (wrta < 0.0) {
319 if (is_negative (argv[c])) {
320 printf (_("<wrta> (%s) must be a non-negative number\n"), argv[c]);
321 return ERROR;
322 } else {
323 wrta = atof (argv[c++]);
324 if (c == argc)
325 return validate_arguments ();
329 if (crta < 0.0) {
330 if (is_negative (argv[c])) {
331 printf (_("<crta> (%s) must be a non-negative number\n"), argv[c]);
332 return ERROR;
333 } else {
334 crta = atof (argv[c++]);
335 if (c == argc)
336 return validate_arguments ();
340 if (max_packets == -1) {
341 if (is_intnonneg (argv[c])) {
342 max_packets = atoi (argv[c++]);
343 } else {
344 printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]);
345 return ERROR;
349 return validate_arguments ();
355 get_threshold (char *arg, float *trta, int *tpl)
357 if (is_intnonneg (arg) && sscanf (arg, "%f", trta) == 1)
358 return OK;
359 else if (strpbrk (arg, ",:") && strstr (arg, "%") && sscanf (arg, "%f%*[:,]%d%%", trta, tpl) == 2)
360 return OK;
361 else if (strstr (arg, "%") && sscanf (arg, "%d%%", tpl) == 1)
362 return OK;
364 usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
365 return STATE_UNKNOWN;
371 validate_arguments ()
373 float max_seconds;
374 int i;
376 if (wrta < 0.0) {
377 printf (_("<wrta> was not set\n"));
378 return ERROR;
380 else if (crta < 0.0) {
381 printf (_("<crta> was not set\n"));
382 return ERROR;
384 else if (wpl == UNKNOWN_PACKET_LOSS) {
385 printf (_("<wpl> was not set\n"));
386 return ERROR;
388 else if (cpl == UNKNOWN_PACKET_LOSS) {
389 printf (_("<cpl> was not set\n"));
390 return ERROR;
392 else if (wrta > crta) {
393 printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta, crta);
394 return ERROR;
396 else if (wpl > cpl) {
397 printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl, cpl);
398 return ERROR;
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"));
417 return OK;
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)) {
437 if (verbose >= 3)
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
453 continue;
455 /* get the round trip average */
456 else
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)
465 continue;
468 /* this is needed because there is no rta if all packets are lost */
469 if (pl == 100)
470 rta = crta;
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")) {
476 if (verbose >= 3) {
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 "));
483 } else {
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("");
500 return result;
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);
538 return (STATE_OK);
543 void
544 print_help (void)
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."));
553 printf ("\n\n");
555 print_usage ();
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."));
579 printf ("\n\n");
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/"));
586 printf ("\n\n");
588 printf (_(UT_SUPPORT));
591 void
592 print_usage (void)
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");