check_ping.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*****************************************************************************
  2. *
  3. * CHECK_PING.C
  4. *
  5. * Program: Ping plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * $Id$
  10. *
  11. *****************************************************************************/
  12. const char *progname = "check_ping";
  13. #define REVISION "$Revision$"
  14. #define COPYRIGHT "1999-2001"
  15. #define AUTHOR "Ethan Galstad/Karl DeBisschop"
  16. #define EMAIL "kdebisschop@users.sourceforge.net"
  17. #define SUMMARY "Use ping to check connection statistics for a remote host.\n"
  18. #define OPTIONS "\
  19. -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n\
  20. [-p packets] [-t timeout] [-L] [-4|-6]\n"
  21. #define LONGOPTIONS "\
  22. -H, --hostname=HOST\n\
  23. host to ping\n\
  24. -4, --use-ipv4\n\
  25. Use IPv4 ICMP PING\n\
  26. -6, --use-ipv6\n\
  27. Use IPv6 ICMP PING\n\
  28. -w, --warning=THRESHOLD\n\
  29. warning threshold pair\n\
  30. -c, --critical=THRESHOLD\n\
  31. critical threshold pair\n\
  32. -p, --packets=INTEGER\n\
  33. number of ICMP ECHO packets to send (Default: %d)\n\
  34. -t, --timeout=INTEGER\n\
  35. optional specified timeout in second (Default: %d)\n\
  36. -L, --link\n\
  37. show HTML in the plugin output (obsoleted by urlize)\n\
  38. THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
  39. time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
  40. percentage of packet loss to trigger an alarm state.\n"
  41. #define DESCRIPTION "\
  42. This plugin uses the ping command to probe the specified host for packet loss\n\
  43. (percentage) and round trip average (milliseconds). It can produce HTML output\n\
  44. linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in\n\
  45. the contrib area of the downloads section at http://www.nagios.org\n\n"
  46. #include "config.h"
  47. #include "common.h"
  48. #include "netutils.h"
  49. #include "popen.h"
  50. #include "utils.h"
  51. #define UNKNOWN_PACKET_LOSS 200 /* 200% */
  52. #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
  53. #define DEFAULT_MAX_PACKETS 5 /* default no. of ICMP ECHO packets */
  54. #define WARN_DUPLICATES "DUPLICATES FOUND! "
  55. int process_arguments (int, char **);
  56. int get_threshold (char *, float *, int *);
  57. int validate_arguments (void);
  58. int run_ping (char *, char *);
  59. void print_usage (void);
  60. void print_help (void);
  61. int display_html = FALSE;
  62. int wpl = UNKNOWN_PACKET_LOSS;
  63. int cpl = UNKNOWN_PACKET_LOSS;
  64. float wrta = UNKNOWN_TRIP_TIME;
  65. float crta = UNKNOWN_TRIP_TIME;
  66. char **addresses = NULL;
  67. int n_addresses;
  68. int max_addr = 1;
  69. int max_packets = -1;
  70. int verbose = FALSE;
  71. float rta = UNKNOWN_TRIP_TIME;
  72. int pl = UNKNOWN_PACKET_LOSS;
  73. char *warn_text = NULL;
  74. int
  75. main (int argc, char **argv)
  76. {
  77. char *command_line = NULL;
  78. int result = STATE_UNKNOWN;
  79. int this_result = STATE_UNKNOWN;
  80. int i;
  81. addresses = malloc (max_addr);
  82. if (process_arguments (argc, argv) == ERROR)
  83. usage ("Could not parse arguments");
  84. exit;
  85. /* Set signal handling and alarm */
  86. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  87. printf ("Cannot catch SIGALRM");
  88. return STATE_UNKNOWN;
  89. }
  90. /* handle timeouts gracefully */
  91. alarm (timeout_interval);
  92. for (i = 0 ; i < n_addresses ; i++) {
  93. /* does the host address of number of packets argument come first? */
  94. #ifdef PING6_COMMAND
  95. # ifdef PING_PACKETS_FIRST
  96. if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
  97. asprintf (&command_line, PING6_COMMAND, max_packets, addresses[i]);
  98. else
  99. asprintf (&command_line, PING_COMMAND, max_packets, addresses[i]);
  100. # else
  101. if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
  102. asprintf (&command_line, PING6_COMMAND, addresses[i], max_packets);
  103. else
  104. asprintf (&command_line, PING_COMMAND, addresses[i], max_packets);
  105. # endif
  106. #else /* USE_IPV6 */
  107. # ifdef PING_PACKETS_FIRST
  108. asprintf (&command_line, PING_COMMAND, max_packets, addresses[i]);
  109. # else
  110. asprintf (&command_line, PING_COMMAND, addresses[i], max_packets);
  111. # endif
  112. #endif /* USE_IPV6 */
  113. if (verbose)
  114. printf ("%s ==> ", command_line);
  115. /* run the command */
  116. this_result = run_ping (command_line, addresses[i]);
  117. if (pl == UNKNOWN_PACKET_LOSS || rta == UNKNOWN_TRIP_TIME) {
  118. printf ("%s\n", command_line);
  119. terminate (STATE_UNKNOWN,
  120. "Error: Could not interpret output from ping command\n");
  121. }
  122. if (pl >= cpl || rta >= crta || rta < 0)
  123. this_result = STATE_CRITICAL;
  124. else if (pl >= wpl || rta >= wrta)
  125. this_result = STATE_WARNING;
  126. else if (pl >= 0 && rta >= 0)
  127. this_result = max_state (STATE_OK, this_result);
  128. if (n_addresses > 1 && this_result != STATE_UNKNOWN)
  129. terminate (STATE_OK, "%s is alive\n", addresses[i]);
  130. if (display_html == TRUE)
  131. printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]);
  132. if (pl == 100)
  133. printf ("PING %s - %sPacket loss = %d%%", state_text (this_result), warn_text,
  134. pl);
  135. else
  136. printf ("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms",
  137. state_text (this_result), warn_text, pl, rta);
  138. if (display_html == TRUE)
  139. printf ("</A>");
  140. printf ("\n");
  141. if (verbose)
  142. printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
  143. result = max_state (result, this_result);
  144. }
  145. return result;
  146. }
  147. /* process command-line arguments */
  148. int
  149. process_arguments (int argc, char **argv)
  150. {
  151. int c = 1;
  152. char *ptr;
  153. int option_index = 0;
  154. static struct option long_options[] = {
  155. STD_LONG_OPTS,
  156. {"packets", required_argument, 0, 'p'},
  157. {"nohtml", no_argument, 0, 'n'},
  158. {"link", no_argument, 0, 'L'},
  159. {"use-ipv4", no_argument, 0, '4'},
  160. {"use-ipv6", no_argument, 0, '6'},
  161. {0, 0, 0, 0}
  162. };
  163. if (argc < 2)
  164. return ERROR;
  165. for (c = 1; c < argc; c++) {
  166. if (strcmp ("-to", argv[c]) == 0)
  167. strcpy (argv[c], "-t");
  168. if (strcmp ("-nohtml", argv[c]) == 0)
  169. strcpy (argv[c], "-n");
  170. }
  171. while (1) {
  172. c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", long_options, &option_index);
  173. if (c == -1 || c == EOF)
  174. break;
  175. switch (c) {
  176. case '?': /* usage */
  177. usage3 ("Unknown argument", optopt);
  178. case 'h': /* help */
  179. print_help ();
  180. exit (STATE_OK);
  181. case 'V': /* version */
  182. print_revision (progname, REVISION);
  183. exit (STATE_OK);
  184. case 't': /* timeout period */
  185. timeout_interval = atoi (optarg);
  186. break;
  187. case 'v': /* verbose mode */
  188. verbose = TRUE;
  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, max_addr);
  207. if (addresses == NULL)
  208. terminate (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. printf ("Invalid host name/address: %s\n\n", argv[c]);
  245. return ERROR;
  246. } else {
  247. addresses[0] = argv[c++];
  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 == UNKNOWN_TRIP_TIME) {
  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 == UNKNOWN_TRIP_TIME) {
  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. else
  312. usage2 ("%s: Warning threshold must be integer or percentage!\n\n", arg);
  313. }
  314. int
  315. validate_arguments ()
  316. {
  317. float max_seconds;
  318. int i;
  319. if (wrta == UNKNOWN_TRIP_TIME) {
  320. printf ("<wrta> was not set\n");
  321. return ERROR;
  322. }
  323. else if (crta == UNKNOWN_TRIP_TIME) {
  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 host name/address", addresses[i]);
  351. }
  352. return OK;
  353. }
  354. int
  355. run_ping (char *command_line, char *server_address)
  356. {
  357. char input_buffer[MAX_INPUT_BUFFER];
  358. int result = STATE_UNKNOWN;
  359. warn_text = malloc (1);
  360. if (warn_text == NULL)
  361. terminate (STATE_UNKNOWN, "unable to malloc warn_text");
  362. warn_text[0] = 0;
  363. if ((child_process = spopen (command_line)) == NULL) {
  364. printf ("Cannot open pipe: ");
  365. terminate (STATE_UNKNOWN, command_line);
  366. }
  367. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  368. if (child_stderr == NULL)
  369. printf ("Cannot open stderr for %s\n", command_line);
  370. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  371. if (strstr (input_buffer, "(DUP!)")) {
  372. /* cannot use the max function since STATE_UNKNOWN is max
  373. result = max (result, STATE_WARNING); */
  374. if( !(result == STATE_CRITICAL) ){
  375. result = STATE_WARNING;
  376. }
  377. warn_text = realloc (warn_text, strlen (WARN_DUPLICATES) + 1);
  378. if (warn_text == NULL)
  379. terminate (STATE_UNKNOWN, "unable to realloc warn_text");
  380. strcpy (warn_text, WARN_DUPLICATES);
  381. }
  382. /* get the percent loss statistics */
  383. if (sscanf
  384. (input_buffer, "%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",
  385. &pl) == 1
  386. || sscanf
  387. (input_buffer, "%*d packets transmitted, %*d packets received, %d%% packet loss",
  388. &pl) == 1
  389. || sscanf
  390. (input_buffer, "%*d packets transmitted, %*d packets received, %d%% loss, time", &pl) == 1
  391. || sscanf
  392. (input_buffer, "%*d packets transmitted, %*d received, %d%% loss, time", &pl) == 1
  393. /* Suse 8.0 as reported by Richard * Brodie */
  394. )
  395. continue;
  396. /* get the round trip average */
  397. else
  398. if (sscanf (input_buffer, "round-trip min/avg/max = %*f/%f/%*f", &rta)
  399. == 1
  400. || sscanf (input_buffer,
  401. "round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",
  402. &rta) == 1
  403. || sscanf (input_buffer,
  404. "round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",
  405. &rta) == 1
  406. || sscanf (input_buffer,
  407. "round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",
  408. &rta) == 1
  409. || sscanf (input_buffer,
  410. "round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",
  411. &rta) == 1
  412. || sscanf (input_buffer, "round-trip (ms) min/avg/max = %*f/%f/%*f",
  413. &rta) == 1
  414. || sscanf (input_buffer, "rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",
  415. &rta) == 1
  416. )
  417. continue;
  418. }
  419. /* this is needed because there is no rta if all packets are lost */
  420. if (pl == 100)
  421. rta = crta;
  422. /* check stderr */
  423. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  424. if (strstr
  425. (input_buffer,
  426. "Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP"))
  427. continue;
  428. if (strstr (input_buffer, "Network is unreachable"))
  429. terminate (STATE_CRITICAL, "PING CRITICAL - Network unreachable (%s)",
  430. server_address);
  431. else if (strstr (input_buffer, "Destination Host Unreachable"))
  432. terminate (STATE_CRITICAL, "PING CRITICAL - Host Unreachable (%s)",
  433. server_address);
  434. else if (strstr (input_buffer, "unknown host" ) )
  435. terminate (STATE_CRITICAL, "PING CRITICAL - Host not found (%s)",
  436. server_address);
  437. warn_text =
  438. realloc (warn_text, strlen (warn_text) + strlen (input_buffer) + 2);
  439. if (warn_text == NULL)
  440. terminate (STATE_UNKNOWN, "unable to realloc warn_text");
  441. if (strlen (warn_text) == 0)
  442. strcpy (warn_text, input_buffer);
  443. else
  444. sprintf (warn_text, "%s %s", warn_text, input_buffer);
  445. if (strstr (input_buffer, "DUPLICATES FOUND")) {
  446. if( !(result == STATE_CRITICAL) ){
  447. result = STATE_WARNING;
  448. }
  449. }
  450. else
  451. result = STATE_CRITICAL ;
  452. }
  453. (void) fclose (child_stderr);
  454. /* close the pipe - WARNING if status is set */
  455. if (spclose (child_process))
  456. result = max_state (result, STATE_WARNING);
  457. return result;
  458. }
  459. void
  460. print_usage (void)
  461. {
  462. printf ("Usage:\n" " %s %s\n"
  463. " %s (-h | --help) for detailed help\n"
  464. " %s (-V | --version) for version information\n",
  465. progname, OPTIONS, progname, progname);
  466. }
  467. void
  468. print_help (void)
  469. {
  470. print_revision (progname, REVISION);
  471. printf
  472. ("Copyright (c) %s %s <%s>\n\n%s\n",
  473. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  474. print_usage ();
  475. printf
  476. ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n",
  477. DEFAULT_MAX_PACKETS, DEFAULT_SOCKET_TIMEOUT);
  478. support ();
  479. }