check_ping.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 ((size_t)max_addr);
  55. if (process_arguments (argc, argv) == ERROR)
  56. usage (_("Could not parse arguments"));
  57. /* Set signal handling and alarm */
  58. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  59. printf (_("Cannot catch SIGALRM"));
  60. return STATE_UNKNOWN;
  61. }
  62. /* handle timeouts gracefully */
  63. alarm (timeout_interval);
  64. for (i = 0 ; i < n_addresses ; i++) {
  65. /* does the host address of number of packets argument come first? */
  66. #ifdef PING6_COMMAND
  67. # ifdef PING_PACKETS_FIRST
  68. if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
  69. asprintf (&cmd, PING6_COMMAND, max_packets, addresses[i]);
  70. else
  71. asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]);
  72. # else
  73. if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
  74. asprintf (&cmd, PING6_COMMAND, addresses[i], max_packets);
  75. else
  76. asprintf (&cmd, PING_COMMAND, addresses[i], max_packets);
  77. # endif
  78. #else /* USE_IPV6 */
  79. # ifdef PING_PACKETS_FIRST
  80. asprintf (&cmd, PING_COMMAND, max_packets, addresses[i]);
  81. # else
  82. asprintf (&cmd, PING_COMMAND, addresses[i], max_packets);
  83. # endif
  84. #endif /* USE_IPV6 */
  85. if (verbose)
  86. printf ("%s ==> ", cmd);
  87. /* run the command */
  88. this_result = run_ping (cmd, addresses[i]);
  89. if (pl == UNKNOWN_PACKET_LOSS || rta < 0.0) {
  90. printf ("%s\n", cmd);
  91. die (STATE_UNKNOWN,
  92. _("Error: Could not interpret output from ping command\n"));
  93. }
  94. if (pl >= cpl || rta >= crta || rta < 0)
  95. this_result = STATE_CRITICAL;
  96. else if (pl >= wpl || rta >= wrta)
  97. this_result = STATE_WARNING;
  98. else if (pl >= 0 && rta >= 0)
  99. this_result = max_state (STATE_OK, this_result);
  100. if (n_addresses > 1 && this_result != STATE_UNKNOWN)
  101. die (STATE_OK, "%s is alive\n", addresses[i]);
  102. if (display_html == TRUE)
  103. printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]);
  104. if (pl == 100)
  105. printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text,
  106. pl);
  107. else
  108. printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"),
  109. state_text (this_result), warn_text, pl, rta);
  110. if (display_html == TRUE)
  111. printf ("</A>");
  112. printf ("\n");
  113. if (verbose)
  114. printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
  115. result = max_state (result, this_result);
  116. }
  117. return result;
  118. }
  119. /* process command-line arguments */
  120. int
  121. process_arguments (int argc, char **argv)
  122. {
  123. int c = 1;
  124. char *ptr;
  125. int option = 0;
  126. static struct option longopts[] = {
  127. STD_LONG_OPTS,
  128. {"packets", required_argument, 0, 'p'},
  129. {"nohtml", no_argument, 0, 'n'},
  130. {"link", no_argument, 0, 'L'},
  131. {"use-ipv4", no_argument, 0, '4'},
  132. {"use-ipv6", no_argument, 0, '6'},
  133. {0, 0, 0, 0}
  134. };
  135. if (argc < 2)
  136. return ERROR;
  137. for (c = 1; c < argc; c++) {
  138. if (strcmp ("-to", argv[c]) == 0)
  139. strcpy (argv[c], "-t");
  140. if (strcmp ("-nohtml", argv[c]) == 0)
  141. strcpy (argv[c], "-n");
  142. }
  143. while (1) {
  144. c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
  145. if (c == -1 || c == EOF)
  146. break;
  147. switch (c) {
  148. case '?': /* usage */
  149. usage3 (_("Unknown argument"), optopt);
  150. break;
  151. case 'h': /* help */
  152. print_help ();
  153. exit (STATE_OK);
  154. break;
  155. case 'V': /* version */
  156. print_revision (progname, revision);
  157. exit (STATE_OK);
  158. break;
  159. case 't': /* timeout period */
  160. timeout_interval = atoi (optarg);
  161. break;
  162. case 'v': /* verbose mode */
  163. verbose = TRUE;
  164. break;
  165. case '4': /* IPv4 only */
  166. address_family = AF_INET;
  167. break;
  168. case '6': /* IPv6 only */
  169. #ifdef USE_IPV6
  170. address_family = AF_INET6;
  171. #else
  172. usage (_("IPv6 support not available\n"));
  173. #endif
  174. break;
  175. case 'H': /* hostname */
  176. ptr=optarg;
  177. while (1) {
  178. n_addresses++;
  179. if (n_addresses > max_addr) {
  180. max_addr *= 2;
  181. addresses = realloc (addresses, (size_t)max_addr);
  182. if (addresses == NULL)
  183. die (STATE_UNKNOWN, _("Could not realloc() addresses\n"));
  184. }
  185. addresses[n_addresses-1] = ptr;
  186. if ((ptr = index (ptr, ','))) {
  187. strcpy (ptr, "");
  188. ptr += sizeof(char);
  189. } else {
  190. break;
  191. }
  192. }
  193. break;
  194. case 'p': /* number of packets to send */
  195. if (is_intnonneg (optarg))
  196. max_packets = atoi (optarg);
  197. else
  198. usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg);
  199. break;
  200. case 'n': /* no HTML */
  201. display_html = FALSE;
  202. break;
  203. case 'L': /* show HTML */
  204. display_html = TRUE;
  205. break;
  206. case 'c':
  207. get_threshold (optarg, &crta, &cpl);
  208. break;
  209. case 'w':
  210. get_threshold (optarg, &wrta, &wpl);
  211. break;
  212. }
  213. }
  214. c = optind;
  215. if (c == argc)
  216. return validate_arguments ();
  217. if (addresses[0] == NULL) {
  218. if (is_host (argv[c]) == FALSE) {
  219. printf (_("Invalid host name/address: %s\n\n"), argv[c]);
  220. return ERROR;
  221. } else {
  222. addresses[0] = argv[c++];
  223. if (c == argc)
  224. return validate_arguments ();
  225. }
  226. }
  227. if (wpl == UNKNOWN_PACKET_LOSS) {
  228. if (is_intpercent (argv[c]) == FALSE) {
  229. printf (_("<wpl> (%s) must be an integer percentage\n"), argv[c]);
  230. return ERROR;
  231. } else {
  232. wpl = atoi (argv[c++]);
  233. if (c == argc)
  234. return validate_arguments ();
  235. }
  236. }
  237. if (cpl == UNKNOWN_PACKET_LOSS) {
  238. if (is_intpercent (argv[c]) == FALSE) {
  239. printf (_("<cpl> (%s) must be an integer percentage\n"), argv[c]);
  240. return ERROR;
  241. } else {
  242. cpl = atoi (argv[c++]);
  243. if (c == argc)
  244. return validate_arguments ();
  245. }
  246. }
  247. if (wrta < 0.0) {
  248. if (is_negative (argv[c])) {
  249. printf (_("<wrta> (%s) must be a non-negative number\n"), argv[c]);
  250. return ERROR;
  251. } else {
  252. wrta = atof (argv[c++]);
  253. if (c == argc)
  254. return validate_arguments ();
  255. }
  256. }
  257. if (crta < 0.0) {
  258. if (is_negative (argv[c])) {
  259. printf (_("<crta> (%s) must be a non-negative number\n"), argv[c]);
  260. return ERROR;
  261. } else {
  262. crta = atof (argv[c++]);
  263. if (c == argc)
  264. return validate_arguments ();
  265. }
  266. }
  267. if (max_packets == -1) {
  268. if (is_intnonneg (argv[c])) {
  269. max_packets = atoi (argv[c++]);
  270. } else {
  271. printf (_("<max_packets> (%s) must be a non-negative number\n"), argv[c]);
  272. return ERROR;
  273. }
  274. }
  275. return validate_arguments ();
  276. }
  277. int
  278. get_threshold (char *arg, float *trta, int *tpl)
  279. {
  280. if (is_intnonneg (arg) && sscanf (arg, "%f", trta) == 1)
  281. return OK;
  282. else if (strpbrk (arg, ",:") && strstr (arg, "%") && sscanf (arg, "%f%*[:,]%d%%", trta, tpl) == 2)
  283. return OK;
  284. else if (strstr (arg, "%") && sscanf (arg, "%d%%", tpl) == 1)
  285. return OK;
  286. usage2 (_("%s: Warning threshold must be integer or percentage!\n\n"), arg);
  287. return STATE_UNKNOWN;
  288. }
  289. int
  290. validate_arguments ()
  291. {
  292. float max_seconds;
  293. int i;
  294. if (wrta < 0.0) {
  295. printf (_("<wrta> was not set\n"));
  296. return ERROR;
  297. }
  298. else if (crta < 0.0) {
  299. printf (_("<crta> was not set\n"));
  300. return ERROR;
  301. }
  302. else if (wpl == UNKNOWN_PACKET_LOSS) {
  303. printf (_("<wpl> was not set\n"));
  304. return ERROR;
  305. }
  306. else if (cpl == UNKNOWN_PACKET_LOSS) {
  307. printf (_("<cpl> was not set\n"));
  308. return ERROR;
  309. }
  310. else if (wrta > crta) {
  311. printf (_("<wrta> (%f) cannot be larger than <crta> (%f)\n"), wrta, crta);
  312. return ERROR;
  313. }
  314. else if (wpl > cpl) {
  315. printf (_("<wpl> (%d) cannot be larger than <cpl> (%d)\n"), wpl, cpl);
  316. return ERROR;
  317. }
  318. if (max_packets == -1)
  319. max_packets = DEFAULT_MAX_PACKETS;
  320. max_seconds = crta / 1000.0 * max_packets + max_packets;
  321. if (max_seconds > timeout_interval)
  322. timeout_interval = (int)max_seconds;
  323. for (i=0; i<n_addresses; i++) {
  324. if (is_host(addresses[i]) == FALSE)
  325. usage2 (_("Invalid host name/address"), addresses[i]);
  326. }
  327. return OK;
  328. }
  329. int
  330. run_ping (char *cmd, char *server_address)
  331. {
  332. char buf[MAX_INPUT_BUFFER];
  333. int result = STATE_UNKNOWN;
  334. if ((child_process = spopen (cmd)) == NULL)
  335. die (STATE_UNKNOWN, _("Cannot open pipe: %s"), cmd);
  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 (warn_text == NULL)
  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. if (warn_text == NULL)
  396. warn_text = strdup("");
  397. return result;
  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. }