check_fping.c 12 KB

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