4
0

check_fping.c 9.7 KB

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