From 33cce285cbfd1d7ca79f2369f9bb735c1720e673 Mon Sep 17 00:00:00 2001
From: "Jeremy T. Bouse" <undrgrid@users.sourceforge.net>
Date: Sun, 16 Mar 2003 23:28:49 +0000
Subject: Applied patch #660973 for tcp refusals

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@426 f882894a-f735-0410-b71e-b25c423dba1c

diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c
index 7d11551..5bec291 100644
--- a/plugins/check_tcp.c
+++ b/plugins/check_tcp.c
@@ -28,7 +28,7 @@ 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]\n\
-	[-e expect] [-W wait] [-t to_sec] [-v]\n";
+	[-e expect] [-W wait] [-t to_sec] [-r refuse_state] [-v]\n";
 
 const char *options = "\
  -H, --hostname=ADDRESS\n\
@@ -48,6 +48,8 @@ const char *options = "\
     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\n\
     Show details for command-line debugging (do not use with nagios server)\n\
  -h, --help\n\
@@ -337,9 +339,11 @@ main (int argc, char **argv)
 	alarm (0);
 
 	printf
-		("%s %s - %.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);
@@ -375,6 +379,7 @@ process_arguments (int argc, char **argv)
 		{"expect", required_argument, 0, 'e'},
 		{"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'},
@@ -402,7 +407,7 @@ process_arguments (int argc, char **argv)
 	}
 
 	while (1) {
-		c = getopt_long (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S", long_options,
+		c = getopt_long (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:Sr:", long_options,
 									 &option_index);
 
 		if (c == -1 || c == EOF || c == 1)
@@ -471,6 +476,16 @@ process_arguments (int argc, char **argv)
 		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);
@@ -541,7 +556,7 @@ connect_SSL (void)
   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)
diff --git a/plugins/netutils.c b/plugins/netutils.c
index 4234c43..b3348d6 100644
--- a/plugins/netutils.c
+++ b/plugins/netutils.c
@@ -32,6 +32,8 @@
 #include "netutils.h"
 
 int socket_timeout = DEFAULT_SOCKET_TIMEOUT; 
+int econn_refuse_state = STATE_CRITICAL;
+int was_refused = FALSE;
 
 /* handles socket timeouts */
 void
@@ -275,8 +277,22 @@ my_connect (char *host_name, int port, int *sd, int proto)
 			/* attempt to open a connection */
 			result = connect (*sd, res->ai_addr, res->ai_addrlen);
 
-			if (result == 0)
+			if (result == 0) {
+				was_refused = FALSE;
 				break;
+			}
+
+			if (result < 0) {
+				switch (errno) {
+					case ECONNREFUSED:
+						switch (econn_refuse_state) {
+							case STATE_OK:
+							case STATE_WARNING:
+								was_refused = TRUE;
+						}
+						break;
+				}
+			}
 
 			close (*sd);
 			res = res->ai_next;
@@ -286,6 +302,8 @@ my_connect (char *host_name, int port, int *sd, int proto)
 
 	if (result == 0)
 		return STATE_OK;
+	else if (was_refused)
+		return econn_refuse_state;
 	else {
 		printf ("%s\n", strerror(errno));
 		return STATE_CRITICAL;
diff --git a/plugins/netutils.h b/plugins/netutils.h
index 6c8eed3..22542a7 100644
--- a/plugins/netutils.h
+++ b/plugins/netutils.h
@@ -60,3 +60,5 @@ int is_inet6_addr (char *);
 int is_hostname (char *);
 
 extern int socket_timeout;
+extern int econn_refuse_state;
+extern int was_refused;
-- 
cgit v0.10-9-g596f

