check_tcp.c 15 KB

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