check_ping.c 14 KB

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