Set the pointers to the SSL and SSL_CTX objects back to NULL after
[monitoring-plugins.git] / plugins / check_ping.c
blobbf726d829a85b7d277f035bc4507502adfb8bdf6
1 /******************************************************************************
3 * Nagios check_ping plugin
5 * License: GPL
6 * Copyright (c) 2000-2006 nagios-plugins team
8 * Last Modified: $Date$
10 * Description:
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.
33 $Id$
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";
42 #include "common.h"
43 #include "netutils.h"
44 #include "popen.h"
45 #include "utils.h"
47 #define WARN_DUPLICATES "DUPLICATES FOUND! "
48 #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
50 enum {
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;
69 int n_addresses = 0;
70 int max_addr = 1;
71 int max_packets = -1;
72 int verbose = 0;
74 float rta = UNKNOWN_TRIP_TIME;
75 int pl = UNKNOWN_PACKET_LOSS;
77 char *warn_text;
81 int
82 main (int argc, char **argv)
84 char *cmd = NULL;
85 char *rawcmd = NULL;
86 int result = STATE_UNKNOWN;
87 int this_result = STATE_UNKNOWN;
88 int i;
90 setlocale (LC_ALL, "");
91 setlocale (LC_NUMERIC, "C");
92 bindtextdomain (PACKAGE, LOCALEDIR);
93 textdomain (PACKAGE);
95 addresses = malloc (sizeof(char*) * max_addr);
96 addresses[0] = NULL;
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>");
168 printf ("\n");
170 if (verbose >= 2)
171 printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
173 result = max_state (result, this_result);
174 free (rawcmd);
175 free (cmd);
178 return result;
183 /* process command-line arguments */
185 process_arguments (int argc, char **argv)
187 int c = 1;
188 char *ptr;
190 int option = 0;
191 static struct option longopts[] = {
192 STD_LONG_OPTS,
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'},
198 {0, 0, 0, 0}
201 if (argc < 2)
202 return ERROR;
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");
211 while (1) {
212 c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
214 if (c == -1 || c == EOF)
215 break;
217 switch (c) {
218 case '?': /* usage */
219 usage5 ();
220 case 'h': /* help */
221 print_help ();
222 exit (STATE_OK);
223 break;
224 case 'V': /* version */
225 print_revision (progname, revision);
226 exit (STATE_OK);
227 break;
228 case 't': /* timeout period */
229 timeout_interval = atoi (optarg);
230 break;
231 case 'v': /* verbose mode */
232 verbose++;
233 break;
234 case '4': /* IPv4 only */
235 address_family = AF_INET;
236 break;
237 case '6': /* IPv6 only */
238 #ifdef USE_IPV6
239 address_family = AF_INET6;
240 #else
241 usage (_("IPv6 support not available\n"));
242 #endif
243 break;
244 case 'H': /* hostname */
245 ptr=optarg;
246 while (1) {
247 n_addresses++;
248 if (n_addresses > max_addr) {
249 max_addr *= 2;
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, ','))) {
256 strcpy (ptr, "");
257 ptr += sizeof(char);
258 } else {
259 break;
262 break;
263 case 'p': /* number of packets to send */
264 if (is_intnonneg (optarg))
265 max_packets = atoi (optarg);
266 else
267 usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg);
268 break;
269 case 'n': /* no HTML */
270 display_html = FALSE;
271 break;
272 case 'L': /* show HTML */
273 display_html = TRUE;
274 break;
275 case 'c':
276 get_threshold (optarg, &crta, &cpl);
277 break;
278 case 'w':
279 get_threshold (optarg, &wrta, &wpl);
280 break;
284 c = optind;
285 if (c == argc)
286 return validate_arguments ();
288 if (addresses[0] == NULL) {
289 if (is_host (argv[c]) == FALSE) {
290 usage2 (_("Invalid hostname/address"), argv[c]);
291 } else {
292 addresses[0] = argv[c++];
293 n_addresses++;
294 if (c == argc)
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]);
302 return ERROR;
303 } else {
304 wpl = atoi (argv[c++]);
305 if (c == argc)
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]);
313 return ERROR;
314 } else {
315 cpl = atoi (argv[c++]);
316 if (c == argc)
317 return validate_arguments ();
321 if (wrta < 0.0) {
322 if (is_negative (argv[c])) {
323 printf (_("<wrta> (%s) must be a non-negative number\n"), argv[c]);
324 return ERROR;
325 } else {
326 wrta = atof (argv[c++]);
327 if (c == argc)
328 return validate_arguments ();
332 if (crta < 0.0) {
333 if (is_negative (argv[c])) {
334 printf (_("<crta> (%s) must be a non-negative number\n"), argv[c]);
335 return ERROR;
336 } else {
337 crta = atof (argv[c++]);
338 if (c == argc)
339 return validate_arguments ();
343 if (max_packets == -1) {
344 if (is_intnonneg (argv[c])) {
345 max_packets = atoi (argv[c++]);
346 } else {
347 printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]);
348 return ERROR;
352 return validate_arguments ();
358 get_threshold (char *arg, float *trta, int *tpl)
360 if (is_intnonneg (arg) && sscanf (arg, "%f", trta) == 1)
361 return OK;
362 else if (strpbrk (arg, ",:") && strstr (arg, "%") && sscanf (arg, "%f%*[:,]%d%%", trta, tpl) == 2)
363 return OK;
364 else if (strstr (arg, "%") && sscanf (arg, "%d%%", tpl) == 1)
365 return OK;
367 usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
368 return STATE_UNKNOWN;
374 validate_arguments ()
376 float max_seconds;
377 int i;
379 if (wrta < 0.0) {
380 printf (_("<wrta> was not set\n"));
381 return ERROR;
383 else if (crta < 0.0) {
384 printf (_("<crta> was not set\n"));
385 return ERROR;
387 else if (wpl == UNKNOWN_PACKET_LOSS) {
388 printf (_("<wpl> was not set\n"));
389 return ERROR;
391 else if (cpl == UNKNOWN_PACKET_LOSS) {
392 printf (_("<cpl> was not set\n"));
393 return ERROR;
395 else if (wrta > crta) {
396 printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta, crta);
397 return ERROR;
399 else if (wpl > cpl) {
400 printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl, cpl);
401 return ERROR;
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"));
420 return OK;
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)) {
440 if (verbose >= 3)
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
456 continue;
458 /* get the round trip average */
459 else
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)
468 continue;
471 /* this is needed because there is no rta if all packets are lost */
472 if (pl == 100)
473 rta = crta;
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")) {
479 if (verbose >= 3) {
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 "));
486 } else {
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("");
503 return result;
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);
541 return (STATE_OK);
546 void
547 print_help (void)
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."));
556 printf ("\n\n");
558 print_usage ();
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."));
582 printf ("\n\n");
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/"));
589 printf ("\n\n");
591 printf (_(UT_SUPPORT));
594 void
595 print_usage (void)
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");