check_fping.c 11 KB

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