check_tcp.c 14 KB

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