diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c
index 158475e9df692fab9892f227d62ff110baa12590..f4fe5f40cf84da5e7e814b2b77a2ebc7677cf33b 100644 (file)
--- a/plugins/check_tcp.c
+++ b/plugins/check_tcp.c
-/******************************************************************************
- *
- * This file is part of the Nagios Plugins.
- *
- * Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>
- *
- * The Nagios Plugins are free software; you can redistribute them
- * and/or modify them under the terms of the GNU General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id$
- *
- *****************************************************************************/
-
-#define REVISION "$Revision$"
-#define DESCRIPTION "Check a TCP port"
-#define AUTHOR "Ethan Galstad"
-#define EMAIL "nagios@nagios.org"
-#define COPYRIGHTDATE "2002"
+/*****************************************************************************
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation; either version 2 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*
+*****************************************************************************/
+
+/* progname changes depending on symlink called */
+char *progname = "check_tcp";
+const char *revision = "$Revision$";
+const char *copyright = "2002-2003";
+const char *authors = "Nagios Plugin Development Team";
+const char *email = "nagiosplug-devel@lists.sourceforge.net";
+
+const char *summary = "\
+This plugin tests %s connections with the specified host.\n";
+
+const char *option_summary = "\
+-H host -p port [-w warn_time] [-c crit_time] [-s send_string]\n\
+ [-e expect_string] [-q quit_string] [-m maxbytes] [-d delay]\n\
+ [-t to_sec] [-r refuse_state] [-v]\n";
+
+const char *options = "\
+ -H, --hostname=ADDRESS\n\
+ Host name argument for servers using host headers (use numeric\n\
+ address if possible to bypass DNS lookup).\n\
+ -p, --port=INTEGER\n\
+ Port number\n\
+ -s, --send=STRING\n\
+ String to send to the server\n\
+ -e, --expect=STRING\n\
+ String to expect in server response\n\
+ -q, --quit=STRING\n\
+ String to send server to initiate a clean close of the connection\n\
+ -m, --maxbytes=INTEGER\n\
+ Close connection once more than this number of bytes are received\n\
+ -d, --delay=INTEGER\n\
+ Seconds to wait between sending string and polling for response\n\
+ -w, --warning=DOUBLE\n\
+ Response time to result in warning status (seconds)\n\
+ -c, --critical=DOUBLE\n\
+ Response time to result in critical status (seconds)\n\
+ -t, --timeout=INTEGER\n\
+ Seconds before connection times out (default: %d)\n\
+ -r, --refuse=ok|warn|crit\n\
+ Accept tcp refusals with states ok, warn, crit (default: crit)\n\
+ -v, --verbose\n\
+ Show details for command-line debugging (Nagios may truncate output)\n";
+
+const char *standard_options = "\
+ -h, --help\n\
+ Print detailed help screen\n\
+ -V, --version\n\
+ Print version information\n\n";
#include "config.h"
#include "common.h"
#include "utils.h"
#ifdef HAVE_SSL_H
-#include <rsa.h>
-#include <crypto.h>
-#include <x509.h>
-#include <pem.h>
-#include <ssl.h>
-#include <err.h>
-#endif
-
-#ifdef HAVE_OPENSSL_SSL_H
-#include <openssl/rsa.h>
-#include <openssl/crypto.h>
-#include <openssl/x509.h>
-#include <openssl/pem.h>
-#include <openssl/ssl.h>
-#include <openssl/err.h>
+# include <rsa.h>
+# include <crypto.h>
+# include <x509.h>
+# include <pem.h>
+# include <ssl.h>
+# include <err.h>
+#else
+# ifdef HAVE_OPENSSL_SSL_H
+# include <openssl/rsa.h>
+# include <openssl/crypto.h>
+# include <openssl/x509.h>
+# include <openssl/pem.h>
+# include <openssl/ssl.h>
+# include <openssl/err.h>
+# endif
#endif
#ifdef HAVE_SSL
void print_help (void);
int my_recv (void);
-char *progname = "check_tcp";
char *SERVICE = NULL;
char *SEND = NULL;
char *EXPECT = NULL;
char *server_quit = NULL;
char **server_expect = NULL;
int server_expect_count = 0;
+int maxbytes = 0;
char **warn_codes = NULL;
int warn_codes_count = 0;
char **crit_codes = NULL;
struct timeval tv;
if (strstr (argv[0], "check_udp")) {
- asprintf (&progname, "check_udp");
- asprintf (&SERVICE, "UDP");
+ progname = strdup ("check_udp");
+ SERVICE = strdup ("UDP");
SEND = NULL;
EXPECT = NULL;
QUIT = NULL;
PORT = 0;
}
else if (strstr (argv[0], "check_tcp")) {
- asprintf (&progname, "check_tcp");
- asprintf (&SERVICE, "TCP");
+ progname = strdup ("check_tcp");
+ SERVICE = strdup ("TCP");
SEND = NULL;
EXPECT = NULL;
QUIT = NULL;
PORT = 0;
}
else if (strstr (argv[0], "check_ftp")) {
- asprintf (&progname, "check_ftp");
- asprintf (&SERVICE, "FTP");
+ progname = strdup ("check_ftp");
+ SERVICE = strdup ("FTP");
SEND = NULL;
- asprintf (&EXPECT, "220");
- asprintf (&QUIT, "QUIT\r\n");
+ EXPECT = strdup ("220");
+ QUIT = strdup ("QUIT\r\n");
PROTOCOL = TCP_PROTOCOL;
PORT = 21;
}
else if (strstr (argv[0], "check_smtp")) {
- asprintf (&progname, "check_smtp");
- asprintf (&SERVICE, "SMTP");
+ progname = strdup ("check_smtp");
+ SERVICE = strdup ("SMTP");
SEND = NULL;
- asprintf (&EXPECT, "220");
- asprintf (&QUIT, "QUIT\r\n");
+ EXPECT = strdup ("220");
+ QUIT = strdup ("QUIT\r\n");
PROTOCOL = TCP_PROTOCOL;
PORT = 25;
}
else if (strstr (argv[0], "check_pop")) {
- asprintf (&progname, "check_pop");
- asprintf (&SERVICE, "POP");
+ progname = strdup ("check_pop");
+ SERVICE = strdup ("POP");
SEND = NULL;
- asprintf (&EXPECT, "+OK");
- asprintf (&QUIT, "QUIT\r\n");
+ EXPECT = strdup ("+OK");
+ QUIT = strdup ("QUIT\r\n");
PROTOCOL = TCP_PROTOCOL;
PORT = 110;
}
else if (strstr (argv[0], "check_imap")) {
- asprintf (&progname, "check_imap");
- asprintf (&SERVICE, "IMAP");
+ progname = strdup ("check_imap");
+ SERVICE = strdup ("IMAP");
SEND = NULL;
- asprintf (&EXPECT, "* OK");
- asprintf (&QUIT, "a1 LOGOUT\r\n");
+ EXPECT = strdup ("* OK");
+ QUIT = strdup ("a1 LOGOUT\r\n");
PROTOCOL = TCP_PROTOCOL;
PORT = 143;
}
#ifdef HAVE_SSL
else if (strstr(argv[0],"check_simap")) {
- asprintf (&progname, "check_simap");
- asprintf (&SERVICE, "SIMAP");
+ progname = strdup ("check_simap");
+ SERVICE = strdup ("SIMAP");
SEND=NULL;
- asprintf (&EXPECT, "* OK");
- asprintf (&QUIT, "a1 LOGOUT\r\n");
+ EXPECT = strdup ("* OK");
+ QUIT = strdup ("a1 LOGOUT\r\n");
PROTOCOL=TCP_PROTOCOL;
use_ssl=TRUE;
PORT=993;
}
else if (strstr(argv[0],"check_spop")) {
- asprintf (&progname, "check_spop");
- asprintf (&SERVICE, "SPOP");
+ progname = strdup ("check_spop");
+ SERVICE = strdup ("SPOP");
SEND=NULL;
- asprintf (&EXPECT, "+OK");
- asprintf (&QUIT, "QUIT\r\n");
+ EXPECT = strdup ("+OK");
+ QUIT = strdup ("QUIT\r\n");
PROTOCOL=TCP_PROTOCOL;
use_ssl=TRUE;
PORT=995;
}
#endif
else if (strstr (argv[0], "check_nntp")) {
- asprintf (&progname, "check_nntp");
- asprintf (&SERVICE, "NNTP");
+ progname = strdup ("check_nntp");
+ SERVICE = strdup ("NNTP");
SEND = NULL;
EXPECT = NULL;
server_expect = realloc (server_expect, ++server_expect_count);
usage ("ERROR: Generic check_tcp called with unknown service\n");
}
- asprintf (&server_address, "127.0.0.1");
+ server_address = strdup ("127.0.0.1");
server_port = PORT;
server_send = SEND;
server_quit = QUIT;
asprintf (&status, "%s%s", status, buffer);
if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
break;
+ if (maxbytes>0 && strlen(status)>=maxbytes)
+ break;
}
/* return a CRITICAL status if we couldn't read any data */
}
}
- if (server_quit != NULL)
+ if (server_quit != NULL) {
#ifdef HAVE_SSL
if (use_ssl) {
- SSL_write (ssl, QUIT, strlen (QUIT));
+ SSL_write (ssl, server_quit, strlen (server_quit));
SSL_shutdown (ssl);
SSL_free (ssl);
SSL_CTX_free (ctx);
}
- else
+ else {
#endif
- send (sd, server_quit, strlen (server_quit), 0);
+ send (sd, server_quit, strlen (server_quit), 0);
+#ifdef HAVE_SSL
+ }
+#endif
+ }
/* close the connection */
if (sd)
alarm (0);
printf
- ("%s %s - %7.3f second response time on port %d",
+ ("%s %s%s - %.3f second response time on port %d",
SERVICE,
- state_text (result), elapsed_time, server_port);
+ state_text (result),
+ (was_refused) ? " (refused)" : "",
+ elapsed_time, server_port);
if (status && strlen(status) > 0)
printf (" [%s]", status);
- printf ("|time=%7.3f\n", elapsed_time);
+ printf ("|time=%.3f\n", elapsed_time);
return result;
}
-
-
-
-
+\f
{"port", required_argument, 0, 'p'},
{"send", required_argument, 0, 's'},
{"expect", required_argument, 0, 'e'},
+ {"maxbytes", required_argument, 0, 'm'},
{"quit", required_argument, 0, 'q'},
{"delay", required_argument, 0, 'd'},
+ {"refuse", required_argument, 0, 'r'},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
}
while (1) {
- c = getopt_long (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S", long_options,
- &option_index);
+ c = getopt_long (argc, argv, "+hVvH:s:e:q:m:c:w:t:p:C:W:d:Sr:",
+ long_options, &option_index);
if (c == -1 || c == EOF || c == 1)
break;
server_expect = realloc (server_expect, ++server_expect_count);
server_expect[server_expect_count - 1] = optarg;
break;
+ case 'm':
+ if (!is_intpos (optarg))
+ usage ("Maxbytes must be a positive integer\n");
+ maxbytes = atoi (optarg);
case 'q':
server_quit = optarg;
break;
+ case 'r':
+ if (!strncmp(optarg,"ok",2))
+ econn_refuse_state = STATE_OK;
+ else if (!strncmp(optarg,"warn",4))
+ econn_refuse_state = STATE_WARNING;
+ else if (!strncmp(optarg,"crit",4))
+ econn_refuse_state = STATE_CRITICAL;
+ else
+ usage ("Refuse mut be one of ok, warn, crit\n");
+ break;
case 'd':
if (is_intpos (optarg))
delay = atoi (optarg);
return OK;
}
-
-
+\f
void
-print_usage (void)
+print_help (void)
{
- printf
- ("Usage: %s -H host -p port [-w warn_time] [-c crit_time] [-s send]\n"
- " [-e expect] [-W wait] [-t to_sec] [-v]\n", progname);
+ print_revision (progname, revision);
+ printf ("Copyright (c) %s %s\n\t<%s>\n\n", copyright, authors, email);
+ printf (summary, SERVICE);
+ print_usage ();
+ printf ("\nOptions:\n");
+ printf (options, DEFAULT_SOCKET_TIMEOUT);
+ printf (standard_options);
+ support ();
}
-
-
-
-
void
-print_help (void)
+print_usage (void)
{
- print_revision (progname, "$Revision$");
- printf
- ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
- "This plugin tests %s connections with the specified host.\n\n",
- SERVICE);
- print_usage ();
- printf
- ("Options:\n"
- " -H, --hostname=ADDRESS\n"
- " Host name argument for servers using host headers (use numeric\n"
- " address if possible to bypass DNS lookup).\n"
- " -p, --port=INTEGER\n"
- " Port number\n"
- " -s, --send=STRING\n"
- " String to send to the server\n"
- " -e, --expect=STRING\n"
- " String to expect in server response"
- " -W, --wait=INTEGER\n"
- " Seconds to wait between sending string and polling for response\n"
- " -w, --warning=DOUBLE\n"
- " Response time to result in warning status (seconds)\n"
- " -c, --critical=DOUBLE\n"
- " Response time to result in critical status (seconds)\n"
- " -t, --timeout=INTEGER\n"
- " Seconds before connection times out (default: %d)\n"
- " -v"
- " Show details for command-line debugging (do not use with nagios server)\n"
- " -h, --help\n"
- " Print detailed help screen\n"
- " -V, --version\n"
- " Print version information\n", DEFAULT_SOCKET_TIMEOUT);
+ printf ("Usage: %s %s\n", progname, option_summary);
+ printf (" %s (-h|--help)\n", progname);
+ printf (" %s (-V|--version)\n", progname);
}
+\f
+
#ifdef HAVE_SSL
time (&start_time);
/* Make TCP connection */
- if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK)
+ if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
{
/* Do the SSL handshake */
if ((ssl = SSL_new (ctx)) != NULL)