check_tcp.c 15 KB

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