check_fping.c 11 KB

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