check_ping.c 14 KB

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