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