4
0

check_tcp.c 15 KB

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