check_ping.c 14 KB

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