check_tcp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. /* watch for the expect string */
  221. #ifdef HAVE_SSL
  222. if (use_ssl && SSL_read (ssl, buffer, MAX_INPUT_BUFFER - 1)>=0)
  223. status = strscat(status,buffer);
  224. else
  225. #endif
  226. {
  227. if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) >= 0)
  228. status = strscat (status, buffer);
  229. }
  230. strip (status);
  231. /* return a CRITICAL status if we couldn't read any data */
  232. if (status == NULL)
  233. terminate (STATE_CRITICAL, "No data received from host\n");
  234. if (status && verbose)
  235. printf ("%s\n", status);
  236. if (server_expect_count > 0) {
  237. for (i = 0;; i++) {
  238. printf ("%d %d\n", i, server_expect_count);
  239. if (i >= server_expect_count)
  240. terminate (STATE_WARNING, "Invalid response from host\n");
  241. if (strstr (status, server_expect[i]))
  242. break;
  243. }
  244. }
  245. }
  246. if (server_quit)
  247. #ifdef HAVE_SSL
  248. if (use_ssl) {
  249. SSL_write (ssl, QUIT, strlen (QUIT));
  250. SSL_shutdown (ssl);
  251. SSL_free (ssl);
  252. SSL_CTX_free (ctx);
  253. }
  254. else
  255. #endif
  256. send (sd, server_quit, strlen (server_quit), 0);
  257. /* close the connection */
  258. close (sd);
  259. time (&end_time);
  260. if (check_critical_time == TRUE && (end_time - start_time) > critical_time)
  261. result = STATE_CRITICAL;
  262. else if (check_warning_time == TRUE
  263. && (end_time - start_time) > warning_time) result = STATE_WARNING;
  264. /* reset the alarm */
  265. alarm (0);
  266. printf
  267. ("%s %s - %d second response time on port %d",
  268. SERVICE,
  269. state_text (result), (int) (end_time - start_time), server_port);
  270. if (status)
  271. printf (" [%s]\n", status);
  272. else
  273. printf ("\n");
  274. return result;
  275. }
  276. /* process command-line arguments */
  277. int
  278. process_arguments (int argc, char **argv)
  279. {
  280. int c;
  281. #ifdef HAVE_GETOPT_H
  282. int option_index = 0;
  283. static struct option long_options[] = {
  284. {"hostname", required_argument, 0, 'H'},
  285. {"critical-time", required_argument, 0, 'c'},
  286. {"warning-time", required_argument, 0, 'w'},
  287. {"critical-codes", required_argument, 0, 'C'},
  288. {"warning-codes", required_argument, 0, 'W'},
  289. {"timeout", required_argument, 0, 't'},
  290. {"protocol", required_argument, 0, 'P'},
  291. {"port", required_argument, 0, 'p'},
  292. {"send", required_argument, 0, 's'},
  293. {"expect", required_argument, 0, 'e'},
  294. {"quit", required_argument, 0, 'q'},
  295. {"delay", required_argument, 0, 'd'},
  296. {"verbose", no_argument, 0, 'v'},
  297. {"version", no_argument, 0, 'V'},
  298. {"help", no_argument, 0, 'h'},
  299. {0, 0, 0, 0}
  300. };
  301. #endif
  302. if (argc < 2)
  303. usage ("No arguments found\n");
  304. /* backwards compatibility */
  305. for (c = 1; c < argc; c++) {
  306. if (strcmp ("-to", argv[c]) == 0)
  307. strcpy (argv[c], "-t");
  308. else if (strcmp ("-wt", argv[c]) == 0)
  309. strcpy (argv[c], "-w");
  310. else if (strcmp ("-ct", argv[c]) == 0)
  311. strcpy (argv[c], "-c");
  312. }
  313. if (!is_option (argv[1])) {
  314. server_address = argv[1];
  315. argv[1] = argv[0];
  316. argv = &argv[1];
  317. argc--;
  318. }
  319. while (1) {
  320. #ifdef HAVE_GETOPT_H
  321. c =
  322. getopt_long (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S", long_options,
  323. &option_index);
  324. #else
  325. c = getopt (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S");
  326. #endif
  327. if (c == -1 || c == EOF || c == 1)
  328. break;
  329. switch (c) {
  330. case '?': /* print short usage statement if args not parsable */
  331. printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
  332. print_usage ();
  333. exit (STATE_UNKNOWN);
  334. case 'h': /* help */
  335. print_help ();
  336. exit (STATE_OK);
  337. case 'V': /* version */
  338. print_revision (PROGNAME, "$Revision$");
  339. exit (STATE_OK);
  340. case 'v': /* verbose mode */
  341. verbose = TRUE;
  342. break;
  343. case 'H': /* hostname */
  344. if (is_host (optarg) == FALSE)
  345. usage ("Invalid host name/address\n");
  346. server_address = optarg;
  347. break;
  348. case 'c': /* critical */
  349. if (!is_intnonneg (optarg))
  350. usage ("Critical threshold must be a nonnegative integer\n");
  351. critical_time = atoi (optarg);
  352. check_critical_time = TRUE;
  353. break;
  354. case 'w': /* warning */
  355. if (!is_intnonneg (optarg))
  356. usage ("Warning threshold must be a nonnegative integer\n");
  357. warning_time = atoi (optarg);
  358. check_warning_time = TRUE;
  359. break;
  360. case 'C':
  361. crit_codes = realloc (crit_codes, ++crit_codes_count);
  362. crit_codes[crit_codes_count - 1] = optarg;
  363. break;
  364. case 'W':
  365. warn_codes = realloc (warn_codes, ++warn_codes_count);
  366. warn_codes[warn_codes_count - 1] = optarg;
  367. break;
  368. case 't': /* timeout */
  369. if (!is_intpos (optarg))
  370. usage ("Timeout interval must be a positive integer\n");
  371. socket_timeout = atoi (optarg);
  372. break;
  373. case 'p': /* port */
  374. if (!is_intpos (optarg))
  375. usage ("Server port must be a positive integer\n");
  376. server_port = atoi (optarg);
  377. break;
  378. case 's':
  379. server_send = optarg;
  380. break;
  381. case 'e':
  382. EXPECT = NULL;
  383. if (server_expect_count == 0)
  384. server_expect = malloc (++server_expect_count);
  385. else
  386. server_expect = realloc (server_expect, ++server_expect_count);
  387. server_expect[server_expect_count - 1] = optarg;
  388. break;
  389. case 'q':
  390. server_quit = optarg;
  391. break;
  392. case 'd':
  393. if (is_intpos (optarg))
  394. delay = atoi (optarg);
  395. else
  396. usage ("Delay must be a positive integer\n");
  397. break;
  398. case 'S':
  399. #ifndef HAVE_SSL
  400. terminate (STATE_UNKNOWN,
  401. "SSL support not available. Install OpenSSL and recompile.");
  402. #endif
  403. use_ssl = TRUE;
  404. break;
  405. }
  406. }
  407. if (server_address == NULL)
  408. usage ("You must provide a server address\n");
  409. return OK;
  410. }
  411. void
  412. print_usage (void)
  413. {
  414. printf
  415. ("Usage: %s -H host -p port [-w warn_time] [-c crit_time] [-s send]\n"
  416. " [-e expect] [-W wait] [-t to_sec] [-v]\n", PROGNAME);
  417. }
  418. void
  419. print_help (void)
  420. {
  421. print_revision (PROGNAME, "$Revision$");
  422. printf
  423. ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
  424. "This plugin tests %s connections with the specified host.\n\n",
  425. SERVICE);
  426. print_usage ();
  427. printf
  428. ("Options:\n"
  429. " -H, --hostname=ADDRESS\n"
  430. " Host name argument for servers using host headers (use numeric\n"
  431. " address if possible to bypass DNS lookup).\n"
  432. " -p, --port=INTEGER\n"
  433. " Port number\n"
  434. " -s, --send=STRING\n"
  435. " String to send to the server\n"
  436. " -e, --expect=STRING\n"
  437. " String to expect in server response"
  438. " -W, --wait=INTEGER\n"
  439. " Seconds to wait between sending string and polling for response\n"
  440. " -w, --warning=INTEGER\n"
  441. " Response time to result in warning status (seconds)\n"
  442. " -c, --critical=INTEGER\n"
  443. " Response time to result in critical status (seconds)\n"
  444. " -t, --timeout=INTEGER\n"
  445. " Seconds before connection times out (default: %d)\n"
  446. " -v"
  447. " Show details for command-line debugging (do not use with nagios server)\n"
  448. " -h, --help\n"
  449. " Print detailed help screen\n"
  450. " -V, --version\n"
  451. " Print version information\n", DEFAULT_SOCKET_TIMEOUT);
  452. }
  453. #ifdef HAVE_SSL
  454. int
  455. connect_SSL (void)
  456. {
  457. SSL_METHOD *meth;
  458. /* Initialize SSL context */
  459. SSLeay_add_ssl_algorithms ();
  460. meth = SSLv2_client_method ();
  461. SSL_load_error_strings ();
  462. if ((ctx = SSL_CTX_new (meth)) == NULL)
  463. {
  464. printf ("ERROR: Cannot create SSL context.\n");
  465. return STATE_CRITICAL;
  466. }
  467. /* Initialize alarm signal handling */
  468. signal (SIGALRM, socket_timeout_alarm_handler);
  469. /* Set socket timeout */
  470. alarm (socket_timeout);
  471. /* Save start time */
  472. time (&start_time);
  473. /* Make TCP connection */
  474. if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK)
  475. {
  476. /* Do the SSL handshake */
  477. if ((ssl = SSL_new (ctx)) != NULL)
  478. {
  479. SSL_set_fd (ssl, sd);
  480. if (SSL_connect (ssl) != -1)
  481. return OK;
  482. ERR_print_errors_fp (stderr);
  483. }
  484. else
  485. {
  486. printf ("ERROR: Cannot initiate SSL handshake.\n");
  487. }
  488. SSL_free (ssl);
  489. }
  490. SSL_CTX_free (ctx);
  491. close (sd);
  492. return STATE_CRITICAL;
  493. }
  494. #endif