check_ping.c 14 KB

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