check_tcp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /******************************************************************************
  2. *
  3. * This file is part of the Nagios Plugins.
  4. *
  5. * Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>
  6. *
  7. * The Nagios Plugins are free software; you can redistribute them
  8. * and/or modify them under the terms of the GNU General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * $Id$
  22. *
  23. *****************************************************************************/
  24. #define REVISION "$Revision$"
  25. #define DESCRIPTION "Check a TCP port"
  26. #define AUTHOR "Ethan Galstad"
  27. #define EMAIL "nagios@nagios.org"
  28. #define COPYRIGHTDATE "2002"
  29. #include "config.h"
  30. #include "common.h"
  31. #include "netutils.h"
  32. #include "utils.h"
  33. #ifdef HAVE_SSL_H
  34. #include <rsa.h>
  35. #include <crypto.h>
  36. #include <x509.h>
  37. #include <pem.h>
  38. #include <ssl.h>
  39. #include <err.h>
  40. #endif
  41. #ifdef HAVE_OPENSSL_SSL_H
  42. #include <openssl/rsa.h>
  43. #include <openssl/crypto.h>
  44. #include <openssl/x509.h>
  45. #include <openssl/pem.h>
  46. #include <openssl/ssl.h>
  47. #include <openssl/err.h>
  48. #endif
  49. #ifdef HAVE_SSL
  50. SSL_CTX *ctx;
  51. SSL *ssl;
  52. int connect_SSL (void);
  53. #endif
  54. #define TCP_PROTOCOL 1
  55. #define UDP_PROTOCOL 2
  56. int process_arguments (int, char **);
  57. void print_usage (void);
  58. void print_help (void);
  59. char *progname = "check_tcp";
  60. char *SERVICE = NULL;
  61. char *SEND = NULL;
  62. char *EXPECT = NULL;
  63. char *QUIT = NULL;
  64. int PROTOCOL = 0;
  65. int PORT = 0;
  66. int server_port = 0;
  67. char *server_address = NULL;
  68. char *server_send = NULL;
  69. char *server_quit = NULL;
  70. char **server_expect = NULL;
  71. int server_expect_count = 0;
  72. char **warn_codes = NULL;
  73. int warn_codes_count = 0;
  74. char **crit_codes = NULL;
  75. int crit_codes_count = 0;
  76. int delay = 0;
  77. double warning_time = 0;
  78. int check_warning_time = FALSE;
  79. double critical_time = 0;
  80. int check_critical_time = FALSE;
  81. double elapsed_time = 0;
  82. int verbose = FALSE;
  83. int use_ssl = FALSE;
  84. int sd = 0;
  85. int
  86. main (int argc, char **argv)
  87. {
  88. int result;
  89. int i;
  90. char *buffer = "";
  91. char *status = "";
  92. struct timeval tv;
  93. if (strstr (argv[0], "check_udp")) {
  94. asprintf (&progname, "check_udp");
  95. asprintf (&SERVICE, "UDP");
  96. SEND = NULL;
  97. EXPECT = NULL;
  98. QUIT = NULL;
  99. PROTOCOL = UDP_PROTOCOL;
  100. PORT = 0;
  101. }
  102. else if (strstr (argv[0], "check_tcp")) {
  103. asprintf (&progname, "check_tcp");
  104. asprintf (&SERVICE, "TCP");
  105. SEND = NULL;
  106. EXPECT = NULL;
  107. QUIT = NULL;
  108. PROTOCOL = TCP_PROTOCOL;
  109. PORT = 0;
  110. }
  111. else if (strstr (argv[0], "check_ftp")) {
  112. asprintf (&progname, "check_ftp");
  113. asprintf (&SERVICE, "FTP");
  114. SEND = NULL;
  115. asprintf (&EXPECT, "220");
  116. asprintf (&QUIT, "QUIT\r\n");
  117. PROTOCOL = TCP_PROTOCOL;
  118. PORT = 21;
  119. }
  120. else if (strstr (argv[0], "check_smtp")) {
  121. asprintf (&progname, "check_smtp");
  122. asprintf (&SERVICE, "SMTP");
  123. SEND = NULL;
  124. asprintf (&EXPECT, "220");
  125. asprintf (&QUIT, "QUIT\r\n");
  126. PROTOCOL = TCP_PROTOCOL;
  127. PORT = 25;
  128. }
  129. else if (strstr (argv[0], "check_pop")) {
  130. asprintf (&progname, "check_pop");
  131. asprintf (&SERVICE, "POP");
  132. SEND = NULL;
  133. asprintf (&EXPECT, "+OK");
  134. asprintf (&QUIT, "QUIT\r\n");
  135. PROTOCOL = TCP_PROTOCOL;
  136. PORT = 110;
  137. }
  138. else if (strstr (argv[0], "check_imap")) {
  139. asprintf (&progname, "check_imap");
  140. asprintf (&SERVICE, "IMAP");
  141. SEND = NULL;
  142. asprintf (&EXPECT, "* OK");
  143. asprintf (&QUIT, "a1 LOGOUT\r\n");
  144. PROTOCOL = TCP_PROTOCOL;
  145. PORT = 143;
  146. }
  147. #ifdef HAVE_SSL
  148. else if (strstr(argv[0],"check_simap")) {
  149. asprintf (&progname, "check_simap");
  150. asprintf (&SERVICE, "SIMAP");
  151. SEND=NULL;
  152. asprintf (&EXPECT, "* OK");
  153. asprintf (&QUIT, "a1 LOGOUT\r\n");
  154. PROTOCOL=TCP_PROTOCOL;
  155. use_ssl=TRUE;
  156. PORT=993;
  157. }
  158. else if (strstr(argv[0],"check_spop")) {
  159. asprintf (&progname, "check_spop");
  160. asprintf (&SERVICE, "SPOP");
  161. SEND=NULL;
  162. asprintf (&EXPECT, "+OK");
  163. asprintf (&QUIT, "QUIT\r\n");
  164. PROTOCOL=TCP_PROTOCOL;
  165. use_ssl=TRUE;
  166. PORT=995;
  167. }
  168. #endif
  169. else if (strstr (argv[0], "check_nntp")) {
  170. asprintf (&progname, "check_nntp");
  171. asprintf (&SERVICE, "NNTP");
  172. SEND = NULL;
  173. EXPECT = NULL;
  174. server_expect = realloc (server_expect, ++server_expect_count);
  175. asprintf (&server_expect[server_expect_count - 1], "200");
  176. server_expect = realloc (server_expect, ++server_expect_count);
  177. asprintf (&server_expect[server_expect_count - 1], "201");
  178. asprintf (&QUIT, "QUIT\r\n");
  179. PROTOCOL = TCP_PROTOCOL;
  180. PORT = 119;
  181. }
  182. else {
  183. usage ("ERROR: Generic check_tcp called with unknown service\n");
  184. }
  185. asprintf (&server_address, "127.0.0.1");
  186. server_port = PORT;
  187. server_send = SEND;
  188. server_quit = QUIT;
  189. if (process_arguments (argc, argv) == ERROR)
  190. usage ("Could not parse arguments\n");
  191. /* use default expect if none listed in process_arguments() */
  192. if (EXPECT && server_expect_count == 0) {
  193. server_expect = malloc (++server_expect_count);
  194. server_expect[server_expect_count - 1] = EXPECT;
  195. }
  196. /* initialize alarm signal handling */
  197. signal (SIGALRM, socket_timeout_alarm_handler);
  198. /* set socket timeout */
  199. alarm (socket_timeout);
  200. /* try to connect to the host at the given port number */
  201. gettimeofday (&tv, NULL);
  202. #ifdef HAVE_SSL
  203. if (use_ssl)
  204. result = connect_SSL ();
  205. else
  206. #endif
  207. {
  208. if (PROTOCOL == UDP_PROTOCOL)
  209. result = my_udp_connect (server_address, server_port, &sd);
  210. else /* default is TCP */
  211. result = my_tcp_connect (server_address, server_port, &sd);
  212. }
  213. if (result == STATE_CRITICAL)
  214. return STATE_CRITICAL;
  215. if (server_send != NULL) { /* Something to send? */
  216. asprintf (&server_send, "%s\r\n", server_send);
  217. #ifdef HAVE_SSL
  218. if (use_ssl)
  219. SSL_write(ssl, server_send, strlen (server_send));
  220. else
  221. #endif
  222. send (sd, server_send, strlen (server_send), 0);
  223. }
  224. if (delay > 0) {
  225. tv.tv_sec += delay;
  226. sleep (delay);
  227. }
  228. if (server_send || server_expect_count > 0) {
  229. buffer = malloc (MAX_INPUT_BUFFER);
  230. /* watch for the expect string */
  231. #ifdef HAVE_SSL
  232. if (use_ssl && SSL_read (ssl, buffer, MAX_INPUT_BUFFER - 1) > 0)
  233. asprintf (&status, "%s%s", status, buffer);
  234. else
  235. #endif
  236. if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) > 0)
  237. asprintf (&status, "%s%s", status, buffer);
  238. /* return a CRITICAL status if we couldn't read any data */
  239. if (status == NULL)
  240. terminate (STATE_CRITICAL, "No data received from host\n");
  241. strip (status);
  242. if (status && verbose)
  243. printf ("%s\n", status);
  244. if (server_expect_count > 0) {
  245. for (i = 0;; i++) {
  246. if (verbose)
  247. printf ("%d %d\n", i, server_expect_count);
  248. if (i >= server_expect_count)
  249. terminate (STATE_WARNING, "Invalid response from host\n");
  250. if (strstr (status, server_expect[i]))
  251. break;
  252. }
  253. }
  254. }
  255. if (server_quit)
  256. #ifdef HAVE_SSL
  257. if (use_ssl) {
  258. SSL_write (ssl, QUIT, strlen (QUIT));
  259. SSL_shutdown (ssl);
  260. SSL_free (ssl);
  261. SSL_CTX_free (ctx);
  262. }
  263. else
  264. #endif
  265. send (sd, server_quit, strlen (server_quit), 0);
  266. /* close the connection */
  267. if (sd)
  268. close (sd);
  269. elapsed_time = delta_time (tv);
  270. if (check_critical_time == TRUE && elapsed_time > critical_time)
  271. result = STATE_CRITICAL;
  272. else if (check_warning_time == TRUE && elapsed_time > warning_time)
  273. result = STATE_WARNING;
  274. /* reset the alarm */
  275. alarm (0);
  276. printf
  277. ("%s %s - %7.3f second response time on port %d",
  278. SERVICE,
  279. state_text (result), elapsed_time, server_port);
  280. if (status && strlen(status) > 0)
  281. printf (" [%s]", status);
  282. printf ("|time=%7.3f\n", elapsed_time);
  283. return result;
  284. }
  285. /* process command-line arguments */
  286. int
  287. process_arguments (int argc, char **argv)
  288. {
  289. int c;
  290. #ifdef HAVE_GETOPT_H
  291. int option_index = 0;
  292. static struct option long_options[] = {
  293. {"hostname", required_argument, 0, 'H'},
  294. {"critical-time", required_argument, 0, 'c'},
  295. {"warning-time", required_argument, 0, 'w'},
  296. {"critical-codes", required_argument, 0, 'C'},
  297. {"warning-codes", required_argument, 0, 'W'},
  298. {"timeout", required_argument, 0, 't'},
  299. {"protocol", required_argument, 0, 'P'},
  300. {"port", required_argument, 0, 'p'},
  301. {"send", required_argument, 0, 's'},
  302. {"expect", required_argument, 0, 'e'},
  303. {"quit", required_argument, 0, 'q'},
  304. {"delay", required_argument, 0, 'd'},
  305. {"verbose", no_argument, 0, 'v'},
  306. {"version", no_argument, 0, 'V'},
  307. {"help", no_argument, 0, 'h'},
  308. {0, 0, 0, 0}
  309. };
  310. #endif
  311. if (argc < 2)
  312. usage ("No arguments found\n");
  313. /* backwards compatibility */
  314. for (c = 1; c < argc; c++) {
  315. if (strcmp ("-to", argv[c]) == 0)
  316. strcpy (argv[c], "-t");
  317. else if (strcmp ("-wt", argv[c]) == 0)
  318. strcpy (argv[c], "-w");
  319. else if (strcmp ("-ct", argv[c]) == 0)
  320. strcpy (argv[c], "-c");
  321. }
  322. if (!is_option (argv[1])) {
  323. server_address = argv[1];
  324. argv[1] = argv[0];
  325. argv = &argv[1];
  326. argc--;
  327. }
  328. while (1) {
  329. #ifdef HAVE_GETOPT_H
  330. c =
  331. getopt_long (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S", long_options,
  332. &option_index);
  333. #else
  334. c = getopt (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S");
  335. #endif
  336. if (c == -1 || c == EOF || c == 1)
  337. break;
  338. switch (c) {
  339. case '?': /* print short usage statement if args not parsable */
  340. printf ("%s: Unknown argument: %s\n\n", progname, optarg);
  341. print_usage ();
  342. exit (STATE_UNKNOWN);
  343. case 'h': /* help */
  344. print_help ();
  345. exit (STATE_OK);
  346. case 'V': /* version */
  347. print_revision (progname, "$Revision$");
  348. exit (STATE_OK);
  349. case 'v': /* verbose mode */
  350. verbose = TRUE;
  351. break;
  352. case 'H': /* hostname */
  353. if (is_host (optarg) == FALSE)
  354. usage2 ("invalid host name or address", optarg);
  355. server_address = optarg;
  356. break;
  357. case 'c': /* critical */
  358. if (!is_intnonneg (optarg))
  359. usage ("Critical threshold must be a nonnegative integer\n");
  360. critical_time = strtod (optarg, NULL);
  361. check_critical_time = TRUE;
  362. break;
  363. case 'w': /* warning */
  364. if (!is_intnonneg (optarg))
  365. usage ("Warning threshold must be a nonnegative integer\n");
  366. warning_time = strtod (optarg, NULL);
  367. check_warning_time = TRUE;
  368. break;
  369. case 'C':
  370. crit_codes = realloc (crit_codes, ++crit_codes_count);
  371. crit_codes[crit_codes_count - 1] = optarg;
  372. break;
  373. case 'W':
  374. warn_codes = realloc (warn_codes, ++warn_codes_count);
  375. warn_codes[warn_codes_count - 1] = optarg;
  376. break;
  377. case 't': /* timeout */
  378. if (!is_intpos (optarg))
  379. usage ("Timeout interval must be a positive integer\n");
  380. socket_timeout = atoi (optarg);
  381. break;
  382. case 'p': /* port */
  383. if (!is_intpos (optarg))
  384. usage ("Server port must be a positive integer\n");
  385. server_port = atoi (optarg);
  386. break;
  387. case 's':
  388. server_send = optarg;
  389. break;
  390. case 'e': /* expect string (may be repeated) */
  391. EXPECT = NULL;
  392. if (server_expect_count == 0)
  393. server_expect = malloc (++server_expect_count);
  394. else
  395. server_expect = realloc (server_expect, ++server_expect_count);
  396. server_expect[server_expect_count - 1] = optarg;
  397. break;
  398. case 'q':
  399. server_quit = optarg;
  400. break;
  401. case 'd':
  402. if (is_intpos (optarg))
  403. delay = atoi (optarg);
  404. else
  405. usage ("Delay must be a positive integer\n");
  406. break;
  407. case 'S':
  408. #ifndef HAVE_SSL
  409. terminate (STATE_UNKNOWN,
  410. "SSL support not available. Install OpenSSL and recompile.");
  411. #endif
  412. use_ssl = TRUE;
  413. break;
  414. }
  415. }
  416. if (server_address == NULL)
  417. usage ("You must provide a server address\n");
  418. return OK;
  419. }
  420. void
  421. print_usage (void)
  422. {
  423. printf
  424. ("Usage: %s -H host -p port [-w warn_time] [-c crit_time] [-s send]\n"
  425. " [-e expect] [-W wait] [-t to_sec] [-v]\n", progname);
  426. }
  427. void
  428. print_help (void)
  429. {
  430. print_revision (progname, "$Revision$");
  431. printf
  432. ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
  433. "This plugin tests %s connections with the specified host.\n\n",
  434. SERVICE);
  435. print_usage ();
  436. printf
  437. ("Options:\n"
  438. " -H, --hostname=ADDRESS\n"
  439. " Host name argument for servers using host headers (use numeric\n"
  440. " address if possible to bypass DNS lookup).\n"
  441. " -p, --port=INTEGER\n"
  442. " Port number\n"
  443. " -s, --send=STRING\n"
  444. " String to send to the server\n"
  445. " -e, --expect=STRING\n"
  446. " String to expect in server response"
  447. " -W, --wait=INTEGER\n"
  448. " Seconds to wait between sending string and polling for response\n"
  449. " -w, --warning=DOUBLE\n"
  450. " Response time to result in warning status (seconds)\n"
  451. " -c, --critical=DOUBLE\n"
  452. " Response time to result in critical status (seconds)\n"
  453. " -t, --timeout=INTEGER\n"
  454. " Seconds before connection times out (default: %d)\n"
  455. " -v"
  456. " Show details for command-line debugging (do not use with nagios server)\n"
  457. " -h, --help\n"
  458. " Print detailed help screen\n"
  459. " -V, --version\n"
  460. " Print version information\n", DEFAULT_SOCKET_TIMEOUT);
  461. }
  462. #ifdef HAVE_SSL
  463. int
  464. connect_SSL (void)
  465. {
  466. SSL_METHOD *meth;
  467. /* Initialize SSL context */
  468. SSLeay_add_ssl_algorithms ();
  469. meth = SSLv2_client_method ();
  470. SSL_load_error_strings ();
  471. if ((ctx = SSL_CTX_new (meth)) == NULL)
  472. {
  473. printf ("ERROR: Cannot create SSL context.\n");
  474. return STATE_CRITICAL;
  475. }
  476. /* Initialize alarm signal handling */
  477. signal (SIGALRM, socket_timeout_alarm_handler);
  478. /* Set socket timeout */
  479. alarm (socket_timeout);
  480. /* Save start time */
  481. time (&start_time);
  482. /* Make TCP connection */
  483. if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK)
  484. {
  485. /* Do the SSL handshake */
  486. if ((ssl = SSL_new (ctx)) != NULL)
  487. {
  488. SSL_set_fd (ssl, sd);
  489. if (SSL_connect (ssl) != -1)
  490. return OK;
  491. ERR_print_errors_fp (stderr);
  492. }
  493. else
  494. {
  495. printf ("ERROR: Cannot initiate SSL handshake.\n");
  496. }
  497. SSL_free (ssl);
  498. }
  499. SSL_CTX_free (ctx);
  500. close (sd);
  501. return STATE_CRITICAL;
  502. }
  503. #endif