check_fping.c 13 KB

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