check_tcp.c 14 KB

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