check_ping.c 13 KB

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