check_ping.c 12 KB

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