check_fping.c 9.5 KB

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