4
0

check_fping.c 11 KB

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