check_fping.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 "netutils.h"
  36. #include "utils.h"
  37. #define PACKET_COUNT 1
  38. #define PACKET_SIZE 56
  39. #define UNKNOWN_PACKET_LOSS 200 /* 200% */
  40. #define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */
  41. #define PL 0
  42. #define RTA 1
  43. int textscan (char *buf);
  44. int process_arguments (int, char **);
  45. int get_threshold (char *arg, char *rv[2]);
  46. void print_help (void);
  47. void print_usage (void);
  48. char *server_name = NULL;
  49. int cpl = UNKNOWN_PACKET_LOSS;
  50. int wpl = UNKNOWN_PACKET_LOSS;
  51. double crta = UNKNOWN_TRIP_TIME;
  52. double wrta = UNKNOWN_TRIP_TIME;
  53. int packet_size = PACKET_SIZE;
  54. int packet_count = PACKET_COUNT;
  55. int verbose = FALSE;
  56. int
  57. main (int argc, char **argv)
  58. {
  59. int status = STATE_UNKNOWN;
  60. char *server = NULL;
  61. char *command_line = NULL;
  62. char *input_buffer = NULL;
  63. input_buffer = malloc (MAX_INPUT_BUFFER);
  64. if (process_arguments (argc, argv) == ERROR)
  65. usage (_("Could not parse arguments\n"));
  66. server = strscpy (server, server_name);
  67. /* compose the command */
  68. asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
  69. packet_size, packet_count, server);
  70. if (verbose)
  71. printf ("%s\n", command_line);
  72. /* run the command */
  73. child_process = spopen (command_line);
  74. if (child_process == NULL) {
  75. printf (_("Unable to open pipe: %s\n"), command_line);
  76. return STATE_UNKNOWN;
  77. }
  78. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  79. if (child_stderr == NULL) {
  80. printf (_("Could not open stderr for %s\n"), command_line);
  81. }
  82. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  83. if (verbose)
  84. printf ("%s", input_buffer);
  85. status = max_state (status, textscan (input_buffer));
  86. }
  87. /* If we get anything on STDERR, at least set warning */
  88. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  89. status = max_state (status, STATE_WARNING);
  90. if (verbose)
  91. printf ("%s", input_buffer);
  92. status = max_state (status, textscan (input_buffer));
  93. }
  94. (void) fclose (child_stderr);
  95. /* close the pipe */
  96. if (spclose (child_process))
  97. /* need to use max_state not max */
  98. status = max_state (status, STATE_WARNING);
  99. printf ("FPING %s - %s\n", state_text (status), server_name);
  100. return status;
  101. }
  102. int
  103. textscan (char *buf)
  104. {
  105. char *rtastr = NULL;
  106. char *losstr = NULL;
  107. double loss;
  108. double rta;
  109. int status = STATE_UNKNOWN;
  110. if (strstr (buf, "not found")) {
  111. die (STATE_CRITICAL, _("FPING unknown - %s not found\n"), server_name);
  112. }
  113. else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
  114. die (STATE_CRITICAL, _("FPING critical - %s is unreachable\n"),
  115. "host");
  116. }
  117. else if (strstr (buf, "is down")) {
  118. die (STATE_CRITICAL, _("FPING critical - %s is down\n"), server_name);
  119. }
  120. else if (strstr (buf, "is alive")) {
  121. status = STATE_OK;
  122. }
  123. else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
  124. losstr = strstr (buf, "=");
  125. losstr = 1 + strstr (losstr, "/");
  126. losstr = 1 + strstr (losstr, "/");
  127. rtastr = strstr (buf, "min/avg/max");
  128. rtastr = strstr (rtastr, "=");
  129. rtastr = 1 + index (rtastr, '/');
  130. loss = strtod (losstr, NULL);
  131. rta = strtod (rtastr, NULL);
  132. if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
  133. status = STATE_CRITICAL;
  134. else if (crta <= 0 && rta > crta)
  135. status = STATE_CRITICAL;
  136. else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
  137. status = STATE_WARNING;
  138. else if (wrta >= 0 && rta > wrta)
  139. status = STATE_WARNING;
  140. else
  141. status = STATE_OK;
  142. die (status, _("FPING %s - %s (loss=%f%%, rta=%f ms)\n"),
  143. state_text (status), server_name, loss, rta);
  144. }
  145. else if(strstr (buf, "xmt/rcv/%loss") ) {
  146. /* no min/max/avg if host was unreachable in fping v2.2.b1 */
  147. losstr = strstr (buf, "=");
  148. losstr = 1 + strstr (losstr, "/");
  149. losstr = 1 + strstr (losstr, "/");
  150. loss = strtod (losstr, NULL);
  151. if (atoi(losstr) == 100)
  152. status = STATE_CRITICAL;
  153. else if (cpl != UNKNOWN_PACKET_LOSS && loss > cpl)
  154. status = STATE_CRITICAL;
  155. else if (wpl != UNKNOWN_PACKET_LOSS && loss > wpl)
  156. status = STATE_WARNING;
  157. else
  158. status = STATE_OK;
  159. die (status, _("FPING %s - %s (loss=%f%% )\n"),
  160. state_text (status), server_name, loss );
  161. }
  162. else {
  163. status = max_state (status, STATE_WARNING);
  164. }
  165. return status;
  166. }
  167. /* process command-line arguments */
  168. int
  169. process_arguments (int argc, char **argv)
  170. {
  171. int c;
  172. char *rv[2];
  173. int option = 0;
  174. static struct option longopts[] = {
  175. {"hostname", required_argument, 0, 'H'},
  176. {"critical", required_argument, 0, 'c'},
  177. {"warning", required_argument, 0, 'w'},
  178. {"bytes", required_argument, 0, 'b'},
  179. {"number", required_argument, 0, 'n'},
  180. {"verbose", no_argument, 0, 'v'},
  181. {"version", no_argument, 0, 'V'},
  182. {"help", no_argument, 0, 'h'},
  183. {0, 0, 0, 0}
  184. };
  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. c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", longopts, &option);
  197. if (c == -1 || c == EOF || c == 1)
  198. break;
  199. switch (c) {
  200. case '?': /* print short usage statement if args not parsable */
  201. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  202. print_usage ();
  203. exit (STATE_UNKNOWN);
  204. case 'h': /* help */
  205. print_help ();
  206. exit (STATE_OK);
  207. case 'V': /* version */
  208. print_revision (progname, revision);
  209. exit (STATE_OK);
  210. case 'v': /* verbose mode */
  211. verbose = TRUE;
  212. break;
  213. case 'H': /* hostname */
  214. if (is_host (optarg) == FALSE) {
  215. printf (_("Invalid host name/address\n\n"));
  216. print_usage ();
  217. exit (STATE_UNKNOWN);
  218. }
  219. server_name = strscpy (server_name, optarg);
  220. break;
  221. case 'c':
  222. get_threshold (optarg, rv);
  223. if (rv[RTA]) {
  224. crta = strtod (rv[RTA], NULL);
  225. rv[RTA] = NULL;
  226. }
  227. if (rv[PL]) {
  228. cpl = atoi (rv[PL]);
  229. rv[PL] = NULL;
  230. }
  231. break;
  232. case 'w':
  233. get_threshold (optarg, rv);
  234. if (rv[RTA]) {
  235. wrta = strtod (rv[RTA], NULL);
  236. rv[RTA] = NULL;
  237. }
  238. if (rv[PL]) {
  239. wpl = atoi (rv[PL]);
  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 (_("Host name 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 (_("\
  301. Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n\n\
  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. }