check_tcp.c 15 KB

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