check_fping.c 14 KB

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