check_ping.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /******************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. ******************************************************************************/
  14. const char *progname = "check_ping";
  15. const char *revision = "$Revision$";
  16. const char *copyright = "2000-2003";
  17. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  18. #include "common.h"
  19. #include "netutils.h"
  20. #include "popen.h"
  21. #include "utils.h"
  22. #define WARN_DUPLICATES "DUPLICATES FOUND! "
  23. #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
  24. enum {
  25. UNKNOWN_PACKET_LOSS = 200, /* 200% */
  26. DEFAULT_MAX_PACKETS = 5 /* default no. of ICMP ECHO packets */
  27. };
  28. int process_arguments (int, char **);
  29. int get_threshold (char *, float *, int *);
  30. int validate_arguments (void);
  31. int run_ping (const char *cmd, const char *addr);
  32. int error_scan (char buf[MAX_INPUT_BUFFER], const char *addr);
  33. void print_usage (void);
  34. void print_help (void);
  35. int display_html = FALSE;
  36. int wpl = UNKNOWN_PACKET_LOSS;
  37. int cpl = UNKNOWN_PACKET_LOSS;
  38. float wrta = UNKNOWN_TRIP_TIME;
  39. float crta = UNKNOWN_TRIP_TIME;
  40. char **addresses = NULL;
  41. int n_addresses;
  42. int max_addr = 1;
  43. int max_packets = -1;
  44. int verbose = FALSE;
  45. float rta = UNKNOWN_TRIP_TIME;
  46. int pl = UNKNOWN_PACKET_LOSS;
  47. char *warn_text;
  48. int
  49. main (int argc, char **argv)
  50. {
  51. char *cmd = NULL;
  52. int result = STATE_UNKNOWN;
  53. int this_result = STATE_UNKNOWN;
  54. int i;
  55. addresses = malloc ((size_t)max_addr);
  56. if (process_arguments (argc, argv) == ERROR)
  57. usage (_("Could not parse arguments"));
  58. /* Set signal handling and alarm */
  59. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  60. printf (_("Cannot catch SIGALRM"));
  61. return STATE_UNKNOWN;
  62. }
  63. /* handle timeouts gracefully */
  64. alarm (timeout_interval);
  65. for (i = 0 ; i < n_addresses ; i++) {
  66. /* does the host address of number of packets argument come first? */
  67. #ifdef PING6_COMMAND
  68. # ifdef PING_PACKETS_FIRST
  69. if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
  70. asprintf (&cmd, PING6_COMMAND, max_packets, addresses[i]);
  71. else
  72. asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]);
  73. # else
  74. if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
  75. asprintf (&cmd, PING6_COMMAND, addresses[i], max_packets);
  76. else
  77. asprintf (&cmd, PING_COMMAND, addresses[i], max_packets);
  78. # endif
  79. #else /* USE_IPV6 */
  80. # ifdef PING_PACKETS_FIRST
  81. asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]);
  82. # else
  83. asprintf (&cmd, PING_COMMAND, addresses[i], max_packets);
  84. # endif
  85. #endif /* USE_IPV6 */
  86. if (verbose)
  87. printf ("%s ==> ", cmd);
  88. /* run the command */
  89. this_result = run_ping (cmd, addresses[i]);
  90. if (pl == UNKNOWN_PACKET_LOSS || rta < 0.0) {
  91. printf ("%s\n", cmd);
  92. die (STATE_UNKNOWN,
  93. _("Error: Could not interpret output from ping command\n"));
  94. }
  95. if (pl >= cpl || rta >= crta || rta < 0)
  96. this_result = STATE_CRITICAL;
  97. else if (pl >= wpl || rta >= wrta)
  98. this_result = STATE_WARNING;
  99. else if (pl >= 0 && rta >= 0)
  100. this_result = max_state (STATE_OK, this_result);
  101. if (n_addresses > 1 && this_result != STATE_UNKNOWN)
  102. die (STATE_OK, "%s is alive\n", addresses[i]);
  103. if (display_html == TRUE)
  104. printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]);
  105. if (pl == 100)
  106. printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text,
  107. pl);
  108. else
  109. printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"),
  110. state_text (this_result), warn_text, pl, rta);
  111. if (display_html == TRUE)
  112. printf ("</A>");
  113. printf ("\n");
  114. if (verbose)
  115. printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
  116. result = max_state (result, this_result);
  117. }
  118. return result;
  119. }
  120. /* process command-line arguments */
  121. int
  122. process_arguments (int argc, char **argv)
  123. {
  124. int c = 1;
  125. char *ptr;
  126. int option = 0;
  127. static struct option longopts[] = {
  128. STD_LONG_OPTS,
  129. {"packets", required_argument, 0, 'p'},
  130. {"nohtml", no_argument, 0, 'n'},
  131. {"link", no_argument, 0, 'L'},
  132. {"use-ipv4", no_argument, 0, '4'},
  133. {"use-ipv6", no_argument, 0, '6'},
  134. {0, 0, 0, 0}
  135. };
  136. if (argc < 2)
  137. return ERROR;
  138. for (c = 1; c < argc; c++) {
  139. if (strcmp ("-to", argv[c]) == 0)
  140. strcpy (argv[c], "-t");
  141. if (strcmp ("-nohtml", argv[c]) == 0)
  142. strcpy (argv[c], "-n");
  143. }
  144. while (1) {
  145. c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
  146. if (c == -1 || c == EOF)
  147. break;
  148. switch (c) {
  149. case '?': /* usage */
  150. usage3 (_("Unknown argument"), optopt);
  151. break;
  152. case 'h': /* help */
  153. print_help ();
  154. exit (STATE_OK);
  155. break;
  156. case 'V': /* version */
  157. print_revision (progname, revision);
  158. exit (STATE_OK);
  159. break;
  160. case 't': /* timeout period */
  161. timeout_interval = atoi (optarg);
  162. break;
  163. case 'v': /* verbose mode */
  164. verbose = TRUE;
  165. break;
  166. case '4': /* IPv4 only */
  167. address_family = AF_INET;
  168. break;
  169. case '6': /* IPv6 only */
  170. #ifdef USE_IPV6
  171. address_family = AF_INET6;
  172. #else
  173. usage (_("IPv6 support not available\n"));
  174. #endif
  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, (size_t)max_addr);
  183. if (addresses == NULL)
  184. die (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 < 0.0) {
  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 < 0.0) {
  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. usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
  288. return STATE_UNKNOWN;
  289. }
  290. int
  291. validate_arguments ()
  292. {
  293. float max_seconds;
  294. int i;
  295. if (wrta < 0.0) {
  296. printf (_("<wrta> was not set\n"));
  297. return ERROR;
  298. }
  299. else if (crta < 0.0) {
  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 (const char *cmd, const char *addr)
  332. {
  333. char buf[MAX_INPUT_BUFFER];
  334. int result = STATE_UNKNOWN;
  335. if ((child_process = spopen (cmd)) == NULL)
  336. die (STATE_UNKNOWN, _("Cannot open pipe: %s"), cmd);
  337. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  338. if (child_stderr == NULL)
  339. printf (_("Cannot open stderr for %s\n"), cmd);
  340. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
  341. result = max_state (result, error_scan (buf, addr));
  342. /* get the percent loss statistics */
  343. if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 ||
  344. sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 ||
  345. sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 ||
  346. sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1)
  347. continue;
  348. /* get the round trip average */
  349. else
  350. if(sscanf(buf,"round-trip min/avg/max = %*f/%f/%*f",&rta)==1 ||
  351. sscanf(buf,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta)==1 ||
  352. sscanf(buf,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta)==1 ||
  353. sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
  354. sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta)==1 ||
  355. sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta)==1 ||
  356. sscanf(buf,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta)==1)
  357. continue;
  358. }
  359. /* this is needed because there is no rta if all packets are lost */
  360. if (pl == 100)
  361. rta = crta;
  362. /* check stderr, setting at least WARNING if there is output here */
  363. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
  364. if (! strstr(buf,"Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP"))
  365. result = max_state (STATE_WARNING, error_scan (buf, addr));
  366. (void) fclose (child_stderr);
  367. /* close the pipe - WARNING if status is set */
  368. if (spclose (child_process))
  369. result = max_state (result, STATE_WARNING);
  370. if (warn_text == NULL)
  371. warn_text = strdup("");
  372. return result;
  373. }
  374. int
  375. error_scan (char buf[MAX_INPUT_BUFFER], const char *addr)
  376. {
  377. if (strstr (buf, "Network is unreachable"))
  378. die (STATE_CRITICAL, _("PING CRITICAL - Network unreachable (%s)"), addr);
  379. else if (strstr (buf, "Destination Host Unreachable"))
  380. die (STATE_CRITICAL, _("PING CRITICAL - Host Unreachable (%s)"), addr);
  381. else if (strstr (buf, "unknown host" ))
  382. die (STATE_CRITICAL, _("PING CRITICAL - Host not found (%s)"), addr);
  383. if (strstr (buf, "(DUP!)") || strstr (buf, "DUPLICATES FOUND")) {
  384. if (warn_text == NULL)
  385. warn_text = strdup (_(WARN_DUPLICATES));
  386. else if (! strstr (warn_text, _(WARN_DUPLICATES)) &&
  387. asprintf (&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1)
  388. die (STATE_UNKNOWN, _("unable to realloc warn_text"));
  389. return (STATE_WARNING);
  390. }
  391. return (STATE_OK);
  392. }
  393. void
  394. print_usage (void)
  395. {
  396. printf (\
  397. "Usage: %s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n\
  398. [-p packets] [-t timeout] [-L] [-4|-6]\n", progname);
  399. printf (_(UT_HLP_VRS), progname, progname);
  400. }
  401. void
  402. print_help (void)
  403. {
  404. print_revision (progname, revision);
  405. printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>"));
  406. printf (_(COPYRIGHT), copyright, email);
  407. printf (_("Use ping to check connection statistics for a remote host.\n\n"));
  408. print_usage ();
  409. printf (_(UT_HELP_VRSN));
  410. printf (_(UT_IPv46));
  411. printf (_("\
  412. -H, --hostname=HOST\n\
  413. host to ping\n\
  414. -w, --warning=THRESHOLD\n\
  415. warning threshold pair\n\
  416. -c, --critical=THRESHOLD\n\
  417. critical threshold pair\n\
  418. -p, --packets=INTEGER\n\
  419. number of ICMP ECHO packets to send (Default: %d)\n\
  420. -L, --link\n\
  421. show HTML in the plugin output (obsoleted by urlize)\n"),
  422. DEFAULT_MAX_PACKETS);
  423. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  424. printf (_("\
  425. THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
  426. time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
  427. percentage of packet loss to trigger an alarm state.\n\n"));
  428. printf (_("\
  429. This plugin uses the ping command to probe the specified host for packet loss\n\
  430. (percentage) and round trip average (milliseconds). It can produce HTML output\n\
  431. linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in\n\
  432. the contrib area of the downloads section at http://www.nagios.org\n\n"));
  433. printf (_(UT_SUPPORT));
  434. }