check_fping.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. usage2 (_("Unknown argument"), optarg);
  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. usage2 (_("Invalid hostname/address"), optarg);
  213. }
  214. server_name = strscpy (server_name, optarg);
  215. break;
  216. case 'c':
  217. get_threshold (optarg, rv);
  218. if (rv[RTA]) {
  219. crta = strtod (rv[RTA], NULL);
  220. crta_p = TRUE;
  221. rv[RTA] = NULL;
  222. }
  223. if (rv[PL]) {
  224. cpl = atoi (rv[PL]);
  225. cpl_p = TRUE;
  226. rv[PL] = NULL;
  227. }
  228. break;
  229. case 'w':
  230. get_threshold (optarg, rv);
  231. if (rv[RTA]) {
  232. wrta = strtod (rv[RTA], NULL);
  233. wrta_p = TRUE;
  234. rv[RTA] = NULL;
  235. }
  236. if (rv[PL]) {
  237. wpl = atoi (rv[PL]);
  238. wpl_p = TRUE;
  239. rv[PL] = NULL;
  240. }
  241. break;
  242. case 'b': /* bytes per packet */
  243. if (is_intpos (optarg))
  244. packet_size = atoi (optarg);
  245. else
  246. usage (_("Packet size must be a positive integer"));
  247. break;
  248. case 'n': /* number of packets */
  249. if (is_intpos (optarg))
  250. packet_count = atoi (optarg);
  251. else
  252. usage (_("Packet count must be a positive integer"));
  253. break;
  254. }
  255. }
  256. if (server_name == NULL)
  257. usage4 (_("Hostname was not supplied"));
  258. return OK;
  259. }
  260. int
  261. get_threshold (char *arg, char *rv[2])
  262. {
  263. char *arg1 = NULL;
  264. char *arg2 = NULL;
  265. arg1 = strscpy (arg1, arg);
  266. if (strpbrk (arg1, ",:"))
  267. arg2 = 1 + strpbrk (arg1, ",:");
  268. if (arg2) {
  269. arg1[strcspn (arg1, ",:")] = 0;
  270. if (strstr (arg1, "%") && strstr (arg2, "%"))
  271. die (STATE_UNKNOWN,
  272. _("%s: Only one threshold may be packet loss (%s)\n"), progname,
  273. arg);
  274. if (!strstr (arg1, "%") && !strstr (arg2, "%"))
  275. die (STATE_UNKNOWN,
  276. _("%s: Only one threshold must be packet loss (%s)\n"),
  277. progname, arg);
  278. }
  279. if (arg2 && strstr (arg2, "%")) {
  280. rv[PL] = arg2;
  281. rv[RTA] = arg1;
  282. }
  283. else if (arg2) {
  284. rv[PL] = arg1;
  285. rv[RTA] = arg2;
  286. }
  287. else if (strstr (arg1, "%")) {
  288. rv[PL] = arg1;
  289. }
  290. else {
  291. rv[RTA] = arg1;
  292. }
  293. return OK;
  294. }
  295. void
  296. print_help (void)
  297. {
  298. print_revision (progname, revision);
  299. printf ("Copyright (c) 1999 Didi Rieder <adrieder@sbox.tu-graz.ac.at>\n");
  300. printf (COPYRIGHT, copyright, email);
  301. printf (_("This plugin will use the fping command to ping the specified host for a fast check"));
  302. printf ("\n");
  303. printf (_("Note that it is necessary to set the suid flag on fping."));
  304. printf ("\n\n");
  305. print_usage ();
  306. printf (_(UT_HELP_VRSN));
  307. printf (" -H, --hostname=HOST");
  308. printf (_("name or IP Address of host to ping (IP Address bypasses name lookup, reducing system load)"));
  309. printf ("\n");
  310. printf ("-w, --warning=THRESHOLD");
  311. printf ("\n");
  312. printf (_("warning threshold pair"));
  313. printf ("\n");
  314. printf (" -c, --critical=THRESHOLD");
  315. printf ("\n");
  316. printf (_("critical threshold pair"));
  317. printf ("\n");
  318. printf (" -b, --bytes=INTEGER");
  319. printf (_("size of ICMP packet (default: %d)"),PACKET_SIZE);
  320. printf ("\n");
  321. printf (" -n, --number=INTEGER");
  322. printf ("\n");
  323. printf (_("number of ICMP packets to send (default: %d)"),PACKET_COUNT);
  324. printf ("\n");
  325. printf (_(UT_VERBOSE));
  326. printf ("\n");
  327. printf (_("THRESHOLD is <rta>,<pl>%% where <rta> is the round trip average travel time (ms)"));
  328. printf ("\n");
  329. printf (_("which triggers a WARNING or CRITICAL state, and <pl> is the percentage of"));
  330. printf ("\n");
  331. printf (_("packet loss to trigger an alarm state."));
  332. printf ("\n");
  333. printf (_(UT_SUPPORT));
  334. }
  335. void
  336. print_usage (void)
  337. {
  338. printf (_("Usage:"));
  339. printf (" %s <host_address>\n", progname);
  340. }