check_ping.c 13 KB

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