1
/******************************************************************************
3
* This file is part of the Nagios Plugins.
5
* Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>
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.
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.
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.
21
* $Id: check_tcp.c,v 1.13.2.3 2003/06/02 14:46:48 kdebisschop Exp $
23
*****************************************************************************/
25
#define REVISION "$Revision: 1.13.2.3 $"
26
#define DESCRIPTION "Check a TCP port"
27
#define AUTHOR "Ethan Galstad"
28
#define EMAIL "nagios@nagios.org"
29
#define COPYRIGHTDATE "2002"
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>
57
int connect_SSL (void);
66
int process_arguments (int, char **);
67
void print_usage (void);
68
void print_help (void);
71
char *progname = "check_tcp";
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;
86
char **warn_codes = NULL;
87
int warn_codes_count = 0;
88
char **crit_codes = NULL;
89
int crit_codes_count = 0;
91
double warning_time = 0;
92
int check_warning_time = FALSE;
93
double critical_time = 0;
94
int check_critical_time = FALSE;
95
double elapsed_time = 0;
102
main (int argc, char **argv)
109
if (strstr (argv[0], "check_udp")) {
110
asprintf (&progname, "check_udp");
111
asprintf (&SERVICE, "UDP");
115
PROTOCOL = UDP_PROTOCOL;
118
else if (strstr (argv[0], "check_tcp")) {
119
asprintf (&progname, "check_tcp");
120
asprintf (&SERVICE, "TCP");
124
PROTOCOL = TCP_PROTOCOL;
127
else if (strstr (argv[0], "check_ftp")) {
128
asprintf (&progname, "check_ftp");
129
asprintf (&SERVICE, "FTP");
131
asprintf (&EXPECT, "220");
132
asprintf (&QUIT, "QUIT\r\n");
133
PROTOCOL = TCP_PROTOCOL;
136
else if (strstr (argv[0], "check_smtp")) {
137
asprintf (&progname, "check_smtp");
138
asprintf (&SERVICE, "SMTP");
140
asprintf (&EXPECT, "220");
141
asprintf (&QUIT, "QUIT\r\n");
142
PROTOCOL = TCP_PROTOCOL;
145
else if (strstr (argv[0], "check_pop")) {
146
asprintf (&progname, "check_pop");
147
asprintf (&SERVICE, "POP");
149
asprintf (&EXPECT, "+OK");
150
asprintf (&QUIT, "QUIT\r\n");
151
PROTOCOL = TCP_PROTOCOL;
154
else if (strstr (argv[0], "check_imap")) {
155
asprintf (&progname, "check_imap");
156
asprintf (&SERVICE, "IMAP");
158
asprintf (&EXPECT, "* OK");
159
asprintf (&QUIT, "a1 LOGOUT\r\n");
160
PROTOCOL = TCP_PROTOCOL;
164
else if (strstr(argv[0],"check_simap")) {
165
asprintf (&progname, "check_simap");
166
asprintf (&SERVICE, "SIMAP");
168
asprintf (&EXPECT, "* OK");
169
asprintf (&QUIT, "a1 LOGOUT\r\n");
170
PROTOCOL=TCP_PROTOCOL;
174
else if (strstr(argv[0],"check_spop")) {
175
asprintf (&progname, "check_spop");
176
asprintf (&SERVICE, "SPOP");
178
asprintf (&EXPECT, "+OK");
179
asprintf (&QUIT, "QUIT\r\n");
180
PROTOCOL=TCP_PROTOCOL;
185
else if (strstr (argv[0], "check_nntp")) {
186
asprintf (&progname, "check_nntp");
187
asprintf (&SERVICE, "NNTP");
190
server_expect = realloc (server_expect, ++server_expect_count);
191
asprintf (&server_expect[server_expect_count - 1], "200");
192
server_expect = realloc (server_expect, ++server_expect_count);
193
asprintf (&server_expect[server_expect_count - 1], "201");
194
asprintf (&QUIT, "QUIT\r\n");
195
PROTOCOL = TCP_PROTOCOL;
199
usage ("ERROR: Generic check_tcp called with unknown service\n");
202
asprintf (&server_address, "127.0.0.1");
207
if (process_arguments (argc, argv) == ERROR)
208
usage ("Could not parse arguments\n");
210
/* use default expect if none listed in process_arguments() */
211
if (EXPECT && server_expect_count == 0) {
212
server_expect = malloc (++server_expect_count);
213
server_expect[server_expect_count - 1] = EXPECT;
216
/* initialize alarm signal handling */
217
signal (SIGALRM, socket_timeout_alarm_handler);
219
/* set socket timeout */
220
alarm (socket_timeout);
222
/* try to connect to the host at the given port number */
223
gettimeofday (&tv, NULL);
226
result = connect_SSL ();
230
if (PROTOCOL == UDP_PROTOCOL)
231
result = my_udp_connect (server_address, server_port, &sd);
232
else /* default is TCP */
233
result = my_tcp_connect (server_address, server_port, &sd);
236
if (result == STATE_CRITICAL)
237
return STATE_CRITICAL;
239
if (server_send != NULL) { /* Something to send? */
240
asprintf (&server_send, "%s\r\n", server_send);
243
SSL_write(ssl, server_send, strlen (server_send));
246
send (sd, server_send, strlen (server_send), 0);
254
if (server_send || server_expect_count > 0) {
256
buffer = malloc (MAXBUF);
257
memset (buffer, '\0', MAXBUF);
258
/* watch for the expect string */
259
while ((i = my_recv ()) > 0) {
261
asprintf (&status, "%s%s", status, buffer);
262
if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
264
if (maxbytes>0 && strlen(status)>=maxbytes)
268
/* return a CRITICAL status if we couldn't read any data */
270
terminate (STATE_CRITICAL, "No data received from host\n");
274
if (status && verbose)
275
printf ("%s\n", status);
277
if (server_expect_count > 0) {
280
printf ("%d %d\n", i, server_expect_count);
281
if (i >= server_expect_count)
282
terminate (STATE_WARNING, "Invalid response from host\n");
283
if (strstr (status, server_expect[i]))
289
if (server_quit != NULL)
292
SSL_write (ssl, QUIT, strlen (QUIT));
299
send (sd, server_quit, strlen (server_quit), 0);
301
/* close the connection */
305
elapsed_time = delta_time (tv);
307
if (check_critical_time == TRUE && elapsed_time > critical_time)
308
result = STATE_CRITICAL;
309
else if (check_warning_time == TRUE && elapsed_time > warning_time)
310
result = STATE_WARNING;
312
/* reset the alarm */
316
("%s %s - %7.3f second response time on port %d",
318
state_text (result), elapsed_time, server_port);
320
if (status && strlen(status) > 0)
321
printf (" [%s]", status);
323
printf ("|time=%7.3f\n", elapsed_time);
334
/* process command-line arguments */
336
process_arguments (int argc, char **argv)
341
int option_index = 0;
342
static struct option long_options[] = {
343
{"hostname", required_argument, 0, 'H'},
344
{"critical-time", required_argument, 0, 'c'},
345
{"warning-time", required_argument, 0, 'w'},
346
{"critical-codes", required_argument, 0, 'C'},
347
{"warning-codes", required_argument, 0, 'W'},
348
{"timeout", required_argument, 0, 't'},
349
{"protocol", required_argument, 0, 'P'},
350
{"port", required_argument, 0, 'p'},
351
{"send", required_argument, 0, 's'},
352
{"expect", required_argument, 0, 'e'},
353
{"maxbytes", required_argument, 0, 'm'},
354
{"quit", required_argument, 0, 'q'},
355
{"delay", required_argument, 0, 'd'},
356
{"verbose", no_argument, 0, 'v'},
357
{"version", no_argument, 0, 'V'},
358
{"help", no_argument, 0, 'h'},
364
usage ("No arguments found\n");
366
/* backwards compatibility */
367
for (c = 1; c < argc; c++) {
368
if (strcmp ("-to", argv[c]) == 0)
369
strcpy (argv[c], "-t");
370
else if (strcmp ("-wt", argv[c]) == 0)
371
strcpy (argv[c], "-w");
372
else if (strcmp ("-ct", argv[c]) == 0)
373
strcpy (argv[c], "-c");
376
if (!is_option (argv[1])) {
377
server_address = argv[1];
386
getopt_long (argc, argv, "+hVvH:s:e:q:m:c:w:t:p:C:W:d:S", long_options,
389
c = getopt (argc, argv, "+hVvH:s:e:q:m:c:w:t:p:C:W:d:S");
392
if (c == -1 || c == EOF || c == 1)
396
case '?': /* print short usage statement if args not parsable */
397
printf ("%s: Unknown argument: %s\n\n", progname, optarg);
399
exit (STATE_UNKNOWN);
403
case 'V': /* version */
404
print_revision (progname, "$Revision: 1.13.2.3 $");
406
case 'v': /* verbose mode */
409
case 'H': /* hostname */
410
if (is_host (optarg) == FALSE)
411
usage2 ("invalid host name or address", optarg);
412
server_address = optarg;
414
case 'c': /* critical */
415
if (!is_intnonneg (optarg))
416
usage ("Critical threshold must be a nonnegative integer\n");
417
critical_time = strtod (optarg, NULL);
418
check_critical_time = TRUE;
420
case 'w': /* warning */
421
if (!is_intnonneg (optarg))
422
usage ("Warning threshold must be a nonnegative integer\n");
423
warning_time = strtod (optarg, NULL);
424
check_warning_time = TRUE;
427
crit_codes = realloc (crit_codes, ++crit_codes_count);
428
crit_codes[crit_codes_count - 1] = optarg;
431
warn_codes = realloc (warn_codes, ++warn_codes_count);
432
warn_codes[warn_codes_count - 1] = optarg;
434
case 't': /* timeout */
435
if (!is_intpos (optarg))
436
usage ("Timeout interval must be a positive integer\n");
437
socket_timeout = atoi (optarg);
440
if (!is_intpos (optarg))
441
usage ("Server port must be a positive integer\n");
442
server_port = atoi (optarg);
445
server_send = optarg;
447
case 'e': /* expect string (may be repeated) */
449
if (server_expect_count == 0)
450
server_expect = malloc (++server_expect_count);
452
server_expect = realloc (server_expect, ++server_expect_count);
453
server_expect[server_expect_count - 1] = optarg;
456
if (!is_intpos (optarg))
457
usage ("Maxbytes must be a positive integer\n");
458
maxbytes = atoi (optarg);
460
server_quit = optarg;
463
if (is_intpos (optarg))
464
delay = atoi (optarg);
466
usage ("Delay must be a positive integer\n");
470
terminate (STATE_UNKNOWN,
471
"SSL support not available. Install OpenSSL and recompile.");
478
if (server_address == NULL)
479
usage ("You must provide a server address\n");
492
("Usage: %s -H host -p port [-w warn_time] [-c crit_time] [-s send_string]\n"
493
" [-e expect_string] [-q quit_string] [-m maxbytes] [-d delay]\n"
494
" [-t to_sec] [-v]\n", progname);
504
print_revision (progname, "$Revision: 1.13.2.3 $");
506
("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
507
"This plugin tests %s connections with the specified host.\n\n",
512
" -H, --hostname=ADDRESS\n"
513
" Host name argument for servers using host headers (use numeric\n"
514
" address if possible to bypass DNS lookup).\n"
515
" -p, --port=INTEGER\n"
517
" -s, --send=STRING\n"
518
" String to send to the server\n"
519
" -e, --expect=STRING\n"
520
" String to expect in server response\n"
521
" -q, --quit=STRING\n"
522
" String to send server to initiate a clean close of the connection\n"
523
" -m, --maxbytes=INTEGER\n"
524
" Close connection once more than this number of bytes are received\n"
525
" -d, --delay=INTEGER\n"
526
" Seconds to wait between sending string and polling for response\n"
527
" -w, --warning=DOUBLE\n"
528
" Response time to result in warning status (seconds)\n"
529
" -c, --critical=DOUBLE\n"
530
" Response time to result in critical status (seconds)\n"
531
" -t, --timeout=INTEGER\n"
532
" Seconds before connection times out (default: %d)\n"
534
" Show details for command-line debugging (do not use with nagios server)\n"
536
" Print detailed help screen\n"
538
" Print version information\n", DEFAULT_SOCKET_TIMEOUT);
542
" -W, --warning-codes=STRING\n"
544
" -C, --critical-code=STRING\n"
554
/* Initialize SSL context */
555
SSLeay_add_ssl_algorithms ();
556
meth = SSLv2_client_method ();
557
SSL_load_error_strings ();
558
if ((ctx = SSL_CTX_new (meth)) == NULL)
560
printf ("ERROR: Cannot create SSL context.\n");
561
return STATE_CRITICAL;
564
/* Initialize alarm signal handling */
565
signal (SIGALRM, socket_timeout_alarm_handler);
567
/* Set socket timeout */
568
alarm (socket_timeout);
570
/* Save start time */
573
/* Make TCP connection */
574
if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK)
576
/* Do the SSL handshake */
577
if ((ssl = SSL_new (ctx)) != NULL)
579
SSL_set_fd (ssl, sd);
580
if (SSL_connect (ssl) != -1)
582
ERR_print_errors_fp (stderr);
586
printf ("ERROR: Cannot initiate SSL handshake.\n");
594
return STATE_CRITICAL;
607
i = SSL_read (ssl, buffer, MAXBUF - 1);
611
i = read (sd, buffer, MAXBUF - 1);