check_tcp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 (" |%s\n", perfdata ("time", microsec, "us",
  293. TRUE, warning_time*1000,
  294. TRUE, critical_time*1000,
  295. TRUE, 0,
  296. TRUE, socket_timeout*1000));
  297. return result;
  298. }
  299. /* process command-line arguments */
  300. int
  301. process_arguments (int argc, char **argv)
  302. {
  303. int c;
  304. int option = 0;
  305. static struct option longopts[] = {
  306. {"hostname", required_argument, 0, 'H'},
  307. {"critical-time", required_argument, 0, 'c'},
  308. {"warning-time", required_argument, 0, 'w'},
  309. {"critical-codes", required_argument, 0, 'C'},
  310. {"warning-codes", required_argument, 0, 'W'},
  311. {"timeout", required_argument, 0, 't'},
  312. {"protocol", required_argument, 0, 'P'},
  313. {"port", required_argument, 0, 'p'},
  314. {"send", required_argument, 0, 's'},
  315. {"expect", required_argument, 0, 'e'},
  316. {"maxbytes", required_argument, 0, 'm'},
  317. {"quit", required_argument, 0, 'q'},
  318. {"delay", required_argument, 0, 'd'},
  319. {"refuse", required_argument, 0, 'r'},
  320. {"use-ipv4", no_argument, 0, '4'},
  321. {"use-ipv6", no_argument, 0, '6'},
  322. {"verbose", no_argument, 0, 'v'},
  323. {"version", no_argument, 0, 'V'},
  324. {"help", no_argument, 0, 'h'},
  325. {0, 0, 0, 0}
  326. };
  327. if (argc < 2)
  328. usage ("No arguments found\n");
  329. /* backwards compatibility */
  330. for (c = 1; c < argc; c++) {
  331. if (strcmp ("-to", argv[c]) == 0)
  332. strcpy (argv[c], "-t");
  333. else if (strcmp ("-wt", argv[c]) == 0)
  334. strcpy (argv[c], "-w");
  335. else if (strcmp ("-ct", argv[c]) == 0)
  336. strcpy (argv[c], "-c");
  337. }
  338. if (!is_option (argv[1])) {
  339. server_address = argv[1];
  340. argv[1] = argv[0];
  341. argv = &argv[1];
  342. argc--;
  343. }
  344. while (1) {
  345. c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:",
  346. longopts, &option);
  347. if (c == -1 || c == EOF || c == 1)
  348. break;
  349. switch (c) {
  350. case '?': /* print short usage statement if args not parsable */
  351. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  352. print_usage ();
  353. exit (STATE_UNKNOWN);
  354. case 'h': /* help */
  355. print_help ();
  356. exit (STATE_OK);
  357. case 'V': /* version */
  358. print_revision (progname, "$Revision$");
  359. exit (STATE_OK);
  360. case 'v': /* verbose mode */
  361. verbose = TRUE;
  362. break;
  363. case '4':
  364. address_family = AF_INET;
  365. break;
  366. case '6':
  367. #ifdef USE_IPV6
  368. address_family = AF_INET6;
  369. #else
  370. usage (_("IPv6 support not available\n"));
  371. #endif
  372. break;
  373. case 'H': /* hostname */
  374. if (is_host (optarg) == FALSE)
  375. usage2 (_("invalid host name or address"), optarg);
  376. server_address = optarg;
  377. break;
  378. case 'c': /* critical */
  379. if (!is_intnonneg (optarg))
  380. usage (_("Critical threshold must be a nonnegative integer\n"));
  381. else
  382. critical_time = strtod (optarg, NULL);
  383. check_critical_time = TRUE;
  384. break;
  385. case 'w': /* warning */
  386. if (!is_intnonneg (optarg))
  387. usage (_("Warning threshold must be a nonnegative integer\n"));
  388. else
  389. warning_time = strtod (optarg, NULL);
  390. check_warning_time = TRUE;
  391. break;
  392. case 'C':
  393. crit_codes = realloc (crit_codes, ++crit_codes_count);
  394. crit_codes[crit_codes_count - 1] = optarg;
  395. break;
  396. case 'W':
  397. warn_codes = realloc (warn_codes, ++warn_codes_count);
  398. warn_codes[warn_codes_count - 1] = optarg;
  399. break;
  400. case 't': /* timeout */
  401. if (!is_intpos (optarg))
  402. usage (_("Timeout interval must be a positive integer\n"));
  403. else
  404. socket_timeout = atoi (optarg);
  405. break;
  406. case 'p': /* port */
  407. if (!is_intpos (optarg))
  408. usage (_("Server port must be a positive integer\n"));
  409. else
  410. server_port = atoi (optarg);
  411. break;
  412. case 's':
  413. server_send = optarg;
  414. break;
  415. case 'e': /* expect string (may be repeated) */
  416. EXPECT = NULL;
  417. if (server_expect_count == 0)
  418. server_expect = malloc (++server_expect_count);
  419. else
  420. server_expect = realloc (server_expect, ++server_expect_count);
  421. server_expect[server_expect_count - 1] = optarg;
  422. break;
  423. case 'm':
  424. if (!is_intpos (optarg))
  425. usage (_("Maxbytes must be a positive integer\n"));
  426. else
  427. maxbytes = atoi (optarg);
  428. case 'q':
  429. server_quit = optarg;
  430. break;
  431. case 'r':
  432. if (!strncmp(optarg,"ok",2))
  433. econn_refuse_state = STATE_OK;
  434. else if (!strncmp(optarg,"warn",4))
  435. econn_refuse_state = STATE_WARNING;
  436. else if (!strncmp(optarg,"crit",4))
  437. econn_refuse_state = STATE_CRITICAL;
  438. else
  439. usage (_("Refuse mut be one of ok, warn, crit\n"));
  440. break;
  441. case 'd':
  442. if (is_intpos (optarg))
  443. delay = atoi (optarg);
  444. else
  445. usage (_("Delay must be a positive integer\n"));
  446. break;
  447. case 'S':
  448. #ifndef HAVE_SSL
  449. die (STATE_UNKNOWN,
  450. _("SSL support not available. Install OpenSSL and recompile."));
  451. #endif
  452. use_ssl = TRUE;
  453. break;
  454. }
  455. }
  456. if (server_address == NULL)
  457. usage (_("You must provide a server address\n"));
  458. return OK;
  459. }
  460. #ifdef HAVE_SSL
  461. int
  462. connect_SSL (void)
  463. {
  464. SSL_METHOD *meth;
  465. /* Initialize SSL context */
  466. SSLeay_add_ssl_algorithms ();
  467. meth = SSLv2_client_method ();
  468. SSL_load_error_strings ();
  469. if ((ctx = SSL_CTX_new (meth)) == NULL)
  470. {
  471. printf (_("ERROR: Cannot create SSL context.\n"));
  472. return STATE_CRITICAL;
  473. }
  474. /* Initialize alarm signal handling */
  475. signal (SIGALRM, socket_timeout_alarm_handler);
  476. /* Set socket timeout */
  477. alarm (socket_timeout);
  478. /* Save start time */
  479. time (&start_time);
  480. /* Make TCP connection */
  481. if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
  482. {
  483. /* Do the SSL handshake */
  484. if ((ssl = SSL_new (ctx)) != NULL)
  485. {
  486. SSL_set_fd (ssl, sd);
  487. if (SSL_connect (ssl) != -1)
  488. return OK;
  489. ERR_print_errors_fp (stderr);
  490. }
  491. else
  492. {
  493. printf (_("ERROR: Cannot initiate SSL handshake.\n"));
  494. }
  495. SSL_free (ssl);
  496. }
  497. SSL_CTX_free (ctx);
  498. close (sd);
  499. return STATE_CRITICAL;
  500. }
  501. #endif
  502. int
  503. my_recv (void)
  504. {
  505. int i;
  506. #ifdef HAVE_SSL
  507. if (use_ssl) {
  508. i = SSL_read (ssl, buffer, MAXBUF - 1);
  509. }
  510. else {
  511. #endif
  512. i = read (sd, buffer, MAXBUF - 1);
  513. #ifdef HAVE_SSL
  514. }
  515. #endif
  516. return i;
  517. }
  518. void
  519. print_help (void)
  520. {
  521. print_revision (progname, revision);
  522. printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
  523. printf (_(COPYRIGHT), copyright, email);
  524. printf (_("This plugin tests %s connections with the specified host.\n\n"),
  525. SERVICE);
  526. print_usage ();
  527. printf (_(UT_HELP_VRSN));
  528. printf (_(UT_HOST_PORT), 'p', "none");
  529. printf (_(UT_IPv46));
  530. printf (_("\
  531. -s, --send=STRING\n\
  532. String to send to the server\n\
  533. -e, --expect=STRING\n\
  534. String to expect in server response\n\
  535. -q, --quit=STRING\n\
  536. String to send server to initiate a clean close of the connection\n"));
  537. printf (_("\
  538. -r, --refuse=ok|warn|crit\n\
  539. Accept tcp refusals with states ok, warn, crit (default: crit)\n\
  540. -m, --maxbytes=INTEGER\n\
  541. Close connection once more than this number of bytes are received\n\
  542. -d, --delay=INTEGER\n\
  543. Seconds to wait between sending string and polling for response\n"));
  544. printf (_(UT_WARN_CRIT));
  545. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  546. printf (_(UT_VERBOSE));
  547. printf (_(UT_SUPPORT));
  548. }
  549. void
  550. print_usage (void)
  551. {
  552. printf (_("\
  553. Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
  554. [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
  555. [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
  556. [-r <refuse state>] [-v] [-4|-6]\n"), progname);
  557. printf (" %s (-h|--help)\n", progname);
  558. printf (" %s (-V|--version)\n", progname);
  559. }