check_ping.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 (char *, char *);
  32. void print_usage (void);
  33. void print_help (void);
  34. int display_html = FALSE;
  35. int wpl = UNKNOWN_PACKET_LOSS;
  36. int cpl = UNKNOWN_PACKET_LOSS;
  37. float wrta = UNKNOWN_TRIP_TIME;
  38. float crta = UNKNOWN_TRIP_TIME;
  39. char **addresses = NULL;
  40. int n_addresses;
  41. int max_addr = 1;
  42. int max_packets = -1;
  43. int verbose = FALSE;
  44. float rta = UNKNOWN_TRIP_TIME;
  45. int pl = UNKNOWN_PACKET_LOSS;
  46. char *warn_text = "";
  47. int
  48. main (int argc, char **argv)
  49. {
  50. char *cmd = NULL;
  51. int result = STATE_UNKNOWN;
  52. int this_result = STATE_UNKNOWN;
  53. int i;
  54. addresses = malloc (max_addr);
  55. if (process_arguments (argc, argv) == ERROR)
  56. usage (_("Could not parse arguments"));
  57. exit;
  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 == UNKNOWN_TRIP_TIME) {
  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_index = 0;
  127. static struct option long_options[] = {
  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:", long_options, &option_index);
  146. if (c == -1 || c == EOF)
  147. break;
  148. switch (c) {
  149. case '?': /* usage */
  150. usage3 (_("Unknown argument"), optopt);
  151. case 'h': /* help */
  152. print_help ();
  153. exit (STATE_OK);
  154. case 'V': /* version */
  155. print_revision (progname, revision);
  156. exit (STATE_OK);
  157. case 't': /* timeout period */
  158. timeout_interval = atoi (optarg);
  159. break;
  160. case 'v': /* verbose mode */
  161. verbose = TRUE;
  162. break;
  163. case '4': /* IPv4 only */
  164. address_family = AF_INET;
  165. break;
  166. case '6': /* IPv6 only */
  167. #ifdef USE_IPV6
  168. address_family = AF_INET6;
  169. #else
  170. usage (_("IPv6 support not available\n"));
  171. #endif
  172. break;
  173. case 'H': /* hostname */
  174. ptr=optarg;
  175. while (1) {
  176. n_addresses++;
  177. if (n_addresses > max_addr) {
  178. max_addr *= 2;
  179. addresses = realloc (addresses, max_addr);
  180. if (addresses == NULL)
  181. die (STATE_UNKNOWN, _("Could not realloc() addresses\n"));
  182. }
  183. addresses[n_addresses-1] = ptr;
  184. if ((ptr = index (ptr, ','))) {
  185. strcpy (ptr, "");
  186. ptr += sizeof(char);
  187. } else {
  188. break;
  189. }
  190. }
  191. break;
  192. case 'p': /* number of packets to send */
  193. if (is_intnonneg (optarg))
  194. max_packets = atoi (optarg);
  195. else
  196. usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg);
  197. break;
  198. case 'n': /* no HTML */
  199. display_html = FALSE;
  200. break;
  201. case 'L': /* show HTML */
  202. display_html = TRUE;
  203. break;
  204. case 'c':
  205. get_threshold (optarg, &crta, &cpl);
  206. break;
  207. case 'w':
  208. get_threshold (optarg, &wrta, &wpl);
  209. break;
  210. }
  211. }
  212. c = optind;
  213. if (c == argc)
  214. return validate_arguments ();
  215. if (addresses[0] == NULL) {
  216. if (is_host (argv[c]) == FALSE) {
  217. printf (_("Invalid host name/address: %s\n\n"), argv[c]);
  218. return ERROR;
  219. } else {
  220. addresses[0] = argv[c++];
  221. if (c == argc)
  222. return validate_arguments ();
  223. }
  224. }
  225. if (wpl == UNKNOWN_PACKET_LOSS) {
  226. if (is_intpercent (argv[c]) == FALSE) {
  227. printf (_("<wpl> (%s) must be an integer percentage\n"), argv[c]);
  228. return ERROR;
  229. } else {
  230. wpl = atoi (argv[c++]);
  231. if (c == argc)
  232. return validate_arguments ();
  233. }
  234. }
  235. if (cpl == UNKNOWN_PACKET_LOSS) {
  236. if (is_intpercent (argv[c]) == FALSE) {
  237. printf (_("<cpl> (%s) must be an integer percentage\n"), argv[c]);
  238. return ERROR;
  239. } else {
  240. cpl = atoi (argv[c++]);
  241. if (c == argc)
  242. return validate_arguments ();
  243. }
  244. }
  245. if (wrta == UNKNOWN_TRIP_TIME) {
  246. if (is_negative (argv[c])) {
  247. printf (_("<wrta> (%s) must be a non-negative number\n"), argv[c]);
  248. return ERROR;
  249. } else {
  250. wrta = atof (argv[c++]);
  251. if (c == argc)
  252. return validate_arguments ();
  253. }
  254. }
  255. if (crta == UNKNOWN_TRIP_TIME) {
  256. if (is_negative (argv[c])) {
  257. printf (_("<crta> (%s) must be a non-negative number\n"), argv[c]);
  258. return ERROR;
  259. } else {
  260. crta = atof (argv[c++]);
  261. if (c == argc)
  262. return validate_arguments ();
  263. }
  264. }
  265. if (max_packets == -1) {
  266. if (is_intnonneg (argv[c])) {
  267. max_packets = atoi (argv[c++]);
  268. } else {
  269. printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]);
  270. return ERROR;
  271. }
  272. }
  273. return validate_arguments ();
  274. }
  275. int
  276. get_threshold (char *arg, float *trta, int *tpl)
  277. {
  278. if (is_intnonneg (arg) && sscanf (arg, "%f", trta) == 1)
  279. return OK;
  280. else if (strpbrk (arg, ",:") && strstr (arg, "%") && sscanf (arg, "%f%*[:,]%d%%", trta, tpl) == 2)
  281. return OK;
  282. else if (strstr (arg, "%") && sscanf (arg, "%d%%", tpl) == 1)
  283. return OK;
  284. usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
  285. return STATE_UNKNOWN;
  286. }
  287. int
  288. validate_arguments ()
  289. {
  290. float max_seconds;
  291. int i;
  292. if (wrta == UNKNOWN_TRIP_TIME) {
  293. printf (_("<wrta> was not set\n"));
  294. return ERROR;
  295. }
  296. else if (crta == UNKNOWN_TRIP_TIME) {
  297. printf (_("<crta> was not set\n"));
  298. return ERROR;
  299. }
  300. else if (wpl == UNKNOWN_PACKET_LOSS) {
  301. printf (_("<wpl> was not set\n"));
  302. return ERROR;
  303. }
  304. else if (cpl == UNKNOWN_PACKET_LOSS) {
  305. printf (_("<cpl> was not set\n"));
  306. return ERROR;
  307. }
  308. else if (wrta > crta) {
  309. printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta, crta);
  310. return ERROR;
  311. }
  312. else if (wpl > cpl) {
  313. printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl, cpl);
  314. return ERROR;
  315. }
  316. if (max_packets == -1)
  317. max_packets = DEFAULT_MAX_PACKETS;
  318. max_seconds = crta / 1000.0 * max_packets + max_packets;
  319. if (max_seconds > timeout_interval)
  320. timeout_interval = (int)max_seconds;
  321. for (i=0; i<n_addresses; i++) {
  322. if (is_host(addresses[i]) == FALSE)
  323. usage2 (_("Invalid host name/address"), addresses[i]);
  324. }
  325. return OK;
  326. }
  327. int
  328. run_ping (char *cmd, char *server_address)
  329. {
  330. char buf[MAX_INPUT_BUFFER];
  331. int result = STATE_UNKNOWN;
  332. if ((child_process = spopen (cmd)) == NULL) {
  333. printf (_("Cannot open pipe: "));
  334. die (STATE_UNKNOWN, cmd);
  335. }
  336. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  337. if (child_stderr == NULL)
  338. printf (_("Cannot open stderr for %s\n"), cmd);
  339. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
  340. if (strstr (buf, _("(DUP!)"))) {
  341. result = max_state (result, STATE_WARNING);
  342. warn_text = strdup (WARN_DUPLICATES);
  343. if (!warn_text)
  344. die (STATE_UNKNOWN, _("unable to realloc warn_text"));
  345. }
  346. /* get the percent loss statistics */
  347. if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 ||
  348. sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 ||
  349. sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 ||
  350. sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &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 */
  367. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) {
  368. if (strstr(buf,"Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP"))
  369. continue;
  370. if (strstr (buf, "Network is unreachable"))
  371. die (STATE_CRITICAL,
  372. _("PING CRITICAL - Network unreachable (%s)"),
  373. server_address);
  374. else if (strstr (buf, "Destination Host Unreachable"))
  375. die (STATE_CRITICAL,
  376. _("PING CRITICAL - Host Unreachable (%s)"),
  377. server_address);
  378. else if (strstr (buf, "unknown host" ))
  379. die (STATE_CRITICAL,
  380. _("PING CRITICAL - Host not found (%s)"),
  381. server_address);
  382. if (strlen (warn_text) == 0)
  383. warn_text = strdup (buf);
  384. else if (asprintf (&warn_text, "%s %s", warn_text, buf) == -1)
  385. die (STATE_UNKNOWN, _("unable to realloc warn_text"));
  386. if (strstr (buf, "DUPLICATES FOUND"))
  387. result = max_state (result, STATE_WARNING);
  388. else
  389. result = STATE_CRITICAL ;
  390. }
  391. (void) fclose (child_stderr);
  392. /* close the pipe - WARNING if status is set */
  393. if (spclose (child_process))
  394. result = max_state (result, STATE_WARNING);
  395. return result;
  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. }