check_tcp.c 15 KB

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