check_tcp.c 14 KB

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