check_fping.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. $Id$
  14. ******************************************************************************/
  15. const char *progname = "check_fping";
  16. const char *revision = "$Revision$";
  17. const char *copyright = "2000-2004";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #include "popen.h"
  21. #include "netutils.h"
  22. #include "utils.h"
  23. enum {
  24. PACKET_COUNT = 1,
  25. PACKET_SIZE = 56,
  26. PL = 0,
  27. RTA = 1
  28. };
  29. int textscan (char *buf);
  30. int process_arguments (int, char **);
  31. int get_threshold (char *arg, char *rv[2]);
  32. void print_help (void);
  33. void print_usage (void);
  34. char *server_name = NULL;
  35. int packet_size = PACKET_SIZE;
  36. int packet_count = PACKET_COUNT;
  37. int verbose = FALSE;
  38. int cpl;
  39. int wpl;
  40. double crta;
  41. double wrta;
  42. int cpl_p = FALSE;
  43. int wpl_p = FALSE;
  44. int crta_p = FALSE;
  45. int wrta_p = FALSE;
  46. int
  47. main (int argc, char **argv)
  48. {
  49. /* normaly should be int result = STATE_UNKNOWN; */
  50. int status = STATE_UNKNOWN;
  51. char *server = NULL;
  52. char *command_line = NULL;
  53. char *input_buffer = NULL;
  54. input_buffer = malloc (MAX_INPUT_BUFFER);
  55. setlocale (LC_ALL, "");
  56. bindtextdomain (PACKAGE, LOCALEDIR);
  57. textdomain (PACKAGE);
  58. if (process_arguments (argc, argv) == ERROR)
  59. usage4 (_("Could not parse arguments"));
  60. server = strscpy (server, server_name);
  61. /* compose the command */
  62. asprintf (&command_line, "%s -b %d -c %d %s", PATH_TO_FPING,
  63. packet_size, packet_count, server);
  64. if (verbose)
  65. printf ("%s\n", command_line);
  66. /* run the command */
  67. child_process = spopen (command_line);
  68. if (child_process == NULL) {
  69. printf (_("Could not open pipe: %s\n"), command_line);
  70. return STATE_UNKNOWN;
  71. }
  72. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  73. if (child_stderr == NULL) {
  74. printf (_("Could not open stderr for %s\n"), command_line);
  75. }
  76. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  77. if (verbose)
  78. printf ("%s", input_buffer);
  79. status = max_state (status, textscan (input_buffer));
  80. }
  81. /* If we get anything on STDERR, at least set warning */
  82. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  83. status = max_state (status, STATE_WARNING);
  84. if (verbose)
  85. printf ("%s", input_buffer);
  86. status = max_state (status, textscan (input_buffer));
  87. }
  88. (void) fclose (child_stderr);
  89. /* close the pipe */
  90. if (spclose (child_process))
  91. /* need to use max_state not max */
  92. status = max_state (status, STATE_WARNING);
  93. printf ("FPING %s - %s\n", state_text (status), server_name);
  94. return status;
  95. }
  96. int
  97. textscan (char *buf)
  98. {
  99. char *rtastr = NULL;
  100. char *losstr = NULL;
  101. double loss;
  102. double rta;
  103. int status = STATE_UNKNOWN;
  104. if (strstr (buf, "not found")) {
  105. die (STATE_CRITICAL, _("FPING UNKNOW - %s not found\n"), server_name);
  106. }
  107. else if (strstr (buf, "is unreachable") || strstr (buf, "Unreachable")) {
  108. die (STATE_CRITICAL, _("FPING CRITICAL - %s is unreachable\n"),
  109. "host");
  110. }
  111. else if (strstr (buf, "is down")) {
  112. die (STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name);
  113. }
  114. else if (strstr (buf, "is alive")) {
  115. status = STATE_OK;
  116. }
  117. else if (strstr (buf, "xmt/rcv/%loss") && strstr (buf, "min/avg/max")) {
  118. losstr = strstr (buf, "=");
  119. losstr = 1 + strstr (losstr, "/");
  120. losstr = 1 + strstr (losstr, "/");
  121. rtastr = strstr (buf, "min/avg/max");
  122. rtastr = strstr (rtastr, "=");
  123. rtastr = 1 + index (rtastr, '/');
  124. loss = strtod (losstr, NULL);
  125. rta = strtod (rtastr, NULL);
  126. if (cpl_p == TRUE && loss > cpl)
  127. status = STATE_CRITICAL;
  128. else if (crta_p == TRUE && rta > crta)
  129. status = STATE_CRITICAL;
  130. else if (wpl_p == TRUE && loss > wpl)
  131. status = STATE_WARNING;
  132. else if (wrta_p == TRUE && rta > wrta)
  133. status = STATE_WARNING;
  134. else
  135. status = STATE_OK;
  136. die (status,
  137. _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"),
  138. state_text (status), server_name, loss, rta,
  139. perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100),
  140. fperfdata ("rta", rta/1.0e3, "s", wrta_p, wrta/1.0e3, crta_p, crta/1.0e3, TRUE, 0, FALSE, 0));
  141. }
  142. else if(strstr (buf, "xmt/rcv/%loss") ) {
  143. /* no min/max/avg if host was unreachable in fping v2.2.b1 */
  144. losstr = strstr (buf, "=");
  145. losstr = 1 + strstr (losstr, "/");
  146. losstr = 1 + strstr (losstr, "/");
  147. loss = strtod (losstr, NULL);
  148. if (atoi(losstr) == 100)
  149. status = STATE_CRITICAL;
  150. else if (cpl_p == TRUE && loss > cpl)
  151. status = STATE_CRITICAL;
  152. else if (wpl_p == TRUE && loss > wpl)
  153. status = STATE_WARNING;
  154. else
  155. status = STATE_OK;
  156. /* loss=%.0f%%;%d;%d;0;100 */
  157. die (status, _("FPING %s - %s (loss=%.0f%% )|%s\n"),
  158. state_text (status), server_name, loss ,
  159. perfdata ("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, TRUE, 0, TRUE, 100));
  160. }
  161. else {
  162. status = max_state (status, STATE_WARNING);
  163. }
  164. return status;
  165. }
  166. /* process command-line arguments */
  167. int
  168. process_arguments (int argc, char **argv)
  169. {
  170. int c;
  171. char *rv[2];
  172. int option = 0;
  173. static struct option longopts[] = {
  174. {"hostname", required_argument, 0, 'H'},
  175. {"critical", required_argument, 0, 'c'},
  176. {"warning", required_argument, 0, 'w'},
  177. {"bytes", required_argument, 0, 'b'},
  178. {"number", required_argument, 0, 'n'},
  179. {"verbose", no_argument, 0, 'v'},
  180. {"version", no_argument, 0, 'V'},
  181. {"help", no_argument, 0, 'h'},
  182. {0, 0, 0, 0}
  183. };
  184. rv[PL] = NULL;
  185. rv[RTA] = NULL;
  186. if (argc < 2)
  187. return ERROR;
  188. if (!is_option (argv[1])) {
  189. server_name = argv[1];
  190. argv[1] = argv[0];
  191. argv = &argv[1];
  192. argc--;
  193. }
  194. while (1) {
  195. c = getopt_long (argc, argv, "+hVvH:c:w:b:n:", longopts, &option);
  196. if (c == -1 || c == EOF || c == 1)
  197. break;
  198. switch (c) {
  199. case '?': /* print short usage statement if args not parsable */
  200. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  201. print_usage ();
  202. exit (STATE_UNKNOWN);
  203. case 'h': /* help */
  204. print_help ();
  205. exit (STATE_OK);
  206. case 'V': /* version */
  207. print_revision (progname, revision);
  208. exit (STATE_OK);
  209. case 'v': /* verbose mode */
  210. verbose = TRUE;
  211. break;
  212. case 'H': /* hostname */
  213. if (is_host (optarg) == FALSE) {
  214. usage2 (_("Invalid hostname/address"), optarg);
  215. }
  216. server_name = strscpy (server_name, optarg);
  217. break;
  218. case 'c':
  219. get_threshold (optarg, rv);
  220. if (rv[RTA]) {
  221. crta = 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 = 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 (_("Hostname 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 to ping the specified host\n\
  305. for a fast check if the host is alive.\n\
  306. Note that it is 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. }