Code

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