check_tcp.c 15 KB

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