check_ftp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /******************************************************************************
  2. *
  3. * CHECK_FTP.C
  4. *
  5. * Program: FTP plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * $Id$
  10. *
  11. * Description:
  12. *
  13. * This plugin will attempt to open an FTP connection with the host.
  14. * Successul connects return STATE_OK, refusals and timeouts return
  15. * STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful
  16. * connects, but incorrect reponse messages from the host result in
  17. * STATE_WARNING return values.
  18. *
  19. * License Information:
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License as published by
  23. * the Free Software Foundation; either version 2 of the License, or
  24. * (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. *
  35. *****************************************************************************/
  36. #include "config.h"
  37. #include "common.h"
  38. #include "netutils.h"
  39. #include "utils.h"
  40. #define PROGNAME "check_ftp"
  41. #define FTP_PORT 21
  42. #define FTP_EXPECT "220"
  43. #define FTP_QUIT "QUIT\n"
  44. int process_arguments (int, char **);
  45. int call_getopt (int, char **);
  46. void print_usage (void);
  47. void print_help (void);
  48. time_t start_time, end_time;
  49. int server_port = FTP_PORT;
  50. char *server_address = NULL;
  51. char *server_expect = NULL;
  52. int warning_time = 0;
  53. int check_warning_time = FALSE;
  54. int critical_time = 0;
  55. int check_critical_time = FALSE;
  56. int verbose = FALSE;
  57. int
  58. main (int argc, char **argv)
  59. {
  60. int sd;
  61. int result;
  62. char buffer[MAX_INPUT_BUFFER];
  63. if (process_arguments (argc, argv) == ERROR)
  64. usage ("Could not parse arguments\n");
  65. /* initialize alarm signal handling */
  66. signal (SIGALRM, socket_timeout_alarm_handler);
  67. /* set socket timeout */
  68. alarm (socket_timeout);
  69. /* try to connect to the host at the given port number */
  70. time (&start_time);
  71. result = my_tcp_connect (server_address, server_port, &sd);
  72. /* we connected, so close connection before exiting */
  73. if (result == STATE_OK) {
  74. /* watch for the FTP connection string */
  75. result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0);
  76. /* strip the buffer of carriage returns */
  77. strip (buffer);
  78. /* return a WARNING status if we couldn't read any data */
  79. if (result == -1) {
  80. printf ("recv() failed\n");
  81. result = STATE_WARNING;
  82. }
  83. else {
  84. /* make sure we find the response we are looking for */
  85. if (!strstr (buffer, server_expect)) {
  86. if (server_port == FTP_PORT)
  87. printf ("Invalid FTP response received from host\n");
  88. else
  89. printf ("Invalid FTP response received from host on port %d\n",
  90. server_port);
  91. result = STATE_WARNING;
  92. }
  93. else {
  94. time (&end_time);
  95. result = STATE_OK;
  96. if (check_critical_time == TRUE
  97. && (end_time - start_time) > critical_time) result =
  98. STATE_CRITICAL;
  99. else if (check_warning_time == TRUE
  100. && (end_time - start_time) > warning_time) result =
  101. STATE_WARNING;
  102. if (verbose == TRUE)
  103. printf ("FTP %s - %d sec. response time, %s\n",
  104. (result == STATE_OK) ? "ok" : "problem",
  105. (int) (end_time - start_time), buffer);
  106. else
  107. printf ("FTP %s - %d second response time\n",
  108. (result == STATE_OK) ? "ok" : "problem",
  109. (int) (end_time - start_time));
  110. }
  111. }
  112. /* close the connection */
  113. send (sd, FTP_QUIT, strlen (FTP_QUIT), 0);
  114. close (sd);
  115. }
  116. /* reset the alarm */
  117. alarm (0);
  118. return result;
  119. }
  120. /* process command-line arguments */
  121. int
  122. process_arguments (int argc, char **argv)
  123. {
  124. int c;
  125. if (argc < 2)
  126. usage ("\n");
  127. for (c = 1; c < argc; c++) {
  128. if (strcmp ("-to", argv[c]) == 0)
  129. strcpy (argv[c], "-t");
  130. else if (strcmp ("-wt", argv[c]) == 0)
  131. strcpy (argv[c], "-w");
  132. else if (strcmp ("-ct", argv[c]) == 0)
  133. strcpy (argv[c], "-c");
  134. }
  135. c = 0;
  136. while ((c += call_getopt (argc - c, &argv[c])) < argc) {
  137. if (is_option (argv[c]))
  138. continue;
  139. if (server_address == NULL) {
  140. if (argc > c) {
  141. if (is_host (argv[c]) == FALSE)
  142. usage ("Invalid host name/address\n");
  143. server_address = argv[c];
  144. }
  145. else {
  146. usage ("Host name was not supplied\n");
  147. }
  148. }
  149. }
  150. if (server_expect == NULL)
  151. server_expect = strscpy (NULL, FTP_EXPECT);
  152. return OK;
  153. }
  154. int
  155. call_getopt (int argc, char **argv)
  156. {
  157. int c, i = 0;
  158. #ifdef HAVE_GETOPT_H
  159. int option_index = 0;
  160. static struct option long_options[] = {
  161. {"hostname", required_argument, 0, 'H'},
  162. {"expect", required_argument, 0, 'e'},
  163. {"critical", required_argument, 0, 'c'},
  164. {"warning", required_argument, 0, 'w'},
  165. {"timeout", required_argument, 0, 'w'},
  166. {"port", required_argument, 0, 'p'},
  167. {"verbose", no_argument, 0, 'v'},
  168. {"version", no_argument, 0, 'V'},
  169. {"help", no_argument, 0, 'h'},
  170. {0, 0, 0, 0}
  171. };
  172. #endif
  173. while (1) {
  174. #ifdef HAVE_GETOPT_H
  175. c =
  176. getopt_long (argc, argv, "+hVvH:e:c:w:t:p:", long_options,
  177. &option_index);
  178. #else
  179. c = getopt (argc, argv, "+hVvH:e:c:w:t:p:");
  180. #endif
  181. i++;
  182. if (c == -1 || c == EOF || c == 1)
  183. break;
  184. switch (c) {
  185. case 'H':
  186. case 'e':
  187. case 'c':
  188. case 'w':
  189. case 't':
  190. case 'p':
  191. i++;
  192. }
  193. switch (c) {
  194. case '?': /* print short usage statement if args not parsable */
  195. printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
  196. print_usage ();
  197. exit (STATE_UNKNOWN);
  198. case 'h': /* help */
  199. print_help ();
  200. exit (STATE_OK);
  201. case 'V': /* version */
  202. print_revision (my_basename (argv[0]), "$Revision$");
  203. exit (STATE_OK);
  204. case 'v': /* verbose mode */
  205. verbose = TRUE;
  206. break;
  207. case 'H': /* hostname */
  208. if (is_host (optarg) == FALSE)
  209. usage ("Invalid host name/address\n");
  210. server_address = optarg;
  211. break;
  212. case 'e': /* expect */
  213. server_expect = optarg;
  214. break;
  215. case 'c': /* critical */
  216. if (!is_intnonneg (optarg))
  217. usage ("Critical threshold must be a nonnegative integer\n");
  218. critical_time = atoi (optarg);
  219. check_critical_time = TRUE;
  220. break;
  221. case 'w': /* warning */
  222. if (!is_intnonneg (optarg))
  223. usage ("Warning threshold must be a nonnegative integer\n");
  224. warning_time = atoi (optarg);
  225. check_warning_time = TRUE;
  226. break;
  227. case 't': /* timeout */
  228. if (!is_intnonneg (optarg))
  229. usage ("Timeout interval must be a nonnegative integer\n");
  230. socket_timeout = atoi (optarg);
  231. break;
  232. case 'p': /* port */
  233. if (!is_intnonneg (optarg))
  234. usage ("Serevr port must be a nonnegative integer\n");
  235. server_port = atoi (optarg);
  236. break;
  237. }
  238. }
  239. return i;
  240. }
  241. void
  242. print_usage (void)
  243. {
  244. printf
  245. ("Usage: %s -H <host_address> [-e expect] [-p port] [-w warn_time]\n"
  246. " [-c crit_time] [-t to_sec] [-v]\n", PROGNAME);
  247. }
  248. void
  249. print_help (void)
  250. {
  251. print_revision (PROGNAME, "$Revision$");
  252. printf
  253. ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
  254. "This plugin tests an FTP connection with the specified host.\n\n");
  255. print_usage ();
  256. printf
  257. ("Options:\n"
  258. " -H, --hostname=ADDRESS\n"
  259. " Host name argument for servers using host headers (use numeric\n"
  260. " address if possible to bypass DNS lookup).\n"
  261. " -e, --expect=STRING\n"
  262. " String to expect in first line of server response (default: %s)\n"
  263. " -p, --port=INTEGER\n"
  264. " Port number (default: %d)\n"
  265. " -w, --warning=INTEGER\n"
  266. " Response time to result in warning status (seconds)\n"
  267. " -c, --critical=INTEGER\n"
  268. " Response time to result in critical status (seconds)\n"
  269. " -t, --timeout=INTEGER\n"
  270. " Seconds before connection times out (default: %d)\n"
  271. " -v"
  272. " Show details for command-line debugging (do not use with nagios server)\n"
  273. " -h, --help\n"
  274. " Print detailed help screen\n"
  275. " -V, --version\n"
  276. " Print version information\n",
  277. FTP_EXPECT, FTP_PORT, DEFAULT_SOCKET_TIMEOUT);
  278. }