check_fping.c 9.7 KB

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