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