check_fping.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 (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 (status, STATE_WARNING);
  88. if (verbose)
  89. printf ("%s", input_buffer);
  90. status = max (status, textscan (input_buffer));
  91. }
  92. (void) fclose (child_stderr);
  93. /* close the pipe */
  94. if (spclose (child_process))
  95. status = max (status, STATE_WARNING);
  96. printf ("FPING %s - %s\n", state_text (status), server_name);
  97. return status;
  98. }
  99. int
  100. textscan (char *buf)
  101. {
  102. char *rtastr = NULL;
  103. char *losstr = NULL;
  104. double loss;
  105. double rta;
  106. int status = STATE_UNKNOWN;
  107. if (strstr (buf, "not found")) {
  108. terminate (STATE_CRITICAL, "FPING unknown - %s not found\n", server_name);
  109. }
  110. else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
  111. terminate (STATE_CRITICAL, "FPING critical - %s is unreachable\n",
  112. "host");
  113. }
  114. else if (strstr (buf, "is down")) {
  115. terminate (STATE_CRITICAL, "FPING critical - %s is down\n", server_name);
  116. }
  117. else if (strstr (buf, "is alive")) {
  118. status = STATE_OK;
  119. }
  120. else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
  121. losstr = strstr (buf, "=");
  122. losstr = 1 + strstr (losstr, "/");
  123. losstr = 1 + strstr (losstr, "/");
  124. rtastr = strstr (buf, "min/avg/max");
  125. rtastr = strstr (rtastr, "=");
  126. rtastr = 1 + index (rtastr, '/');
  127. loss = strtod (losstr, NULL);
  128. rta = strtod (rtastr, NULL);
  129. if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
  130. status = STATE_CRITICAL;
  131. else if (crta != UNKNOWN_TRIP_TIME && rta > crta)
  132. status = STATE_CRITICAL;
  133. else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
  134. status = STATE_WARNING;
  135. else if (wrta != UNKNOWN_TRIP_TIME && rta > wrta)
  136. status = STATE_WARNING;
  137. else
  138. status = STATE_OK;
  139. terminate (status, "FPING %s - %s (loss=%f%%, rta=%f ms)\n",
  140. state_text (status), server_name, loss, rta);
  141. }
  142. else {
  143. status = max (status, STATE_WARNING);
  144. }
  145. return status;
  146. }
  147. /* process command-line arguments */
  148. int
  149. process_arguments (int argc, char **argv)
  150. {
  151. int c;
  152. char *rv[2];
  153. #ifdef HAVE_GETOPT_H
  154. int option_index = 0;
  155. static struct option long_options[] = {
  156. {"hostname", required_argument, 0, 'H'},
  157. {"critical", required_argument, 0, 'c'},
  158. {"warning", required_argument, 0, 'w'},
  159. {"bytes", required_argument, 0, 'b'},
  160. {"number", required_argument, 0, 'n'},
  161. {"verbose", no_argument, 0, 'v'},
  162. {"version", no_argument, 0, 'V'},
  163. {"help", no_argument, 0, 'h'},
  164. {0, 0, 0, 0}
  165. };
  166. #endif
  167. rv[PL] = NULL;
  168. rv[RTA] = NULL;
  169. if (argc < 2)
  170. return ERROR;
  171. if (!is_option (argv[1])) {
  172. server_name = argv[1];
  173. argv[1] = argv[0];
  174. argv = &argv[1];
  175. argc--;
  176. }
  177. while (1) {
  178. #ifdef HAVE_GETOPT_H
  179. c =
  180. getopt_long (argc, argv, "+hVvH:c:w:b:n:", long_options, &option_index);
  181. #else
  182. c = getopt (argc, argv, "+hVvH:c:w:b:n:");
  183. #endif
  184. if (c == -1 || c == EOF || c == 1)
  185. break;
  186. switch (c) {
  187. case '?': /* print short usage statement if args not parsable */
  188. printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
  189. print_usage ();
  190. exit (STATE_UNKNOWN);
  191. case 'h': /* help */
  192. print_help ();
  193. exit (STATE_OK);
  194. case 'V': /* version */
  195. print_revision (my_basename (argv[0]), "$Revision$");
  196. exit (STATE_OK);
  197. case 'v': /* verbose mode */
  198. verbose = TRUE;
  199. break;
  200. case 'H': /* hostname */
  201. if (is_host (optarg) == FALSE) {
  202. printf ("Invalid host name/address\n\n");
  203. print_usage ();
  204. exit (STATE_UNKNOWN);
  205. }
  206. server_name = strscpy (server_name, optarg);
  207. break;
  208. case 'c':
  209. get_threshold (optarg, rv);
  210. if (rv[RTA]) {
  211. crta = strtod (rv[RTA], NULL);
  212. rv[RTA] = NULL;
  213. }
  214. if (rv[PL]) {
  215. cpl = atoi (rv[PL]);
  216. rv[PL] = NULL;
  217. }
  218. break;
  219. case 'w':
  220. get_threshold (optarg, rv);
  221. if (rv[RTA]) {
  222. wrta = strtod (rv[RTA], NULL);
  223. rv[RTA] = NULL;
  224. }
  225. if (rv[PL]) {
  226. wpl = atoi (rv[PL]);
  227. rv[PL] = NULL;
  228. }
  229. break;
  230. case 'b': /* bytes per packet */
  231. if (is_intpos (optarg))
  232. packet_size = atoi (optarg);
  233. else
  234. usage ("Packet size must be a positive integer");
  235. break;
  236. case 'n': /* number of packets */
  237. if (is_intpos (optarg))
  238. packet_count = atoi (optarg);
  239. else
  240. usage ("Packet count must be a positive integer");
  241. break;
  242. }
  243. }
  244. if (server_name == NULL)
  245. usage ("Host name was not supplied\n\n");
  246. return OK;
  247. }
  248. int
  249. get_threshold (char *arg, char *rv[2])
  250. {
  251. char *arg1 = NULL;
  252. char *arg2 = NULL;
  253. arg1 = strscpy (arg1, arg);
  254. if (strpbrk (arg1, ",:"))
  255. arg2 = 1 + strpbrk (arg1, ",:");
  256. if (arg2) {
  257. arg1[strcspn (arg1, ",:")] = 0;
  258. if (strstr (arg1, "%") && strstr (arg2, "%"))
  259. terminate (STATE_UNKNOWN,
  260. "%s: Only one threshold may be packet loss (%s)\n", PROGNAME,
  261. arg);
  262. if (!strstr (arg1, "%") && !strstr (arg2, "%"))
  263. terminate (STATE_UNKNOWN,
  264. "%s: Only one threshold must be packet loss (%s)\n",
  265. PROGNAME, arg);
  266. }
  267. if (arg2 && strstr (arg2, "%")) {
  268. rv[PL] = arg2;
  269. rv[RTA] = arg1;
  270. }
  271. else if (arg2) {
  272. rv[PL] = arg1;
  273. rv[RTA] = arg2;
  274. }
  275. else if (strstr (arg1, "%")) {
  276. rv[PL] = arg1;
  277. }
  278. else {
  279. rv[RTA] = arg1;
  280. }
  281. return OK;
  282. }
  283. void
  284. print_usage (void)
  285. {
  286. printf ("Usage: %s <host_address>\n", PROGNAME);
  287. }
  288. void
  289. print_help (void)
  290. {
  291. print_revision (PROGNAME, "$Revision$");
  292. printf
  293. ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n\n"
  294. "This plugin will use the /bin/fping command (from saint) to ping the\n"
  295. "specified host for a fast check if the host is alive. Note that it is\n"
  296. "necessary to set the suid flag on fping.\n\n");
  297. print_usage ();
  298. printf
  299. ("\nOptions:\n"
  300. "-H, --hostname=HOST\n"
  301. " Name or IP Address of host to ping (IP Address bypasses name lookup,\n"
  302. " reducing system load)\n"
  303. "-w, --warning=THRESHOLD\n"
  304. " warning threshold pair\n"
  305. "-c, --critical=THRESHOLD\n"
  306. " critical threshold pair\n"
  307. "-b, --bytes=INTEGER\n"
  308. " Size of ICMP packet (default: %d)\n"
  309. "-n, --number=INTEGER\n"
  310. " Number of ICMP packets to send (default: %d)\n"
  311. "-v, --verbose\n"
  312. " Show details for command-line debugging (do not use with nagios server)\n"
  313. "-h, --help\n"
  314. " Print this help screen\n"
  315. "-V, --version\n"
  316. " Print version information\n"
  317. "THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel\n"
  318. "time (ms) which triggers a WARNING or CRITICAL state, and <pl> is the\n"
  319. "percentage of packet loss to trigger an alarm state.\n",
  320. PACKET_SIZE, PACKET_COUNT);
  321. }