check_tcp.c 16 KB

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