check_fping.c 9.8 KB

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