Code

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