check_fping.c 12 KB

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