check_tcp.c 15 KB

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