Sfoglia il codice sorgente

Applied patch #660973 for tcp refusals

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@426 f882894a-f735-0410-b71e-b25c423dba1c
Jeremy T. Bouse 23 anni fa
parent
commit
33cce285cb
3 ha cambiato i file con 41 aggiunte e 6 eliminazioni
  1. 20 5
      plugins/check_tcp.c
  2. 19 1
      plugins/netutils.c
  3. 2 0
      plugins/netutils.h

+ 20 - 5
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)

+ 19 - 1
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;

+ 2 - 0
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;