check_tcp.c 16 KB

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