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