check_ping.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. $Id$
  14. ******************************************************************************/
  15. const char *progname = "check_ping";
  16. const char *revision = "$Revision$";
  17. const char *copyright = "2000-2006";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #include "netutils.h"
  21. #include "popen.h"
  22. #include "utils.h"
  23. #define WARN_DUPLICATES "DUPLICATES FOUND! "
  24. #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
  25. enum {
  26. UNKNOWN_PACKET_LOSS = 200, /* 200% */
  27. DEFAULT_MAX_PACKETS = 5 /* default no. of ICMP ECHO packets */
  28. };
  29. int process_arguments (int, char **);
  30. int get_threshold (char *, float *, int *);
  31. int validate_arguments (void);
  32. int run_ping (const char *cmd, const char *addr);
  33. int error_scan (char buf[MAX_INPUT_BUFFER], const char *addr);
  34. void print_usage (void);
  35. void print_help (void);
  36. int display_html = FALSE;
  37. int wpl = UNKNOWN_PACKET_LOSS;
  38. int cpl = UNKNOWN_PACKET_LOSS;
  39. float wrta = UNKNOWN_TRIP_TIME;
  40. float crta = UNKNOWN_TRIP_TIME;
  41. char **addresses = NULL;
  42. int n_addresses = 0;
  43. int max_addr = 1;
  44. int max_packets = -1;
  45. int verbose = 0;
  46. float rta = UNKNOWN_TRIP_TIME;
  47. int pl = UNKNOWN_PACKET_LOSS;
  48. char *warn_text;
  49. int
  50. main (int argc, char **argv)
  51. {
  52. char *cmd = NULL;
  53. char *rawcmd = NULL;
  54. int result = STATE_UNKNOWN;
  55. int this_result = STATE_UNKNOWN;
  56. int i;
  57. setlocale (LC_ALL, "");
  58. setlocale (LC_NUMERIC, "C");
  59. bindtextdomain (PACKAGE, LOCALEDIR);
  60. textdomain (PACKAGE);
  61. addresses = malloc (sizeof(char*) * max_addr);
  62. addresses[0] = NULL;
  63. if (process_arguments (argc, argv) == ERROR)
  64. usage4 (_("Could not parse arguments"));
  65. /* Set signal handling and alarm */
  66. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  67. usage4 (_("Cannot catch SIGALRM"));
  68. }
  69. /* handle timeouts gracefully */
  70. alarm (timeout_interval);
  71. for (i = 0 ; i < n_addresses ; i++) {
  72. #ifdef PING6_COMMAND
  73. if (is_inet6_addr(addresses[i]) && address_family != AF_INET)
  74. rawcmd = strdup(PING6_COMMAND);
  75. else
  76. rawcmd = strdup(PING_COMMAND);
  77. #else
  78. rawcmd = strdup(PING_COMMAND);
  79. #endif
  80. /* does the host address of number of packets argument come first? */
  81. #ifdef PING_PACKETS_FIRST
  82. # ifdef PING_HAS_TIMEOUT
  83. asprintf (&cmd, rawcmd, timeout_interval, max_packets, addresses[i]);
  84. # else
  85. asprintf (&cmd, rawcmd, max_packets, addresses[i]);
  86. # endif
  87. #else
  88. asprintf (&cmd, rawcmd, addresses[i], max_packets);
  89. #endif
  90. if (verbose >= 2)
  91. printf ("CMD: %s\n", 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. _("CRITICAL - 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 >= 2)
  119. printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl);
  120. result = max_state (result, this_result);
  121. free (rawcmd);
  122. free (cmd);
  123. }
  124. return result;
  125. }
  126. /* process command-line arguments */
  127. int
  128. process_arguments (int argc, char **argv)
  129. {
  130. int c = 1;
  131. char *ptr;
  132. int option = 0;
  133. static struct option longopts[] = {
  134. STD_LONG_OPTS,
  135. {"packets", required_argument, 0, 'p'},
  136. {"nohtml", no_argument, 0, 'n'},
  137. {"link", no_argument, 0, 'L'},
  138. {"use-ipv4", no_argument, 0, '4'},
  139. {"use-ipv6", no_argument, 0, '6'},
  140. {0, 0, 0, 0}
  141. };
  142. if (argc < 2)
  143. return ERROR;
  144. for (c = 1; c < argc; c++) {
  145. if (strcmp ("-to", argv[c]) == 0)
  146. strcpy (argv[c], "-t");
  147. if (strcmp ("-nohtml", argv[c]) == 0)
  148. strcpy (argv[c], "-n");
  149. }
  150. while (1) {
  151. c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
  152. if (c == -1 || c == EOF)
  153. break;
  154. switch (c) {
  155. case '?': /* usage */
  156. usage2 (_("Unknown argument"), optarg);
  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++;
  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. usage2 (_("Invalid hostname/address"), argv[c]);
  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 hostname/address"), addresses[i]);
  332. }
  333. if (n_addresses == 0) {
  334. usage (_("You must specify a server address or host name"));
  335. }
  336. return OK;
  337. }
  338. int
  339. run_ping (const char *cmd, const char *addr)
  340. {
  341. char buf[MAX_INPUT_BUFFER];
  342. int result = STATE_UNKNOWN;
  343. if ((child_process = spopen (cmd)) == NULL)
  344. die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd);
  345. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  346. if (child_stderr == NULL)
  347. printf (_("Cannot open stderr for %s\n"), cmd);
  348. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
  349. if (verbose >= 3)
  350. printf("Output: %s", buf);
  351. result = max_state (result, error_scan (buf, addr));
  352. /* get the percent loss statistics */
  353. if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 ||
  354. sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss", &pl) == 1 ||
  355. sscanf(buf,"%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss", &pl) == 1 ||
  356. sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 ||
  357. sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 ||
  358. sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1 ||
  359. sscanf(buf,"%*d packets transmitted, %*d received, %d%% packet loss, time", &pl)==1 ||
  360. sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl) == 1 ||
  361. sscanf(buf,"%*d packets transmitted %*d received, +%*d errors, %d%% packet loss", &pl) == 1
  362. )
  363. continue;
  364. /* get the round trip average */
  365. else
  366. if(sscanf(buf,"round-trip min/avg/max = %*f/%f/%*f",&rta)==1 ||
  367. sscanf(buf,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta)==1 ||
  368. sscanf(buf,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta)==1 ||
  369. sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
  370. sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta)==1 ||
  371. sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta)==1 ||
  372. sscanf(buf,"round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
  373. sscanf(buf,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta)==1)
  374. continue;
  375. }
  376. /* this is needed because there is no rta if all packets are lost */
  377. if (pl == 100)
  378. rta = crta;
  379. /* check stderr, setting at least WARNING if there is output here */
  380. /* Add warning into warn_text */
  381. while (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr)) {
  382. if (! strstr(buf,"WARNING - no SO_TIMESTAMP support, falling back to SIOCGSTAMP")) {
  383. if (verbose >= 3) {
  384. printf("Got stderr: %s", buf);
  385. }
  386. if ((result=error_scan(buf, addr)) == STATE_OK) {
  387. result = STATE_WARNING;
  388. if (warn_text == NULL) {
  389. warn_text = strdup(_("System call sent warnings to stderr "));
  390. } else {
  391. asprintf(&warn_text, "%s %s", warn_text, _("System call sent warnings to stderr "));
  392. }
  393. }
  394. }
  395. }
  396. (void) fclose (child_stderr);
  397. /* close the pipe - WARNING if status is set */
  398. if (spclose (child_process))
  399. result = max_state (result, STATE_WARNING);
  400. if (warn_text == NULL)
  401. warn_text = strdup("");
  402. return result;
  403. }
  404. int
  405. error_scan (char buf[MAX_INPUT_BUFFER], const char *addr)
  406. {
  407. if (strstr (buf, "Network is unreachable"))
  408. die (STATE_CRITICAL, _("CRITICAL - Network unreachable (%s)"), addr);
  409. else if (strstr (buf, "Destination Host Unreachable"))
  410. die (STATE_CRITICAL, _("CRITICAL - Host Unreachable (%s)"), addr);
  411. else if (strstr (buf, "unknown host" ))
  412. die (STATE_CRITICAL, _("CRITICAL - Host not found (%s)"), addr);
  413. else if (strstr (buf, "Time to live exceeded"))
  414. die (STATE_CRITICAL, _("CRITICAL - Time to live exceeded (%s)"), addr);
  415. if (strstr (buf, "(DUP!)") || strstr (buf, "DUPLICATES FOUND")) {
  416. if (warn_text == NULL)
  417. warn_text = strdup (_(WARN_DUPLICATES));
  418. else if (! strstr (warn_text, _(WARN_DUPLICATES)) &&
  419. asprintf (&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1)
  420. die (STATE_UNKNOWN, _("Unable to realloc warn_text"));
  421. return (STATE_WARNING);
  422. }
  423. return (STATE_OK);
  424. }
  425. void
  426. print_help (void)
  427. {
  428. print_revision (progname, revision);
  429. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>");
  430. printf (COPYRIGHT, copyright, email);
  431. printf (_("Use ping to check connection statistics for a remote host."));
  432. printf ("\n\n");
  433. print_usage ();
  434. printf (_(UT_HELP_VRSN));
  435. printf (_(UT_IPv46));
  436. printf (" %s\n", "-H, --hostname=HOST");
  437. printf (" %s\n", _("host to ping"));
  438. printf (" %s\n", "-w, --warning=THRESHOLD");
  439. printf (" %s\n", _("warning threshold pair"));
  440. printf (" %s\n", "-c, --critical=THRESHOLD");
  441. printf (" %s\n", _("critical threshold pair"));
  442. printf (" %s\n", "-p, --packets=INTEGER");
  443. printf (" %s\n", _("number of ICMP ECHO packets to send"));
  444. printf (_("(Default: %d)"), DEFAULT_MAX_PACKETS);
  445. printf (" %s\n", "-L, --link");
  446. printf (" %s\n", _("show HTML in the plugin output (obsoleted by urlize)"));
  447. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  448. printf ("%s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel"));
  449. printf ("%s\n", _("time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the"));
  450. printf ("%s\n", _("percentage of packet loss to trigger an alarm state."));
  451. printf ("\n\n");
  452. printf ("%s\n", _("This plugin uses the ping command to probe the specified host for packet loss"));
  453. printf ("%s\n", _("(percentage) and round trip average (milliseconds). It can produce HTML output"));
  454. printf ("%s\n", _("linking to a traceroute CGI contributed by Ian Cass. The CGI can be found in"));
  455. printf ("%s\n", _("the contrib area of the downloads section at http://www.nagios.org/"));
  456. printf ("\n\n");
  457. printf (_(UT_SUPPORT));
  458. }
  459. void
  460. print_usage (void)
  461. {
  462. printf (_("Usage:"));
  463. printf ("%s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n", progname);
  464. printf (" [-p packets] [-t timeout] [-L] [-4|-6]\n");
  465. }