From dc8f5c0f658b5e060fc17a4beb4a0a2b195ba470 Mon Sep 17 00:00:00 2001
From: Karl DeBisschop <kdebisschop@users.sourceforge.net>
Date: Sun, 29 Feb 2004 04:09:34 +0000
Subject: was making up to 34 separate tcp connections - now we open one and
 reuse

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

diff --git a/plugins/check_nwstat.c b/plugins/check_nwstat.c
index 4ceb58f..dc8ce21 100644
--- a/plugins/check_nwstat.c
+++ b/plugins/check_nwstat.c
@@ -86,6 +86,7 @@ void print_usage(void);
 int
 main(int argc, char **argv) {
 	int result;
+	int sd;
 	char *send_buffer=NULL;
 	char recv_buffer[MAX_INPUT_BUFFER];
 	char *output_message=NULL;
@@ -129,11 +130,14 @@ main(int argc, char **argv) {
 
 	/* set socket timeout */
 	alarm(socket_timeout);
-	
+
+	/* open connection */
+	my_tcp_connect (server_address, server_port, &sd);
+
 	/* get OS version string */
 	if (check_netware_version==TRUE) {
 		send_buffer = strdup ("S19\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		if (!strcmp(recv_buffer,"-1\n"))
@@ -162,12 +166,12 @@ main(int argc, char **argv) {
 		}
 
 		asprintf (&send_buffer,"UTIL%s\r\n",temp_buffer);
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		utilization=strtoul(recv_buffer,NULL,10);
 		send_buffer = strdup ("UPTIME\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		recv_buffer[strlen(recv_buffer)-1]=0;
@@ -189,7 +193,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==CONNS) {
 
 		send_buffer = strdup ("CONNECT\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		current_connections=strtoul(recv_buffer,NULL,10);
@@ -208,7 +212,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==LTCH) {
 
 		send_buffer = strdup ("S1\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		cache_hits=atoi(recv_buffer);
@@ -227,7 +231,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==CBUFF) {
 
 		send_buffer = strdup ("S2\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		cache_buffers=strtoul(recv_buffer,NULL,10);
@@ -246,7 +250,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==CDBUFF) {
 
 		send_buffer = strdup ("S3\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		cache_buffers=strtoul(recv_buffer,NULL,10);
@@ -265,7 +269,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==LRUM) {
 
 		send_buffer = strdup ("S5\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		lru_time=strtoul(recv_buffer,NULL,10);
@@ -285,7 +289,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==VKF) {
 
 		asprintf (&send_buffer,"VKF%s\r\n",volume_name);
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
@@ -309,7 +313,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==VPF) {
 
 		asprintf (&send_buffer,"VKF%s\r\n",volume_name);
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
@@ -323,7 +327,7 @@ main(int argc, char **argv) {
 			free_disk_space=strtoul(recv_buffer,NULL,10);
 
 			asprintf (&send_buffer,"VKS%s\r\n",volume_name);
-			result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+			result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 			if (result!=STATE_OK)
 				return result;
 			total_disk_space=strtoul(recv_buffer,NULL,10);
@@ -342,7 +346,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==DSDB) {
 
 		send_buffer = strdup ("S11\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		if (atoi(recv_buffer)==1)
@@ -351,7 +355,7 @@ main(int argc, char **argv) {
 			result=STATE_WARNING;
  
 		send_buffer = strdup ("S13\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		temp_buffer=strtok(recv_buffer,"\r\n");
  
 		asprintf (&output_message,_("Directory Services Database is %s (DS version %s)"),(result==STATE_OK)?"open":"closed",temp_buffer);
@@ -360,7 +364,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==LOGINS) {
 
 		send_buffer = strdup ("S12\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		if (atoi(recv_buffer)==1)
@@ -374,14 +378,14 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==UPRB || vars_to_check==PUPRB) {
  
 		asprintf (&send_buffer,"S15\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
 		used_packet_receive_buffers=atoi(recv_buffer);
 
 		asprintf (&send_buffer,"S16\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
@@ -410,7 +414,7 @@ main(int argc, char **argv) {
 			asprintf (&send_buffer,"S9\r\n");
 		else
 			asprintf (&send_buffer,"S9.%d\r\n",sap_number);
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
  
@@ -430,7 +434,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==VKP) {
 
 		asprintf (&send_buffer,"VKP%s\r\n",volume_name);
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
@@ -450,7 +454,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==VPP) {
 
 		asprintf (&send_buffer,"VKP%s\r\n",volume_name);
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
@@ -464,7 +468,7 @@ main(int argc, char **argv) {
 			purgeable_disk_space=strtoul(recv_buffer,NULL,10);
 
 			asprintf (&send_buffer,"VKS%s\r\n",volume_name);
-			result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+			result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 			if (result!=STATE_OK)
 				return result;
 			total_disk_space=strtoul(recv_buffer,NULL,10);
@@ -483,7 +487,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==VKNP) {
 
 		asprintf (&send_buffer,"VKNP%s\r\n",volume_name);
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
@@ -503,7 +507,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==VPNP) {
 
 		asprintf (&send_buffer,"VKNP%s\r\n",volume_name);
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
@@ -517,7 +521,7 @@ main(int argc, char **argv) {
 			non_purgeable_disk_space=strtoul(recv_buffer,NULL,10);
 
 			asprintf (&send_buffer,"VKS%s\r\n",volume_name);
-			result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+			result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 			if (result!=STATE_OK)
 				return result;
 			total_disk_space=strtoul(recv_buffer,NULL,10);
@@ -536,7 +540,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==OFILES) {
 
 		asprintf (&send_buffer,"S18\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
  
@@ -553,7 +557,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==ABENDS) {
 
 		asprintf (&send_buffer,"S17\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
  
@@ -570,14 +574,14 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==CSPROCS) {
 
 		asprintf (&send_buffer,"S20\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
  
 		max_service_processes=atoi(recv_buffer);
  
 		asprintf (&send_buffer,"S21\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
  
@@ -597,7 +601,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==TSYNC) {
 
 		asprintf (&send_buffer,"S22\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
@@ -615,7 +619,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==LRUS) {
 
 		send_buffer = strdup ("S4\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		lru_time=strtoul(recv_buffer,NULL,10);
@@ -631,7 +635,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==DCB) {
 
 		send_buffer = strdup ("S6\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		dirty_cache_buffers=atoi(recv_buffer);
@@ -646,7 +650,7 @@ main(int argc, char **argv) {
 	} else if (vars_to_check==TCB) {
 
 		send_buffer = strdup ("S7\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 		total_cache_buffers=atoi(recv_buffer);
@@ -659,7 +663,7 @@ main(int argc, char **argv) {
 		
 	} else if (vars_to_check==DSVER) {
 		asprintf (&send_buffer,"S13\r\n");
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
@@ -668,7 +672,7 @@ main(int argc, char **argv) {
 		asprintf (&output_message,_("NDS Version %s"),recv_buffer);
 
 	} else if (vars_to_check==UPTIME) {
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 	 		return result;
 
@@ -678,7 +682,7 @@ main(int argc, char **argv) {
 
 	} else if (vars_to_check==NLM) {
 		asprintf (&send_buffer,"S24:%s\r\n",nlm_name);
-		result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
+		result=send_tcp_request(sd,send_buffer,recv_buffer,sizeof(recv_buffer));
 		if (result!=STATE_OK)
 			return result;
 
@@ -697,6 +701,8 @@ main(int argc, char **argv) {
 
 	}
 
+	close (sd);
+
 	/* reset timeout */
 	alarm(0);
 
diff --git a/plugins/netutils.c b/plugins/netutils.c
index 55f02ca..5017eb9 100644
--- a/plugins/netutils.c
+++ b/plugins/netutils.c
@@ -168,11 +168,7 @@ process_request (const char *server_address, int server_port, int proto,
 	const char *send_buffer, char *recv_buffer, int recv_size)
 {
 	int result;
-	int send_result;
-	int recv_result;
 	int sd;
-	struct timeval tv;
-	fd_set readfds;
 
 	result = STATE_OK;
 
@@ -180,41 +176,7 @@ process_request (const char *server_address, int server_port, int proto,
 	if (result != STATE_OK)
 		return STATE_CRITICAL;
 
-	send_result = send (sd, send_buffer, strlen (send_buffer), 0);
-	if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
-		printf ("send() failed\n");
-		result = STATE_WARNING;
-	}
-
-	/* wait up to the number of seconds for socket timeout minus one 
-	   for data from the host */
-	tv.tv_sec = socket_timeout - 1;
-	tv.tv_usec = 0;
-	FD_ZERO (&readfds);
-	FD_SET (sd, &readfds);
-	select (sd + 1, &readfds, NULL, NULL, &tv);
-
-	/* make sure some data has arrived */
-	if (!FD_ISSET (sd, &readfds)) {
-		strcpy (recv_buffer, "");
-		printf ("No data was received from host!\n");
-		result = STATE_WARNING;
-	}
-
-	else {
-		recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
-		if (recv_result == -1) {
-			strcpy (recv_buffer, "");
-			if (proto != IPPROTO_TCP)
-				printf ("recv() failed\n");
-			result = STATE_WARNING;
-		}
-		else
-			recv_buffer[recv_result] = 0;
-
-		/* die returned string */
-		recv_buffer[recv_size - 1] = 0;
-	}
+	result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
 
 	close (sd);
 
@@ -315,6 +277,69 @@ my_connect (const char *host_name, int port, int *sd, int proto)
 	}
 }
 
+
+int
+send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
+{
+	return send_request (sd, IPPROTO_TCP, send_buffer, recv_buffer, recv_size);
+}
+
+
+int
+send_udp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size)
+{
+	return send_request (sd, IPPROTO_UDP, send_buffer, recv_buffer, recv_size);
+}
+
+
+int
+send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
+{
+	int result;
+	int send_result;
+	int recv_result;
+	struct timeval tv;
+	fd_set readfds;
+
+	send_result = send (sd, send_buffer, strlen (send_buffer), 0);
+	if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
+		printf ("send() failed\n");
+		result = STATE_WARNING;
+	}
+
+	/* wait up to the number of seconds for socket timeout minus one 
+	   for data from the host */
+	tv.tv_sec = socket_timeout - 1;
+	tv.tv_usec = 0;
+	FD_ZERO (&readfds);
+	FD_SET (sd, &readfds);
+	select (sd + 1, &readfds, NULL, NULL, &tv);
+
+	/* make sure some data has arrived */
+	if (!FD_ISSET (sd, &readfds)) {
+		strcpy (recv_buffer, "");
+		printf ("No data was received from host!\n");
+		result = STATE_WARNING;
+	}
+
+	else {
+		recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
+		if (recv_result == -1) {
+			strcpy (recv_buffer, "");
+			if (proto != IPPROTO_TCP)
+				printf ("recv() failed\n");
+			result = STATE_WARNING;
+		}
+		else
+			recv_buffer[recv_result] = 0;
+
+		/* die returned string */
+		recv_buffer[recv_size - 1] = 0;
+	}
+	return result;
+}
+
+
 int
 is_host (const char *address)
 {
diff --git a/plugins/netutils.h b/plugins/netutils.h
index 88a5162..b2c2a2f 100644
--- a/plugins/netutils.h
+++ b/plugins/netutils.h
@@ -48,6 +48,10 @@ int process_request (const char *address, int port, int proto,
 int my_tcp_connect (const char *address, int port, int *sd);
 int my_udp_connect (const char *address, int port, int *sd);
 
+int send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size);
+int send_udp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size);
+int send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size);
+
 int is_host (const char *);
 int is_addr (const char *);
 int resolve_host_or_addr (const char *, int);
-- 
cgit v0.10-9-g596f

