diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c
index c537729f0764da8261a3c23f4318f4a5123ae562..8a2dcc5c53a14d090c7aa350f075ee64cbbaaf8d 100644 (file)
--- a/plugins/check_tcp.c
+++ b/plugins/check_tcp.c
*
*****************************************************************************/
*
*****************************************************************************/
-#define PROGRAM check_tcp
+#define REVISION "$Revision$"
#define DESCRIPTION "Check a TCP port"
#define AUTHOR "Ethan Galstad"
#define EMAIL "nagios@nagios.org"
#define DESCRIPTION "Check a TCP port"
#define AUTHOR "Ethan Galstad"
#define EMAIL "nagios@nagios.org"
-#define COPYRIGHTDATE "1999"
+#define COPYRIGHTDATE "2002"
#include "config.h"
#include "common.h"
#include "config.h"
#include "common.h"
int connect_SSL (void);
#endif
int connect_SSL (void);
#endif
-#define TCP_PROTOCOL 1
-#define UDP_PROTOCOL 2
+enum {
+ TCP_PROTOCOL = 1,
+ UDP_PROTOCOL = 2,
+ MAXBUF = 1024
+};
int process_arguments (int, char **);
void print_usage (void);
void print_help (void);
int process_arguments (int, char **);
void print_usage (void);
void print_help (void);
+int my_recv (void);
-char *PROGNAME = NULL;
+char *progname = "check_tcp";
char *SERVICE = NULL;
char *SEND = NULL;
char *EXPECT = NULL;
char *SERVICE = NULL;
char *SEND = NULL;
char *EXPECT = NULL;
char **crit_codes = NULL;
int crit_codes_count = 0;
int delay = 0;
char **crit_codes = NULL;
int crit_codes_count = 0;
int delay = 0;
-int warning_time = 0;
+double warning_time = 0;
int check_warning_time = FALSE;
int check_warning_time = FALSE;
-int critical_time = 0;
+double critical_time = 0;
int check_critical_time = FALSE;
int check_critical_time = FALSE;
+double elapsed_time = 0;
int verbose = FALSE;
int use_ssl = FALSE;
int verbose = FALSE;
int use_ssl = FALSE;
-int sd;
+int sd = 0;
+char *buffer = "";
int
main (int argc, char **argv)
{
int result;
int i;
int
main (int argc, char **argv)
{
int result;
int i;
- char buffer[MAX_INPUT_BUFFER] = "";
- char *status = NULL;
- char *output = NULL;
- char *ptr = NULL;
+ char *status = "";
+ struct timeval tv;
if (strstr (argv[0], "check_udp")) {
if (strstr (argv[0], "check_udp")) {
- PROGNAME = strscpy (PROGNAME, "check_udp");
- SERVICE = strscpy (SERVICE, "UDP");
+ asprintf (&progname, "check_udp");
+ asprintf (&SERVICE, "UDP");
SEND = NULL;
EXPECT = NULL;
QUIT = NULL;
SEND = NULL;
EXPECT = NULL;
QUIT = NULL;
PORT = 0;
}
else if (strstr (argv[0], "check_tcp")) {
PORT = 0;
}
else if (strstr (argv[0], "check_tcp")) {
- PROGNAME = strscpy (PROGNAME, "check_tcp");
- SERVICE = strscpy (SERVICE, "TCP");
+ asprintf (&progname, "check_tcp");
+ asprintf (&SERVICE, "TCP");
SEND = NULL;
EXPECT = NULL;
QUIT = NULL;
SEND = NULL;
EXPECT = NULL;
QUIT = NULL;
PORT = 0;
}
else if (strstr (argv[0], "check_ftp")) {
PORT = 0;
}
else if (strstr (argv[0], "check_ftp")) {
- PROGNAME = strscpy (PROGNAME, "check_ftp");
- SERVICE = strscpy (SERVICE, "FTP");
+ asprintf (&progname, "check_ftp");
+ asprintf (&SERVICE, "FTP");
SEND = NULL;
SEND = NULL;
- EXPECT = strscpy (EXPECT, "220");
- QUIT = strscpy (QUIT, "QUIT\r\n");
+ asprintf (&EXPECT, "220");
+ asprintf (&QUIT, "QUIT\r\n");
PROTOCOL = TCP_PROTOCOL;
PORT = 21;
}
else if (strstr (argv[0], "check_smtp")) {
PROTOCOL = TCP_PROTOCOL;
PORT = 21;
}
else if (strstr (argv[0], "check_smtp")) {
- PROGNAME = strscpy (PROGNAME, "check_smtp");
- SERVICE = strscpy (SERVICE, "SMTP");
+ asprintf (&progname, "check_smtp");
+ asprintf (&SERVICE, "SMTP");
SEND = NULL;
SEND = NULL;
- EXPECT = strscpy (EXPECT, "220");
- QUIT = strscpy (QUIT, "QUIT\r\n");
+ asprintf (&EXPECT, "220");
+ asprintf (&QUIT, "QUIT\r\n");
PROTOCOL = TCP_PROTOCOL;
PORT = 25;
}
else if (strstr (argv[0], "check_pop")) {
PROTOCOL = TCP_PROTOCOL;
PORT = 25;
}
else if (strstr (argv[0], "check_pop")) {
- PROGNAME = strscpy (PROGNAME, "check_pop");
- SERVICE = strscpy (SERVICE, "POP");
+ asprintf (&progname, "check_pop");
+ asprintf (&SERVICE, "POP");
SEND = NULL;
SEND = NULL;
- EXPECT = strscpy (EXPECT, "110");
- QUIT = strscpy (QUIT, "QUIT\r\n");
+ asprintf (&EXPECT, "+OK");
+ asprintf (&QUIT, "QUIT\r\n");
PROTOCOL = TCP_PROTOCOL;
PORT = 110;
}
else if (strstr (argv[0], "check_imap")) {
PROTOCOL = TCP_PROTOCOL;
PORT = 110;
}
else if (strstr (argv[0], "check_imap")) {
- PROGNAME = strscpy (PROGNAME, "check_imap");
- SERVICE = strscpy (SERVICE, "IMAP");
+ asprintf (&progname, "check_imap");
+ asprintf (&SERVICE, "IMAP");
SEND = NULL;
SEND = NULL;
- EXPECT = strscpy (EXPECT, "* OK");
- QUIT = strscpy (QUIT, "a1 LOGOUT\r\n");
+ asprintf (&EXPECT, "* OK");
+ asprintf (&QUIT, "a1 LOGOUT\r\n");
PROTOCOL = TCP_PROTOCOL;
PORT = 143;
}
#ifdef HAVE_SSL
else if (strstr(argv[0],"check_simap")) {
PROTOCOL = TCP_PROTOCOL;
PORT = 143;
}
#ifdef HAVE_SSL
else if (strstr(argv[0],"check_simap")) {
- PROGNAME=strscpy(PROGNAME,"check_simap");
- SERVICE=strscpy(SERVICE,"SIMAP");
+ asprintf (&progname, "check_simap");
+ asprintf (&SERVICE, "SIMAP");
SEND=NULL;
SEND=NULL;
- EXPECT=strscpy(EXPECT,"* OK");
- QUIT=strscpy(QUIT,"a1 LOGOUT\n");
+ asprintf (&EXPECT, "* OK");
+ asprintf (&QUIT, "a1 LOGOUT\r\n");
PROTOCOL=TCP_PROTOCOL;
use_ssl=TRUE;
PORT=993;
}
PROTOCOL=TCP_PROTOCOL;
use_ssl=TRUE;
PORT=993;
}
+ else if (strstr(argv[0],"check_spop")) {
+ asprintf (&progname, "check_spop");
+ asprintf (&SERVICE, "SPOP");
+ SEND=NULL;
+ asprintf (&EXPECT, "+OK");
+ asprintf (&QUIT, "QUIT\r\n");
+ PROTOCOL=TCP_PROTOCOL;
+ use_ssl=TRUE;
+ PORT=995;
+ }
#endif
else if (strstr (argv[0], "check_nntp")) {
#endif
else if (strstr (argv[0], "check_nntp")) {
- PROGNAME = strscpy (PROGNAME, "check_nntp");
- SERVICE = strscpy (SERVICE, "NNTP");
+ asprintf (&progname, "check_nntp");
+ asprintf (&SERVICE, "NNTP");
SEND = NULL;
EXPECT = NULL;
server_expect = realloc (server_expect, ++server_expect_count);
SEND = NULL;
EXPECT = NULL;
server_expect = realloc (server_expect, ++server_expect_count);
- server_expect[server_expect_count - 1] = strscpy (EXPECT, "200");
+ asprintf (&server_expect[server_expect_count - 1], "200");
server_expect = realloc (server_expect, ++server_expect_count);
server_expect = realloc (server_expect, ++server_expect_count);
- server_expect[server_expect_count - 1] = strscpy (NULL, "201");
- QUIT = strscpy (QUIT, "QUIT\r\n");
+ asprintf (&server_expect[server_expect_count - 1], "201");
+ asprintf (&QUIT, "QUIT\r\n");
PROTOCOL = TCP_PROTOCOL;
PORT = 119;
}
PROTOCOL = TCP_PROTOCOL;
PORT = 119;
}
usage ("ERROR: Generic check_tcp called with unknown service\n");
}
usage ("ERROR: Generic check_tcp called with unknown service\n");
}
- server_address = strscpy (NULL, "127.0.0.1");
+ asprintf (&server_address, "127.0.0.1");
server_port = PORT;
server_send = SEND;
server_quit = QUIT;
server_port = PORT;
server_send = SEND;
server_quit = QUIT;
/* use default expect if none listed in process_arguments() */
if (EXPECT && server_expect_count == 0) {
/* use default expect if none listed in process_arguments() */
if (EXPECT && server_expect_count == 0) {
- server_expect = malloc (1);
+ server_expect = malloc (++server_expect_count);
server_expect[server_expect_count - 1] = EXPECT;
}
server_expect[server_expect_count - 1] = EXPECT;
}
alarm (socket_timeout);
/* try to connect to the host at the given port number */
alarm (socket_timeout);
/* try to connect to the host at the given port number */
- time (&start_time);
+ gettimeofday (&tv, NULL);
#ifdef HAVE_SSL
if (use_ssl)
result = connect_SSL ();
#ifdef HAVE_SSL
if (use_ssl)
result = connect_SSL ();
return STATE_CRITICAL;
if (server_send != NULL) { /* Something to send? */
return STATE_CRITICAL;
if (server_send != NULL) { /* Something to send? */
- snprintf (buffer, MAX_INPUT_BUFFER - 1, "%s\r\n", server_send);
- buffer[MAX_INPUT_BUFFER - 1] = 0;
+ asprintf (&server_send, "%s\r\n", server_send);
#ifdef HAVE_SSL
if (use_ssl)
#ifdef HAVE_SSL
if (use_ssl)
- SSL_write(ssl,buffer,strlen(buffer));
+ SSL_write(ssl, server_send, strlen (server_send));
else
#endif
else
#endif
- send (sd, buffer, strlen (buffer), 0);
+ send (sd, server_send, strlen (server_send), 0);
}
if (delay > 0) {
}
if (delay > 0) {
- start_time = start_time + delay;
+ tv.tv_sec += delay;
sleep (delay);
}
if (server_send || server_expect_count > 0) {
sleep (delay);
}
if (server_send || server_expect_count > 0) {
+ buffer = malloc (MAXBUF);
+ memset (buffer, '\0', MAXBUF);
/* watch for the expect string */
/* watch for the expect string */
-#ifdef HAVE_SSL
- if (use_ssl && SSL_read (ssl, buffer, MAX_INPUT_BUFFER - 1)>=0)
- status = strscat(status,buffer);
- else
-#endif
- {
- if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) >= 0)
- status = strscat (status, buffer);
- }
- strip (status);
+ while ((i = my_recv ()) > 0) {
+ buffer[i] = '\0';
+ asprintf (&status, "%s%s", status, buffer);
+ if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
+ break;
+ }
/* return a CRITICAL status if we couldn't read any data */
if (status == NULL)
terminate (STATE_CRITICAL, "No data received from host\n");
/* return a CRITICAL status if we couldn't read any data */
if (status == NULL)
terminate (STATE_CRITICAL, "No data received from host\n");
+ strip (status);
+
if (status && verbose)
printf ("%s\n", status);
if (server_expect_count > 0) {
for (i = 0;; i++) {
if (status && verbose)
printf ("%s\n", status);
if (server_expect_count > 0) {
for (i = 0;; i++) {
- printf ("%d %d\n", i, server_expect_count);
+ if (verbose)
+ printf ("%d %d\n", i, server_expect_count);
if (i >= server_expect_count)
terminate (STATE_WARNING, "Invalid response from host\n");
if (strstr (status, server_expect[i]))
if (i >= server_expect_count)
terminate (STATE_WARNING, "Invalid response from host\n");
if (strstr (status, server_expect[i]))
}
}
}
}
- if (server_quit)
+ if (server_quit != NULL)
#ifdef HAVE_SSL
if (use_ssl) {
SSL_write (ssl, QUIT, strlen (QUIT));
SSL_shutdown (ssl);
#ifdef HAVE_SSL
if (use_ssl) {
SSL_write (ssl, QUIT, strlen (QUIT));
SSL_shutdown (ssl);
- SSL_free (ssl);
- SSL_CTX_free (ctx);
+ SSL_free (ssl);
+ SSL_CTX_free (ctx);
}
else
#endif
send (sd, server_quit, strlen (server_quit), 0);
/* close the connection */
}
else
#endif
send (sd, server_quit, strlen (server_quit), 0);
/* close the connection */
- close (sd);
+ if (sd)
+ close (sd);
- time (&end_time);
+ elapsed_time = delta_time (tv);
- if (check_critical_time == TRUE && (end_time - start_time) > critical_time)
+ if (check_critical_time == TRUE && elapsed_time > critical_time)
result = STATE_CRITICAL;
result = STATE_CRITICAL;
- else if (check_warning_time == TRUE
- && (end_time - start_time) > warning_time) result = STATE_WARNING;
+ else if (check_warning_time == TRUE && elapsed_time > warning_time)
+ result = STATE_WARNING;
/* reset the alarm */
alarm (0);
printf
/* reset the alarm */
alarm (0);
printf
- ("%s %s - %d second response time on port %d",
+ ("%s %s - %7.3f second response time on port %d",
SERVICE,
SERVICE,
- state_text (result), (int) (end_time - start_time), server_port);
+ state_text (result), elapsed_time, server_port);
- if (status)
+ if (status && strlen(status) > 0)
printf (" [%s]", status);
printf (" [%s]", status);
- printf ("|time=%d\n", (int) (end_time - start_time));
+ printf ("|time=%7.3f\n", elapsed_time);
return result;
}
return result;
}
switch (c) {
case '?': /* print short usage statement if args not parsable */
switch (c) {
case '?': /* print short usage statement if args not parsable */
- printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
+ printf ("%s: Unknown argument: %s\n\n", progname, optarg);
print_usage ();
exit (STATE_UNKNOWN);
case 'h': /* help */
print_help ();
exit (STATE_OK);
case 'V': /* version */
print_usage ();
exit (STATE_UNKNOWN);
case 'h': /* help */
print_help ();
exit (STATE_OK);
case 'V': /* version */
- print_revision (PROGNAME, "$Revision$");
+ print_revision (progname, "$Revision$");
exit (STATE_OK);
case 'v': /* verbose mode */
verbose = TRUE;
break;
case 'H': /* hostname */
if (is_host (optarg) == FALSE)
exit (STATE_OK);
case 'v': /* verbose mode */
verbose = TRUE;
break;
case 'H': /* hostname */
if (is_host (optarg) == FALSE)
- usage ("Invalid host name/address\n");
+ usage2 ("invalid host name or address", optarg);
server_address = optarg;
break;
case 'c': /* critical */
if (!is_intnonneg (optarg))
usage ("Critical threshold must be a nonnegative integer\n");
server_address = optarg;
break;
case 'c': /* critical */
if (!is_intnonneg (optarg))
usage ("Critical threshold must be a nonnegative integer\n");
- critical_time = atoi (optarg);
+ critical_time = strtod (optarg, NULL);
check_critical_time = TRUE;
break;
case 'w': /* warning */
if (!is_intnonneg (optarg))
usage ("Warning threshold must be a nonnegative integer\n");
check_critical_time = TRUE;
break;
case 'w': /* warning */
if (!is_intnonneg (optarg))
usage ("Warning threshold must be a nonnegative integer\n");
- warning_time = atoi (optarg);
+ warning_time = strtod (optarg, NULL);
check_warning_time = TRUE;
break;
case 'C':
check_warning_time = TRUE;
break;
case 'C':
case 's':
server_send = optarg;
break;
case 's':
server_send = optarg;
break;
- case 'e':
+ case 'e': /* expect string (may be repeated) */
EXPECT = NULL;
if (server_expect_count == 0)
server_expect = malloc (++server_expect_count);
EXPECT = NULL;
if (server_expect_count == 0)
server_expect = malloc (++server_expect_count);
{
printf
("Usage: %s -H host -p port [-w warn_time] [-c crit_time] [-s send]\n"
{
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);
+ " [-e expect] [-W wait] [-t to_sec] [-v]\n", progname);
}
}
void
print_help (void)
{
void
print_help (void)
{
- print_revision (PROGNAME, "$Revision$");
+ 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",
printf
("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
"This plugin tests %s connections with the specified host.\n\n",
" String to expect in server response"
" -W, --wait=INTEGER\n"
" Seconds to wait between sending string and polling for response\n"
" String to expect in server response"
" -W, --wait=INTEGER\n"
" Seconds to wait between sending string and polling for response\n"
- " -w, --warning=INTEGER\n"
+ " -w, --warning=DOUBLE\n"
" Response time to result in warning status (seconds)\n"
" Response time to result in warning status (seconds)\n"
- " -c, --critical=INTEGER\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"
" Response time to result in critical status (seconds)\n"
" -t, --timeout=INTEGER\n"
" Seconds before connection times out (default: %d)\n"
}
#endif
}
#endif
+
+
+int
+my_recv (void)
+{
+ int i;
+
+#ifdef HAVE_SSL
+ if (use_ssl) {
+ i = SSL_read (ssl, buffer, MAXBUF - 1);
+ }
+ else {
+#endif
+ i = read (sd, buffer, MAXBUF - 1);
+#ifdef HAVE_SSL
+ }
+#endif
+
+ return i;
+}