check_ping.c 13 KB

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