check_ping.c 14 KB

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