check_ping.c 14 KB

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