check_tcp.c 16 KB

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