check_tcp.c 14 KB

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