check_tcp.c 14 KB

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