4
0

check_fping.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 = "devel@nagios-plugins.org";
  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. char *xmtstr = NULL;
  154. double loss;
  155. double rta;
  156. double xmt;
  157. int status = STATE_UNKNOWN;
  158. if (strstr (buf, "not found")) {
  159. die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
  160. }
  161. else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
  162. die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
  163. "host");
  164. }
  165. else if (strstr (buf, "Operation not permitted") || strstr (buf, "No such device") ) {
  166. die (STATE_UNKNOWN, _("FPING UNKNOWN - %s parameter error\n"),
  167. "host");
  168. }
  169. else if (strstr (buf, "is down")) {
  170. die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
  171. }
  172. else if (strstr (buf, "is alive")) {
  173. status = STATE_OK;
  174. }
  175. else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
  176. losstr = strstr (buf, "=");
  177. losstr = 1 + strstr (losstr, "/");
  178. losstr = 1 + strstr (losstr, "/");
  179. rtastr = strstr (buf, "min/avg/max");
  180. rtastr = strstr (rtastr, "=");
  181. rtastr = 1 + index (rtastr, '/');
  182. loss = strtod (losstr, NULL);
  183. rta = strtod (rtastr, NULL);
  184. if (cpl_p == TRUE && loss > cpl)
  185. status = STATE_CRITICAL;
  186. else if (crta_p == TRUE && rta > crta)
  187. status = STATE_CRITICAL;
  188. else if (wpl_p == TRUE && loss > wpl)
  189. status = STATE_WARNING;
  190. else if (wrta_p == TRUE && rta > wrta)
  191. status = STATE_WARNING;
  192. else
  193. status = STATE_OK;
  194. die (status,
  195. _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
  196. state_text (status), server_name, loss, rta,
  197. perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
  198. fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
  199. }
  200. else if(strstr (buf, "xmt/rcv/%loss") ) {
  201. /* no min/max/avg if host was unreachable in fping v2.2.b1 */
  202. /* in v2.4b2: 10.99.0.1 : xmt/rcv/%loss = 0/0/0% */
  203. losstr = strstr (buf, "=");
  204. xmtstr = 1 + losstr;
  205. xmt = strtod (xmtstr, NULL);
  206. if(xmt == 0)
  207. die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
  208. losstr = 1 + strstr (losstr, "/");
  209. losstr = 1 + strstr (losstr, "/");
  210. loss = strtod (losstr, NULL);
  211. if (atoi(losstr) == 100)
  212. status = STATE_CRITICAL;
  213. else if (cpl_p == TRUE && loss > cpl)
  214. status = STATE_CRITICAL;
  215. else if (wpl_p == TRUE && loss > wpl)
  216. status = STATE_WARNING;
  217. else
  218. status = STATE_OK;
  219. /* loss=%.0f%%;%d;%d;0;100 */
  220. die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
  221. state_text (status), server_name, loss ,
  222. perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
  223. }
  224. else {
  225. status = max_state (status, STATE_WARNING);
  226. }
  227. return status;
  228. }
  229. /* process command-line arguments */
  230. int
  231. process_arguments (int argc, char **argv)
  232. {
  233. int c;
  234. char *rv[2];
  235. int option = 0;
  236. static struct option longopts[] = {
  237. {"hostname", required_argument, 0, 'H'},
  238. {"sourceip", required_argument, 0, 'S'},
  239. {"sourceif", required_argument, 0, 'I'},
  240. {"critical", required_argument, 0, 'c'},
  241. {"warning", required_argument, 0, 'w'},
  242. {"bytes", required_argument, 0, 'b'},
  243. {"number", required_argument, 0, 'n'},
  244. {"target-timeout", required_argument, 0, 'T'},
  245. {"interval", required_argument, 0, 'i'},
  246. {"verbose", no_argument, 0, 'v'},
  247. {"version", no_argument, 0, 'V'},
  248. {"help", no_argument, 0, 'h'},
  249. {"use-ipv4", no_argument, 0, '4'},
  250. {"use-ipv6", no_argument, 0, '6'},
  251. {0, 0, 0, 0}
  252. };
  253. rv[PL] = NULL;
  254. rv[RTA] = NULL;
  255. if (argc < 2)
  256. return ERROR;
  257. if (!is_option (argv[1])) {
  258. server_name = argv[1];
  259. argv[1] = argv[0];
  260. argv = &argv[1];
  261. argc--;
  262. }
  263. while (1) {
  264. c = getopt_long (argc, argv, "+hVvH:S:c:w:b:n:T:i:I:46", longopts, &option);
  265. if (c == -1 || c == EOF || c == 1)
  266. break;
  267. switch (c) {
  268. case '?': /* print short usage statement if args not parsable */
  269. usage5 ();
  270. case 'h': /* help */
  271. print_help ();
  272. exit (STATE_OK);
  273. case 'V': /* version */
  274. print_revision (progname, NP_VERSION);
  275. exit (STATE_OK);
  276. case 'v': /* verbose mode */
  277. verbose = TRUE;
  278. break;
  279. case 'H': /* hostname */
  280. if (is_host (optarg) == FALSE) {
  281. usage2 (_("Invalid hostname/address"), optarg);
  282. }
  283. server_name = strscpy (server_name, optarg);
  284. break;
  285. case 'S': /* sourceip */
  286. if (is_host (optarg) == FALSE) {
  287. usage2 (_("Invalid hostname/address"), optarg);
  288. }
  289. sourceip = strscpy (sourceip, optarg);
  290. break;
  291. case 'I': /* sourceip */
  292. sourceif = strscpy (sourceif, optarg);
  293. case '4': /* IPv4 only */
  294. address_family = AF_INET;
  295. break;
  296. case '6': /* IPv6 only */
  297. #ifdef USE_IPV6
  298. address_family = AF_INET6;
  299. #else
  300. usage (_("IPv6 support not available\n"));
  301. #endif
  302. break;
  303. case 'c':
  304. get_threshold (optarg, rv);
  305. if (rv[RTA]) {
  306. crta = strtod (rv[RTA], NULL);
  307. crta_p = TRUE;
  308. rv[RTA] = NULL;
  309. }
  310. if (rv[PL]) {
  311. cpl = atoi (rv[PL]);
  312. cpl_p = TRUE;
  313. rv[PL] = NULL;
  314. }
  315. break;
  316. case 'w':
  317. get_threshold (optarg, rv);
  318. if (rv[RTA]) {
  319. wrta = strtod (rv[RTA], NULL);
  320. wrta_p = TRUE;
  321. rv[RTA] = NULL;
  322. }
  323. if (rv[PL]) {
  324. wpl = atoi (rv[PL]);
  325. wpl_p = TRUE;
  326. rv[PL] = NULL;
  327. }
  328. break;
  329. case 'b': /* bytes per packet */
  330. if (is_intpos (optarg))
  331. packet_size = atoi (optarg);
  332. else
  333. usage (_("Packet size must be a positive integer"));
  334. break;
  335. case 'n': /* number of packets */
  336. if (is_intpos (optarg))
  337. packet_count = atoi (optarg);
  338. else
  339. usage (_("Packet count must be a positive integer"));
  340. break;
  341. case 'T': /* timeout in msec */
  342. if (is_intpos (optarg))
  343. target_timeout = atoi (optarg);
  344. else
  345. usage (_("Target timeout must be a positive integer"));
  346. break;
  347. case 'i': /* interval in msec */
  348. if (is_intpos (optarg))
  349. packet_interval = atoi (optarg);
  350. else
  351. usage (_("Interval must be a positive integer"));
  352. break;
  353. }
  354. }
  355. if (server_name == NULL)
  356. usage4 (_("Hostname was not supplied"));
  357. return OK;
  358. }
  359. int
  360. get_threshold (char *arg, char *rv[2])
  361. {
  362. char *arg1 = NULL;
  363. char *arg2 = NULL;
  364. arg1 = strscpy (arg1, arg);
  365. if (strpbrk (arg1, ",:"))
  366. arg2 = 1 + strpbrk (arg1, ",:");
  367. if (arg2) {
  368. arg1[strcspn (arg1, ",:")] = 0;
  369. if (strstr (arg1, "%") && strstr (arg2, "%"))
  370. die (STATE_UNKNOWN,
  371. _("%s: Only one threshold may be packet loss (%s)\n"), progname,
  372. arg);
  373. if (!strstr (arg1, "%") && !strstr (arg2, "%"))
  374. die (STATE_UNKNOWN,
  375. _("%s: Only one threshold must be packet loss (%s)\n"),
  376. progname, arg);
  377. }
  378. if (arg2 && strstr (arg2, "%")) {
  379. rv[PL] = arg2;
  380. rv[RTA] = arg1;
  381. }
  382. else if (arg2) {
  383. rv[PL] = arg1;
  384. rv[RTA] = arg2;
  385. }
  386. else if (strstr (arg1, "%")) {
  387. rv[PL] = arg1;
  388. }
  389. else {
  390. rv[RTA] = arg1;
  391. }
  392. return OK;
  393. }
  394. void
  395. print_help (void)
  396. {
  397. print_revision (progname, NP_VERSION);
  398. printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
  399. printf (COPYRIGHT, copyright, email);
  400. printf ("%s\n", _("This plugin will use the fping command to ping the specified host for a fast check"));
  401. printf ("%s\n", _("Note that it is necessary to set the suid flag on fping."));
  402. printf ("\n\n");
  403. print_usage ();
  404. printf (UT_HELP_VRSN);
  405. printf (UT_EXTRA_OPTS);
  406. printf (UT_IPv46);
  407. printf (" %s\n", "-H, --hostname=HOST");
  408. printf (" %s\n", _("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)"));
  409. printf (" %s\n", "-w, --warning=THRESHOLD");
  410. printf (" %s\n", _("warning threshold pair"));
  411. printf (" %s\n", "-c, --critical=THRESHOLD");
  412. printf (" %s\n", _("critical threshold pair"));
  413. printf (" %s\n", "-b, --bytes=INTEGER");
  414. printf (" %s (default: %d)\n", _("size of ICMP packet"),PACKET_SIZE);
  415. printf (" %s\n", "-n, --number=INTEGER");
  416. printf (" %s (default: %d)\n", _("number of ICMP packets to send"),PACKET_COUNT);
  417. printf (" %s\n", "-T, --target-timeout=INTEGER");
  418. printf (" %s (default: fping's default for -t)\n", _("Target timeout (ms)"));
  419. printf (" %s\n", "-i, --interval=INTEGER");
  420. printf (" %s (default: fping's default for -p)\n", _("Interval (ms) between sending packets"));
  421. printf (" %s\n", "-S, --sourceip=HOST");
  422. printf (" %s\n", _("name or IP Address of sourceip"));
  423. printf (" %s\n", "-I, --sourceif=IF");
  424. printf (" %s\n", _("source interface name"));
  425. printf (UT_VERBOSE);
  426. printf ("\n");
  427. printf (" %s\n", _("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)"));
  428. printf (" %s\n", _("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of"));
  429. printf (" %s\n", _("packet loss to trigger an alarm state."));
  430. printf ("\n");
  431. printf (" %s\n", _("IPv4 is used by default. Specify -6 to use IPv6."));
  432. printf (UT_SUPPORT);
  433. }
  434. void
  435. print_usage (void)
  436. {
  437. printf ("%s\n", _("Usage:"));
  438. printf (" %s <host_address> -w limit -c limit [-b size] [-n number] [-T number] [-i number]\n", progname);
  439. }