check_fping.c 9.7 KB

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