check_ping.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /******************************************************************************
  2. *
  3. * Nagios check_ping plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2006 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_ping plugin
  13. *
  14. * Use the ping program to check connection statistics for a remote host.
  15. *
  16. *
  17. * License Information:
  18. *
  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.
  23. *
  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.
  28. *
  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.
  32. $Id$
  33. ******************************************************************************/
  34. const char *progname = "check_ping";
  35. const char *revision = "$Revision$";
  36. const char *copyright = "2000-2006";
  37. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  38. #include "common.h"
  39. #include "netutils.h"
  40. #include "popen.h"
  41. #include "utils.h"
  42. #define WARN_DUPLICATES "DUPLICATES FOUND! "
  43. #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
  44. enum {
  45. UNKNOWN_PACKET_LOSS = 200, /* 200% */
  46. DEFAULT_MAX_PACKETS = 5 /* default no. of ICMP ECHO packets */
  47. };
  48. int process_arguments (int, char **);
  49. int get_threshold (char *, float *, int *);
  50. int validate_arguments (void);
  51. int run_ping (const char *cmd, const char *addr);
  52. int error_scan (char buf[MAX_INPUT_BUFFER], const char *addr);
  53. void print_usage (void);
  54. void print_help (void);
  55. int display_html = FALSE;
  56. int wpl = UNKNOWN_PACKET_LOSS;
  57. int cpl = UNKNOWN_PACKET_LOSS;
  58. float wrta = UNKNOWN_TRIP_TIME;
  59. float crta = UNKNOWN_TRIP_TIME;
  60. char **addresses = NULL;
  61. int n_addresses = 0;
  62. int max_addr = 1;
  63. int max_packets = -1;
  64. int verbose = 0;
  65. float rta = UNKNOWN_TRIP_TIME;
  66. int pl = UNKNOWN_PACKET_LOSS;
  67. char *warn_text;
  68. int
  69. main (int argc, char **argv)
  70. {
  71. char *cmd = NULL;
  72. char *rawcmd = NULL;
  73. int result = STATE_UNKNOWN;
  74. int this_result = STATE_UNKNOWN;
  75. int i;
  76. setlocale (LC_ALL, "");
  77. setlocale (LC_NUMERIC, "C");
  78. bindtextdomain (PACKAGE, LOCALEDIR);
  79. textdomain (PACKAGE);
  80. addresses = malloc (sizeof(char*) * max_addr);
  81. addresses[0] = NULL;
  82. if (process_arguments (argc, argv) == ERROR)
  83. usage4 (_("Could not parse arguments"));
  84. /* Set signal handling and alarm */
  85. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  86. usage4 (_("Cannot catch SIGALRM"));
  87. }
  88. /* handle timeouts gracefully */
  89. alarm (timeout_interval);
  90. for (i = 0 ; i < n_addresses ; i++) {
  91. #ifdef PING6_COMMAND
  92. if (address_family != AF_INET && is_inet6_addr(addresses[i]))
  93. rawcmd = strdup(PING6_COMMAND);
  94. else
  95. rawcmd = strdup(PING_COMMAND);
  96. #else
  97. rawcmd = strdup(PING_COMMAND);
  98. #endif
  99. /* does the host address of number of packets argument come first? */
  100. #ifdef PING_PACKETS_FIRST
  101. # ifdef PING_HAS_TIMEOUT
  102. asprintf (&cmd, rawcmd, timeout_interval, max_packets, addresses[i]);
  103. # else
  104. asprintf (&cmd, rawcmd, max_packets, addresses[i]);
  105. # endif
  106. #else
  107. asprintf (&cmd, rawcmd, addresses[i], max_packets);
  108. #endif
  109. if (verbose >= 2)
  110. printf ("CMD: %s\n", cmd);
  111. /* run the command */
  112. this_result = run_ping (cmd, addresses[i]);
  113. if (pl == UNKNOWN_PACKET_LOSS || rta < 0.0) {
  114. printf ("%s\n", cmd);
  115. die (STATE_UNKNOWN,
  116. _("CRITICAL - Could not interpret output from ping command\n"));
  117. }
  118. if (pl >= cpl || rta >= crta || rta < 0)
  119. this_result = STATE_CRITICAL;
  120. else if (pl >= wpl || rta >= wrta)
  121. this_result = STATE_WARNING;
  122. else if (pl >= 0 && rta >= 0)
  123. this_result = max_state (STATE_OK, this_result);
  124. if (n_addresses > 1 && this_result != STATE_UNKNOWN)
  125. die (STATE_OK, "%s is alive\n", addresses[i]);
  126. if (display_html == TRUE)
  127. printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]);
  128. if (pl == 100)
  129. printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text,
  130. pl);
  131. else
  132. printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"),
  133. state_text (this_result), warn_text, pl, rta);
  134. if (display_html == TRUE)
  135. printf ("</A>");
  136. printf ("\n");
  137. if (verbose >= 2)
  138. printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
  139. result = max_state (result, this_result);
  140. free (rawcmd);
  141. free (cmd);
  142. }
  143. return result;
  144. }
  145. /* process command-line arguments */
  146. int
  147. process_arguments (int argc, char **argv)
  148. {
  149. int c = 1;
  150. char *ptr;
  151. int option = 0;
  152. static struct option longopts[] = {
  153. STD_LONG_OPTS,
  154. {"packets", required_argument, 0, 'p'},
  155. {"nohtml", no_argument, 0, 'n'},
  156. {"link", no_argument, 0, 'L'},
  157. {"use-ipv4", no_argument, 0, '4'},
  158. {"use-ipv6", no_argument, 0, '6'},
  159. {0, 0, 0, 0}
  160. };
  161. if (argc < 2)
  162. return ERROR;
  163. for (c = 1; c < argc; c++) {
  164. if (strcmp ("-to", argv[c]) == 0)
  165. strcpy (argv[c], "-t");
  166. if (strcmp ("-nohtml", argv[c]) == 0)
  167. strcpy (argv[c], "-n");
  168. }
  169. while (1) {
  170. c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
  171. if (c == -1 || c == EOF)
  172. break;
  173. switch (c) {
  174. case '?': /* usage */
  175. usage2 (_("Unknown argument"), optarg);
  176. case 'h': /* help */
  177. print_help ();
  178. exit (STATE_OK);
  179. break;
  180. case 'V': /* version */
  181. print_revision (progname, revision);
  182. exit (STATE_OK);
  183. break;
  184. case 't': /* timeout period */
  185. timeout_interval = atoi (optarg);
  186. break;
  187. case 'v': /* verbose mode */
  188. verbose++;
  189. break;
  190. case '4': /* IPv4 only */
  191. address_family = AF_INET;
  192. break;
  193. case '6': /* IPv6 only */
  194. #ifdef USE_IPV6
  195. address_family = AF_INET6;
  196. #else
  197. usage (_("IPv6 support not available\n"));
  198. #endif
  199. break;
  200. case 'H': /* hostname */
  201. ptr=optarg;
  202. while (1) {
  203. n_addresses++;
  204. if (n_addresses > max_addr) {
  205. max_addr *= 2;
  206. addresses = realloc (addresses, sizeof(char*) * max_addr);
  207. if (addresses == NULL)
  208. die (STATE_UNKNOWN, _("Could not realloc() addresses\n"));
  209. }
  210. addresses[n_addresses-1] = ptr;
  211. if ((ptr = index (ptr, ','))) {
  212. strcpy (ptr, "");
  213. ptr += sizeof(char);
  214. } else {
  215. break;
  216. }
  217. }
  218. break;
  219. case 'p': /* number of packets to send */
  220. if (is_intnonneg (optarg))
  221. max_packets = atoi (optarg);
  222. else
  223. usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg);
  224. break;
  225. case 'n': /* no HTML */
  226. display_html = FALSE;
  227. break;
  228. case 'L': /* show HTML */
  229. display_html = TRUE;
  230. break;
  231. case 'c':
  232. get_threshold (optarg, &crta, &cpl);
  233. break;
  234. case 'w':
  235. get_threshold (optarg, &wrta, &wpl);
  236. break;
  237. }
  238. }
  239. c = optind;
  240. if (c == argc)
  241. return validate_arguments ();
  242. if (addresses[0] == NULL) {
  243. if (is_host (argv[c]) == FALSE) {
  244. usage2 (_("Invalid hostname/address"), argv[c]);
  245. } else {
  246. addresses[0] = argv[c++];
  247. n_addresses++;
  248. if (c == argc)
  249. return validate_arguments ();
  250. }
  251. }
  252. if (wpl == UNKNOWN_PACKET_LOSS) {
  253. if (is_intpercent (argv[c]) == FALSE) {
  254. printf (_("<wpl> (%s) must be an integer percentage\n"), argv[c]);
  255. return ERROR;
  256. } else {
  257. wpl = atoi (argv[c++]);
  258. if (c == argc)
  259. return validate_arguments ();
  260. }
  261. }
  262. if (cpl == UNKNOWN_PACKET_LOSS) {
  263. if (is_intpercent (argv[c]) == FALSE) {
  264. printf (_("<cpl> (%s) must be an integer percentage\n"), argv[c]);
  265. return ERROR;
  266. } else {
  267. cpl = atoi (argv[c++]);
  268. if (c == argc)
  269. return validate_arguments ();
  270. }
  271. }
  272. if (wrta < 0.0) {
  273. if (is_negative (argv[c])) {
  274. printf (_("<wrta> (%s) must be a non-negative number\n"), argv[c]);
  275. return ERROR;
  276. } else {
  277. wrta = atof (argv[c++]);
  278. if (c == argc)
  279. return validate_arguments ();
  280. }
  281. }
  282. if (crta < 0.0) {
  283. if (is_negative (argv[c])) {
  284. printf (_("<crta> (%s) must be a non-negative number\n"), argv[c]);
  285. return ERROR;
  286. } else {
  287. crta = atof (argv[c++]);
  288. if (c == argc)
  289. return validate_arguments ();
  290. }
  291. }
  292. if (max_packets == -1) {
  293. if (is_intnonneg (argv[c])) {
  294. max_packets = atoi (argv[c++]);
  295. } else {
  296. printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]);
  297. return ERROR;
  298. }
  299. }
  300. return validate_arguments ();
  301. }
  302. int
  303. get_threshold (char *arg, float *trta, int *tpl)
  304. {
  305. if (is_intnonneg (arg) && sscanf (arg, "%f", trta) == 1)
  306. return OK;
  307. else if (strpbrk (arg, ",:") && strstr (arg, "%") && sscanf (arg, "%f%*[:,]%d%%", trta, tpl) == 2)
  308. return OK;
  309. else if (strstr (arg, "%") && sscanf (arg, "%d%%", tpl) == 1)
  310. return OK;
  311. usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
  312. return STATE_UNKNOWN;
  313. }
  314. int
  315. validate_arguments ()
  316. {
  317. float max_seconds;
  318. int i;
  319. if (wrta < 0.0) {
  320. printf (_("<wrta> was not set\n"));
  321. return ERROR;
  322. }
  323. else if (crta < 0.0) {
  324. printf (_("<crta> was not set\n"));
  325. return ERROR;
  326. }
  327. else if (wpl == UNKNOWN_PACKET_LOSS) {
  328. printf (_("<wpl> was not set\n"));
  329. return ERROR;
  330. }
  331. else if (cpl == UNKNOWN_PACKET_LOSS) {
  332. printf (_("<cpl> was not set\n"));
  333. return ERROR;
  334. }
  335. else if (wrta > crta) {
  336. printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta, crta);
  337. return ERROR;
  338. }
  339. else if (wpl > cpl) {
  340. printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl, cpl);
  341. return ERROR;
  342. }
  343. if (max_packets == -1)
  344. max_packets = DEFAULT_MAX_PACKETS;
  345. max_seconds = crta / 1000.0 * max_packets + max_packets;
  346. if (max_seconds > timeout_interval)
  347. timeout_interval = (int)max_seconds;
  348. for (i=0; i<n_addresses; i++) {
  349. if (is_host(addresses[i]) == FALSE)
  350. usage2 (_("Invalid hostname/address"), addresses[i]);
  351. }
  352. if (n_addresses == 0) {
  353. usage (_("You must specify a server address or host name"));
  354. }
  355. return OK;
  356. }
  357. int
  358. run_ping (const char *cmd, const char *addr)
  359. {
  360. char buf[MAX_INPUT_BUFFER];
  361. int result = STATE_UNKNOWN;
  362. if ((child_process = spopen (cmd)) == NULL)
  363. die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd);
  364. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  365. if (child_stderr == NULL)
  366. printf (_("Cannot open stderr for %s\n"), cmd);
  367. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
  368. if (verbose >= 3)
  369. printf("Output: %s", buf);
  370. result = max_state (result, error_scan (buf, addr));
  371. /* get the percent loss statistics */
  372. if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 ||
  373. sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss", &pl) == 1 ||
  374. sscanf(buf,"%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss", &pl) == 1 ||
  375. sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 ||
  376. sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 ||
  377. sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1 ||
  378. sscanf(buf,"%*d packets transmitted, %*d received, %d%% packet loss, time", &pl)==1 ||
  379. sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl) == 1 ||
  380. sscanf(buf,"%*d packets transmitted %*d received, +%*d errors, %d%% packet loss", &pl) == 1
  381. )
  382. continue;
  383. /* get the round trip average */
  384. else
  385. if(sscanf(buf,"round-trip min/avg/max = %*f/%f/%*f",&rta)==1 ||
  386. sscanf(buf,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta)==1 ||
  387. sscanf(buf,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta)==1 ||
  388. sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
  389. sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta)==1 ||
  390. sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta)==1 ||
  391. sscanf(buf,"round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
  392. sscanf(buf,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta)==1)
  393. continue;
  394. }
  395. /* this is needed because there is no rta if all packets are lost */
  396. if (pl == 100)
  397. rta = crta;
  398. /* check stderr, setting at least WARNING if there is output here */
  399. /* Add warning into warn_text */
  400. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) {
  401. if (! strstr(buf,"WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP")) {
  402. if (verbose >= 3) {
  403. printf("Got stderr: %s", buf);
  404. }
  405. if ((result=error_scan(buf, addr)) == STATE_OK) {
  406. result = STATE_WARNING;
  407. if (warn_text == NULL) {
  408. warn_text = strdup(_("System call sent warnings to stderr "));
  409. } else {
  410. asprintf(&warn_text, "%s %s", warn_text, _("System call sent warnings to stderr "));
  411. }
  412. }
  413. }
  414. }
  415. (void) fclose (child_stderr);
  416. /* close the pipe - WARNING if status is set */
  417. if (spclose (child_process))
  418. result = max_state (result, STATE_WARNING);
  419. if (warn_text == NULL)
  420. warn_text = strdup("");
  421. return result;
  422. }
  423. int
  424. error_scan (char buf[MAX_INPUT_BUFFER], const char *addr)
  425. {
  426. if (strstr (buf, "Network is unreachable"))
  427. die (STATE_CRITICAL, _("CRITICAL - Network unreachable (%s)"), addr);
  428. else if (strstr (buf, "Destination Host Unreachable"))
  429. die (STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)"), addr);
  430. else if (strstr (buf, "unknown host" ))
  431. die (STATE_CRITICAL, _("CRITICAL - Host not found (%s)"), addr);
  432. else if (strstr (buf, "Time to live exceeded"))
  433. die (STATE_CRITICAL, _("CRITICAL - Time to live exceeded (%s)"), addr);
  434. if (strstr (buf, "(DUP!)") || strstr (buf, "DUPLICATES FOUND")) {
  435. if (warn_text == NULL)
  436. warn_text = strdup (_(WARN_DUPLICATES));
  437. else if (! strstr (warn_text, _(WARN_DUPLICATES)) &&
  438. asprintf (&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1)
  439. die (STATE_UNKNOWN, _("Unable to realloc warn_text"));
  440. return (STATE_WARNING);
  441. }
  442. return (STATE_OK);
  443. }
  444. void
  445. print_help (void)
  446. {
  447. print_revision (progname, revision);
  448. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>");
  449. printf (COPYRIGHT, copyright, email);
  450. printf (_("Use ping to check connection statistics for a remote host."));
  451. printf ("\n\n");
  452. print_usage ();
  453. printf (_(UT_HELP_VRSN));
  454. printf (_(UT_IPv46));
  455. printf (" %s\n", "-H, --hostname=HOST");
  456. printf (" %s\n", _("host to ping"));
  457. printf (" %s\n", "-w, --warning=THRESHOLD");
  458. printf (" %s\n", _("warning threshold pair"));
  459. printf (" %s\n", "-c, --critical=THRESHOLD");
  460. printf (" %s\n", _("critical threshold pair"));
  461. printf (" %s\n", "-p, --packets=INTEGER");
  462. printf (" %s\n", _("number of ICMP ECHO packets to send"));
  463. printf (_("(Default: %d)"), DEFAULT_MAX_PACKETS);
  464. printf (" %s\n", "-L, --link");
  465. printf (" %s\n", _("show HTML in the plugin output (obsoleted by urlize)"));
  466. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  467. printf ("%s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel"));
  468. printf ("%s\n", _("time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the"));
  469. printf ("%s\n", _("percentage of packet loss to trigger an alarm state."));
  470. printf ("\n\n");
  471. printf ("%s\n", _("This plugin uses the ping command to probe the specified host for packet loss"));
  472. printf ("%s\n", _("(percentage) and round trip average (milliseconds). It can produce HTML output"));
  473. printf ("%s\n", _("linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in"));
  474. printf ("%s\n", _("the contrib area of the downloads section at http://www.nagios.org/"));
  475. printf ("\n\n");
  476. printf (_(UT_SUPPORT));
  477. }
  478. void
  479. print_usage (void)
  480. {
  481. printf (_("Usage:"));
  482. printf ("%s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n", progname);
  483. printf (" [-p packets] [-t timeout] [-L] [-4|-6]\n");
  484. }