4
0

check_tcp.c 14 KB

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