check_fping.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /******************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. $Id$
  14. ******************************************************************************/
  15. const char *progname = "check_fping";
  16. const char *revision = "$Revision$";
  17. const char *copyright = "2000-2003";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #include "popen.h"
  21. #include "netutils.h"
  22. #include "utils.h"
  23. enum {
  24. PACKET_COUNT = 1,
  25. PACKET_SIZE = 56,
  26. PL = 0,
  27. RTA = 1
  28. };
  29. int textscan (char *buf);
  30. int process_arguments (int, char **);
  31. int get_threshold (char *arg, char *rv[2]);
  32. void print_help (void);
  33. void print_usage (void);
  34. char *server_name = NULL;
  35. int packet_size = PACKET_SIZE;
  36. int packet_count = PACKET_COUNT;
  37. int verbose = FALSE;
  38. int cpl;
  39. int wpl;
  40. double crta;
  41. double wrta;
  42. int cpl_p = FALSE;
  43. int wpl_p = FALSE;
  44. int crta_p = FALSE;
  45. int wrta_p = FALSE;
  46. int
  47. main (int argc, char **argv)
  48. {
  49. int status = STATE_UNKNOWN;
  50. char *server = NULL;
  51. char *command_line = NULL;
  52. char *input_buffer = NULL;
  53. input_buffer = malloc (MAX_INPUT_BUFFER);
  54. setlocale (LC_ALL, "");
  55. bindtextdomain (PACKAGE, LOCALEDIR);
  56. textdomain (PACKAGE);
  57. if (process_arguments (argc, argv) == ERROR)
  58. usage (_("check_fping: could not parse arguments\n"));
  59. server = strscpy (server, server_name);
  60. /* compose the command */
  61. asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
  62. packet_size, packet_count, server);
  63. if (verbose)
  64. printf ("%s\n", command_line);
  65. /* run the command */
  66. child_process = spopen (command_line);
  67. if (child_process == NULL) {
  68. printf (_("Could not open pipe: %s\n"), command_line);
  69. return STATE_UNKNOWN;
  70. }
  71. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  72. if (child_stderr == NULL) {
  73. printf (_("Could not open stderr for %s\n"), command_line);
  74. }
  75. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  76. if (verbose)
  77. printf ("%s", input_buffer);
  78. status = max_state (status, textscan (input_buffer));
  79. }
  80. /* If we get anything on STDERR, at least set warning */
  81. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  82. status = max_state (status, STATE_WARNING);
  83. if (verbose)
  84. printf ("%s", input_buffer);
  85. status = max_state (status, textscan (input_buffer));
  86. }
  87. (void) fclose (child_stderr);
  88. /* close the pipe */
  89. if (spclose (child_process))
  90. /* need to use max_state not max */
  91. status = max_state (status, STATE_WARNING);
  92. printf ("FPING %s - %s\n", state_text (status), server_name);
  93. return status;
  94. }
  95. int
  96. textscan (char *buf)
  97. {
  98. char *rtastr = NULL;
  99. char *losstr = NULL;
  100. double loss;
  101. double rta;
  102. int status = STATE_UNKNOWN;
  103. if (strstr (buf, "not found")) {
  104. die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
  105. }
  106. else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
  107. die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
  108. "host");
  109. }
  110. else if (strstr (buf, "is down")) {
  111. die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
  112. }
  113. else if (strstr (buf, "is alive")) {
  114. status = STATE_OK;
  115. }
  116. else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
  117. losstr = strstr (buf, "=");
  118. losstr = 1 + strstr (losstr, "/");
  119. losstr = 1 + strstr (losstr, "/");
  120. rtastr = strstr (buf, "min/avg/max");
  121. rtastr = strstr (rtastr, "=");
  122. rtastr = 1 + index (rtastr, '/');
  123. loss = strtod (losstr, NULL);
  124. rta = strtod (rtastr, NULL);
  125. if (cpl_p == TRUE && loss > cpl)
  126. status = STATE_CRITICAL;
  127. else if (crta_p == TRUE && rta > crta)
  128. status = STATE_CRITICAL;
  129. else if (wpl_p == TRUE && loss > wpl)
  130. status = STATE_WARNING;
  131. else if (wrta_p == TRUE && rta > wrta)
  132. status = STATE_WARNING;
  133. else
  134. status = STATE_OK;
  135. die (status,
  136. _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
  137. state_text (status), server_name, loss, rta,
  138. perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
  139. fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
  140. }
  141. else if(strstr (buf, "xmt/rcv/%loss") ) {
  142. /* no min/max/avg if host was unreachable in fping v2.2.b1 */
  143. losstr = strstr (buf, "=");
  144. losstr = 1 + strstr (losstr, "/");
  145. losstr = 1 + strstr (losstr, "/");
  146. loss = strtod (losstr, NULL);
  147. if (atoi(losstr) == 100)
  148. status = STATE_CRITICAL;
  149. else if (cpl_p == TRUE && loss > cpl)
  150. status = STATE_CRITICAL;
  151. else if (wpl_p == TRUE && loss > wpl)
  152. status = STATE_WARNING;
  153. else
  154. status = STATE_OK;
  155. /* loss=%.0f%%;%d;%d;0;100 */
  156. die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
  157. state_text (status), server_name, loss ,
  158. perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
  159. }
  160. else {
  161. status = max_state (status, STATE_WARNING);
  162. }
  163. return status;
  164. }
  165. /* process command-line arguments */
  166. int
  167. process_arguments (int argc, char **argv)
  168. {
  169. int c;
  170. char *rv[2];
  171. int option = 0;
  172. static struct option longopts[] = {
  173. {"hostname", required_argument, 0, 'H'},
  174. {"critical", required_argument, 0, 'c'},
  175. {"warning", required_argument, 0, 'w'},
  176. {"bytes", required_argument, 0, 'b'},
  177. {"number", required_argument, 0, 'n'},
  178. {"verbose", no_argument, 0, 'v'},
  179. {"version", no_argument, 0, 'V'},
  180. {"help", no_argument, 0, 'h'},
  181. {0, 0, 0, 0}
  182. };
  183. rv[PL] = NULL;
  184. rv[RTA] = NULL;
  185. if (argc < 2)
  186. return ERROR;
  187. if (!is_option (argv[1])) {
  188. server_name = argv[1];
  189. argv[1] = argv[0];
  190. argv = &argv[1];
  191. argc--;
  192. }
  193. while (1) {
  194. c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", longopts, &option);
  195. if (c == -1 || c == EOF || c == 1)
  196. break;
  197. switch (c) {
  198. case '?': /* print short usage statement if args not parsable */
  199. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  200. print_usage ();
  201. exit (STATE_UNKNOWN);
  202. case 'h': /* help */
  203. print_help ();
  204. exit (STATE_OK);
  205. case 'V': /* version */
  206. print_revision (progname, revision);
  207. exit (STATE_OK);
  208. case 'v': /* verbose mode */
  209. verbose = TRUE;
  210. break;
  211. case 'H': /* hostname */
  212. if (is_host (optarg) == FALSE) {
  213. usage2 (_("Invalid hostname/address"), optarg);
  214. }
  215. server_name = strscpy (server_name, optarg);
  216. break;
  217. case 'c':
  218. get_threshold (optarg, rv);
  219. if (rv[RTA]) {
  220. crta = strtod (rv[RTA], NULL);
  221. crta_p = TRUE;
  222. rv[RTA] = NULL;
  223. }
  224. if (rv[PL]) {
  225. cpl = atoi (rv[PL]);
  226. cpl_p = TRUE;
  227. rv[PL] = NULL;
  228. }
  229. break;
  230. case 'w':
  231. get_threshold (optarg, rv);
  232. if (rv[RTA]) {
  233. wrta = strtod (rv[RTA], NULL);
  234. wrta_p = TRUE;
  235. rv[RTA] = NULL;
  236. }
  237. if (rv[PL]) {
  238. wpl = atoi (rv[PL]);
  239. wpl_p = TRUE;
  240. rv[PL] = NULL;
  241. }
  242. break;
  243. case 'b': /* bytes per packet */
  244. if (is_intpos (optarg))
  245. packet_size = atoi (optarg);
  246. else
  247. usage (_("Packet size must be a positive integer"));
  248. break;
  249. case 'n': /* number of packets */
  250. if (is_intpos (optarg))
  251. packet_count = atoi (optarg);
  252. else
  253. usage (_("Packet count must be a positive integer"));
  254. break;
  255. }
  256. }
  257. if (server_name == NULL)
  258. usage (_("Hostname was not supplied\n\n"));
  259. return OK;
  260. }
  261. int
  262. get_threshold (char *arg, char *rv[2])
  263. {
  264. char *arg1 = NULL;
  265. char *arg2 = NULL;
  266. arg1 = strscpy (arg1, arg);
  267. if (strpbrk (arg1, ",:"))
  268. arg2 = 1 + strpbrk (arg1, ",:");
  269. if (arg2) {
  270. arg1[strcspn (arg1, ",:")] = 0;
  271. if (strstr (arg1, "%") && strstr (arg2, "%"))
  272. die (STATE_UNKNOWN,
  273. _("%s: Only one threshold may be packet loss (%s)\n"), progname,
  274. arg);
  275. if (!strstr (arg1, "%") && !strstr (arg2, "%"))
  276. die (STATE_UNKNOWN,
  277. _("%s: Only one threshold must be packet loss (%s)\n"),
  278. progname, arg);
  279. }
  280. if (arg2 && strstr (arg2, "%")) {
  281. rv[PL] = arg2;
  282. rv[RTA] = arg1;
  283. }
  284. else if (arg2) {
  285. rv[PL] = arg1;
  286. rv[RTA] = arg2;
  287. }
  288. else if (strstr (arg1, "%")) {
  289. rv[PL] = arg1;
  290. }
  291. else {
  292. rv[RTA] = arg1;
  293. }
  294. return OK;
  295. }
  296. void
  297. print_help (void)
  298. {
  299. print_revision (progname, "$Revision$");
  300. printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
  301. printf (COPYRIGHT, copyright, email);
  302. printf (_("\
  303. This plugin will use the /bin/fping command (from saint) to ping the\n\
  304. specified host for a fast check if the host is alive. Note that it is\n\
  305. necessary to set the suid flag on fping.\n\n"));
  306. print_usage ();
  307. printf (_(UT_HELP_VRSN));
  308. printf (_("\
  309. -H, --hostname=HOST\n\
  310. Name or IP Address of host to ping (IP Address bypasses name lookup,\n\
  311. reducing system load)\n\
  312. -w, --warning=THRESHOLD\n\
  313. warning threshold pair\n\
  314. -c, --critical=THRESHOLD\n\
  315. critical threshold pair\n\
  316. -b, --bytes=INTEGER\n\
  317. Size of ICMP packet (default: %d)\n\
  318. -n, --number=INTEGER\n\
  319. Number of ICMP packets to send (default: %d)\n"),
  320. PACKET_SIZE, PACKET_COUNT);
  321. printf (_(UT_VERBOSE));
  322. printf (_("\n\
  323. THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n\
  324. time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n\
  325. percentage of packet loss to trigger an alarm state.\n"));
  326. printf (_(UT_SUPPORT));
  327. }
  328. void
  329. print_usage (void)
  330. {
  331. printf (_("Usage: %s <host_address>\n"), progname);
  332. printf (_(UT_HLP_VRS), progname, progname);
  333. }