check_fping.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*****************************************************************************
  2. *
  3. * Nagios check_fping plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2007 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_disk plugin
  11. *
  12. * This plugin will use the fping command to ping the specified host for a
  13. * fast check
  14. *
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. *
  30. *****************************************************************************/
  31. const char *progname = "check_fping";
  32. const char *copyright = "2000-2007";
  33. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  34. #include "common.h"
  35. #include "popen.h"
  36. #include "netutils.h"
  37. #include "utils.h"
  38. enum {
  39. PACKET_COUNT = 1,
  40. PACKET_SIZE = 56,
  41. PL = 0,
  42. RTA = 1
  43. };
  44. int textscan (char *buf);
  45. int process_arguments (int, char **);
  46. int get_threshold (char *arg, char *rv[2]);
  47. void print_help (void);
  48. void print_usage (void);
  49. char *server_name = NULL;
  50. char *sourceip = NULL;
  51. char *sourceif = NULL;
  52. int packet_size = PACKET_SIZE;
  53. int packet_count = PACKET_COUNT;
  54. int target_timeout = 0;
  55. int packet_interval = 0;
  56. int verbose = FALSE;
  57. int cpl;
  58. int wpl;
  59. double crta;
  60. double wrta;
  61. int cpl_p = FALSE;
  62. int wpl_p = FALSE;
  63. int crta_p = FALSE;
  64. int wrta_p = FALSE;
  65. int
  66. main (int argc, char **argv)
  67. {
  68. /* normaly should be int result = STATE_UNKNOWN; */
  69. int status = STATE_UNKNOWN;
  70. char *fping_prog = NULL;
  71. char *server = NULL;
  72. char *command_line = NULL;
  73. char *input_buffer = NULL;
  74. char *option_string = "";
  75. input_buffer = malloc (MAX_INPUT_BUFFER);
  76. setlocale (LC_ALL, "");
  77. bindtextdomain (PACKAGE, LOCALEDIR);
  78. textdomain (PACKAGE);
  79. /* Parse extra opts if any */
  80. argv=np_extra_opts (&argc, argv, progname);
  81. if (process_arguments (argc, argv) == ERROR)
  82. usage4 (_("Could not parse arguments"));
  83. server = strscpy (server, server_name);
  84. /* compose the command */
  85. if (target_timeout)
  86. xasprintf(&option_string, "%s-t %d ", option_string, target_timeout);
  87. if (packet_interval)
  88. xasprintf(&option_string, "%s-p %d ", option_string, packet_interval);
  89. if (sourceip)
  90. xasprintf(&option_string, "%s-S %s ", option_string, sourceip);
  91. if (sourceif)
  92. xasprintf(&option_string, "%s-I %s ", option_string, sourceif);
  93. #ifdef PATH_TO_FPING6
  94. if (address_family == AF_INET6)
  95. fping_prog = strdup(PATH_TO_FPING6);
  96. else
  97. fping_prog = strdup(PATH_TO_FPING);
  98. #else
  99. fping_prog = strdup(PATH_TO_FPING);
  100. #endif
  101. xasprintf (&command_line, "%s %s-b %d -c %d %s", fping_prog,
  102. option_string, packet_size, packet_count, server);
  103. if (verbose)
  104. printf ("%s\n", command_line);
  105. /* run the command */
  106. child_process = spopen (command_line);
  107. if (child_process == NULL) {
  108. printf (_("Could not open pipe: %s\n"), command_line);
  109. return STATE_UNKNOWN;
  110. }
  111. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  112. if (child_stderr == NULL) {
  113. printf (_("Could not open stderr for %s\n"), command_line);
  114. }
  115. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  116. if (verbose)
  117. printf ("%s", input_buffer);
  118. status = max_state (status, textscan (input_buffer));
  119. }
  120. /* If we get anything on STDERR, at least set warning */
  121. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  122. status = max_state (status, STATE_WARNING);
  123. if (verbose)
  124. printf ("%s", input_buffer);
  125. status = max_state (status, textscan (input_buffer));
  126. }
  127. (void) fclose (child_stderr);
  128. /* close the pipe */
  129. if (spclose (child_process))
  130. /* need to use max_state not max */
  131. status = max_state (status, STATE_WARNING);
  132. printf ("FPING %s - %s\n", state_text (status), server_name);
  133. return status;
  134. }
  135. int
  136. textscan (char *buf)
  137. {
  138. char *rtastr = NULL;
  139. char *losstr = NULL;
  140. double loss;
  141. double rta;
  142. int status = STATE_UNKNOWN;
  143. if (strstr (buf, "not found")) {
  144. die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
  145. }
  146. else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
  147. die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
  148. "host");
  149. }
  150. else if (strstr (buf, "is down")) {
  151. die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
  152. }
  153. else if (strstr (buf, "is alive")) {
  154. status = STATE_OK;
  155. }
  156. else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
  157. losstr = strstr (buf, "=");
  158. losstr = 1 + strstr (losstr, "/");
  159. losstr = 1 + strstr (losstr, "/");
  160. rtastr = strstr (buf, "min/avg/max");
  161. rtastr = strstr (rtastr, "=");
  162. rtastr = 1 + index (rtastr, '/');
  163. loss = strtod (losstr, NULL);
  164. rta = strtod (rtastr, NULL);
  165. if (cpl_p == TRUE && loss > cpl)
  166. status = STATE_CRITICAL;
  167. else if (crta_p == TRUE && rta > crta)
  168. status = STATE_CRITICAL;
  169. else if (wpl_p == TRUE && loss > wpl)
  170. status = STATE_WARNING;
  171. else if (wrta_p == TRUE && rta > wrta)
  172. status = STATE_WARNING;
  173. else
  174. status = STATE_OK;
  175. die (status,
  176. _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
  177. state_text (status), server_name, loss, rta,
  178. perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
  179. fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
  180. }
  181. else if(strstr (buf, "xmt/rcv/%loss") ) {
  182. /* no min/max/avg if host was unreachable in fping v2.2.b1 */
  183. losstr = strstr (buf, "=");
  184. losstr = 1 + strstr (losstr, "/");
  185. losstr = 1 + strstr (losstr, "/");
  186. loss = strtod (losstr, NULL);
  187. if (atoi(losstr) == 100)
  188. status = STATE_CRITICAL;
  189. else if (cpl_p == TRUE && loss > cpl)
  190. status = STATE_CRITICAL;
  191. else if (wpl_p == TRUE && loss > wpl)
  192. status = STATE_WARNING;
  193. else
  194. status = STATE_OK;
  195. /* loss=%.0f%%;%d;%d;0;100 */
  196. die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
  197. state_text (status), server_name, loss ,
  198. perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
  199. }
  200. else {
  201. status = max_state (status, STATE_WARNING);
  202. }
  203. return status;
  204. }
  205. /* process command-line arguments */
  206. int
  207. process_arguments (int argc, char **argv)
  208. {
  209. int c;
  210. char *rv[2];
  211. int option = 0;
  212. static struct option longopts[] = {
  213. {"hostname", required_argument, 0, 'H'},
  214. {"sourceip", required_argument, 0, 'S'},
  215. {"sourceif", required_argument, 0, 'I'},
  216. {"critical", required_argument, 0, 'c'},
  217. {"warning", required_argument, 0, 'w'},
  218. {"bytes", required_argument, 0, 'b'},
  219. {"number", required_argument, 0, 'n'},
  220. {"target-timeout", required_argument, 0, 'T'},
  221. {"interval", required_argument, 0, 'i'},
  222. {"verbose", no_argument, 0, 'v'},
  223. {"version", no_argument, 0, 'V'},
  224. {"help", no_argument, 0, 'h'},
  225. {"use-ipv4", no_argument, 0, '4'},
  226. {"use-ipv6", no_argument, 0, '6'},
  227. {0, 0, 0, 0}
  228. };
  229. rv[PL] = NULL;
  230. rv[RTA] = NULL;
  231. if (argc < 2)
  232. return ERROR;
  233. if (!is_option (argv[1])) {
  234. server_name = argv[1];
  235. argv[1] = argv[0];
  236. argv = &argv[1];
  237. argc--;
  238. }
  239. while (1) {
  240. c = getopt_long (argc, argv, "+hVvH:S:c:w:b:n:T:i:I:46", longopts, &option);
  241. if (c == -1 || c == EOF || c == 1)
  242. break;
  243. switch (c) {
  244. case '?': /* print short usage statement if args not parsable */
  245. usage5 ();
  246. case 'h': /* help */
  247. print_help ();
  248. exit (STATE_OK);
  249. case 'V': /* version */
  250. print_revision (progname, NP_VERSION);
  251. exit (STATE_OK);
  252. case 'v': /* verbose mode */
  253. verbose = TRUE;
  254. break;
  255. case 'H': /* hostname */
  256. if (is_host (optarg) == FALSE) {
  257. usage2 (_("Invalid hostname/address"), optarg);
  258. }
  259. server_name = strscpy (server_name, optarg);
  260. break;
  261. case 'S': /* sourceip */
  262. if (is_host (optarg) == FALSE) {
  263. usage2 (_("Invalid hostname/address"), optarg);
  264. }
  265. sourceip = strscpy (sourceip, optarg);
  266. break;
  267. case 'I': /* sourceip */
  268. sourceif = strscpy (sourceif, optarg);
  269. case '4': /* IPv4 only */
  270. address_family = AF_INET;
  271. break;
  272. case '6': /* IPv6 only */
  273. #ifdef USE_IPV6
  274. address_family = AF_INET6;
  275. #else
  276. usage (_("IPv6 support not available\n"));
  277. #endif
  278. break;
  279. case 'c':
  280. get_threshold (optarg, rv);
  281. if (rv[RTA]) {
  282. crta = strtod (rv[RTA], NULL);
  283. crta_p = TRUE;
  284. rv[RTA] = NULL;
  285. }
  286. if (rv[PL]) {
  287. cpl = atoi (rv[PL]);
  288. cpl_p = TRUE;
  289. rv[PL] = NULL;
  290. }
  291. break;
  292. case 'w':
  293. get_threshold (optarg, rv);
  294. if (rv[RTA]) {
  295. wrta = strtod (rv[RTA], NULL);
  296. wrta_p = TRUE;
  297. rv[RTA] = NULL;
  298. }
  299. if (rv[PL]) {
  300. wpl = atoi (rv[PL]);
  301. wpl_p = TRUE;
  302. rv[PL] = NULL;
  303. }
  304. break;
  305. case 'b': /* bytes per packet */
  306. if (is_intpos (optarg))
  307. packet_size = atoi (optarg);
  308. else
  309. usage (_("Packet size must be a positive integer"));
  310. break;
  311. case 'n': /* number of packets */
  312. if (is_intpos (optarg))
  313. packet_count = atoi (optarg);
  314. else
  315. usage (_("Packet count must be a positive integer"));
  316. break;
  317. case 'T': /* timeout in msec */
  318. if (is_intpos (optarg))
  319. target_timeout = atoi (optarg);
  320. else
  321. usage (_("Target timeout must be a positive integer"));
  322. break;
  323. case 'i': /* interval in msec */
  324. if (is_intpos (optarg))
  325. packet_interval = atoi (optarg);
  326. else
  327. usage (_("Interval must be a positive integer"));
  328. break;
  329. }
  330. }
  331. if (server_name == NULL)
  332. usage4 (_("Hostname was not supplied"));
  333. return OK;
  334. }
  335. int
  336. get_threshold (char *arg, char *rv[2])
  337. {
  338. char *arg1 = NULL;
  339. char *arg2 = NULL;
  340. arg1 = strscpy (arg1, arg);
  341. if (strpbrk (arg1, ",:"))
  342. arg2 = 1 + strpbrk (arg1, ",:");
  343. if (arg2) {
  344. arg1[strcspn (arg1, ",:")] = 0;
  345. if (strstr (arg1, "%") && strstr (arg2, "%"))
  346. die (STATE_UNKNOWN,
  347. _("%s: Only one threshold may be packet loss (%s)\n"), progname,
  348. arg);
  349. if (!strstr (arg1, "%") && !strstr (arg2, "%"))
  350. die (STATE_UNKNOWN,
  351. _("%s: Only one threshold must be packet loss (%s)\n"),
  352. progname, arg);
  353. }
  354. if (arg2 && strstr (arg2, "%")) {
  355. rv[PL] = arg2;
  356. rv[RTA] = arg1;
  357. }
  358. else if (arg2) {
  359. rv[PL] = arg1;
  360. rv[RTA] = arg2;
  361. }
  362. else if (strstr (arg1, "%")) {
  363. rv[PL] = arg1;
  364. }
  365. else {
  366. rv[RTA] = arg1;
  367. }
  368. return OK;
  369. }
  370. void
  371. print_help (void)
  372. {
  373. print_revision (progname, NP_VERSION);
  374. printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
  375. printf (COPYRIGHT, copyright, email);
  376. printf ("%s\n", _("This plugin will use the fping command to ping the specified host for a fast check"));
  377. printf ("%s\n", _("Note that it is necessary to set the suid flag on fping."));
  378. printf ("\n\n");
  379. print_usage ();
  380. printf (UT_HELP_VRSN);
  381. printf (UT_EXTRA_OPTS);
  382. printf (UT_IPv46);
  383. printf (" %s\n", "-H, --hostname=HOST");
  384. printf (" %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)"));
  385. printf (" %s\n", "-w, --warning=THRESHOLD");
  386. printf (" %s\n", _("warning threshold pair"));
  387. printf (" %s\n", "-c, --critical=THRESHOLD");
  388. printf (" %s\n", _("critical threshold pair"));
  389. printf (" %s\n", "-b, --bytes=INTEGER");
  390. printf (" %s (default: %d)\n", _("size of ICMP packet"),PACKET_SIZE);
  391. printf (" %s\n", "-n, --number=INTEGER");
  392. printf (" %s (default: %d)\n", _("number of ICMP packets to send"),PACKET_COUNT);
  393. printf (" %s\n", "-T, --target-timeout=INTEGER");
  394. printf (" %s (default: fping's default for -t)\n", _("Target timeout (ms)"));
  395. printf (" %s\n", "-i, --interval=INTEGER");
  396. printf (" %s (default: fping's default for -p)\n", _("Interval (ms) between sending packets"));
  397. printf (" %s\n", "-S, --sourceip=HOST");
  398. printf (" %s\n", _("name or IP Address of sourceip"));
  399. printf (" %s\n", "-I, --sourceif=IF");
  400. printf (" %s\n", _("source interface name"));
  401. printf (UT_VERBOSE);
  402. printf ("\n");
  403. printf (" %s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)"));
  404. printf (" %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of"));
  405. printf (" %s\n", _("packet loss to trigger an alarm state."));
  406. printf ("\n");
  407. printf (" %s\n", _("IPv4 is used by default. Specify -6 to use IPv6."));
  408. printf (UT_SUPPORT);
  409. }
  410. void
  411. print_usage (void)
  412. {
  413. printf ("%s\n", _("Usage:"));
  414. printf (" %s <host_address> -w limit -c limit [-b size] [-n number] [-T number] [-i number]\n", progname);
  415. }