check_tcp.c 14 KB

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