2
0
Эх сурвалжийг харах

begin adding some metrics to tcp-proxy

Grant Limberg 1 жил өмнө
parent
commit
342657e629

+ 9 - 4
tcp-proxy/Makefile

@@ -1,9 +1,14 @@
 CXX=$(shell which clang++ g++ c++ 2>/dev/null | head -n 1)
 CXX=$(shell which clang++ g++ c++ 2>/dev/null | head -n 1)
 
 
-INCLUDES?=-I../ext/prometheus-cpp-lite-1.0/core/include  -I../ext/prometheus-cpp-lite-1.0/simpleapi/include
+INCLUDES?=-isystem ../ext/prometheus-cpp-lite-1.0/core/include  -isystem ../ext/prometheus-cpp-lite-1.0/simpleapi/include
 
 
-all:
-	$(CXX) -O3 -fno-rtti $(INCLUDES) -std=c++11 -pthread -frtti  -o tcp-proxy tcp-proxy.cpp ../node/Metrics.cpp
+OBJS=Metrics.o	\
+	../node/Metrics.o 
+
+CXXFLAGS=-O3 -fno-rtti $(INCLUDES) -std=c++17 -pthread -frtti
+
+all: $(OBJS) tcp-proxy.o
+	$(CXX) -O3 -fno-rtti $(INCLUDES) -std=c++17 -pthread -frtti  -o tcp-proxy tcp-proxy.o $(OBJS) 
 
 
 clean:
 clean:
-	rm -f *.o tcp-proxy *.dSYM
+	rm -f $(OBJS) tcp-proxy.o tcp-proxy *.dSYM

+ 24 - 0
tcp-proxy/Metrics.cpp

@@ -0,0 +1,24 @@
+#include <prometheus/simpleapi.h>
+
+
+namespace ZeroTier {
+    namespace Metrics {
+        prometheus::simpleapi::counter_metric_t udp_open_failed
+        {"udp_open_failed", "UDP open failed"};
+
+        prometheus::simpleapi::counter_metric_t tcp_opened
+        {"tcp_opened", "TCP opened"};
+        prometheus::simpleapi::counter_metric_t tcp_closed
+        {"tcp_closed", "TCP closed"};
+
+        prometheus::simpleapi::counter_metric_t tcp_bytes_in
+        {"tcp_byes_in", "TCP bytes in"};
+        prometheus::simpleapi::counter_metric_t tcp_bytes_out
+        {"tcp_byes_out", "TCP bytes out"};
+
+        prometheus::simpleapi::counter_metric_t udp_bytes_in
+        {"udp_bytes_in", "UDP bytes in"};
+        prometheus::simpleapi::counter_metric_t udp_bytes_out
+        {"udp_bytes_out", "UDP bytes out"};
+    }
+}

+ 21 - 0
tcp-proxy/Metrics.hpp

@@ -0,0 +1,21 @@
+#ifndef _TCP_PROXY_METRICS_H_
+#define _TCP_PROXY_METRICS_H_
+
+#include "../node/Metrics.hpp"
+
+namespace ZeroTier {
+    namespace Metrics {
+        extern prometheus::simpleapi::counter_metric_t udp_open_failed;
+
+        extern prometheus::simpleapi::counter_metric_t tcp_opened;
+        extern prometheus::simpleapi::counter_metric_t tcp_closed;
+
+        extern prometheus::simpleapi::counter_metric_t tcp_bytes_in;
+        extern prometheus::simpleapi::counter_metric_t tcp_bytes_out;
+
+        extern prometheus::simpleapi::counter_metric_t udp_bytes_in;
+        extern prometheus::simpleapi::counter_metric_t udp_bytes_out;
+    }
+}
+
+#endif // _TCP_PROXY_METRICS_H_

+ 13 - 2
tcp-proxy/tcp-proxy.cpp

@@ -43,7 +43,7 @@
 
 
 #include "../osdep/Phy.hpp"
 #include "../osdep/Phy.hpp"
 
 
-#include "../node/Metrics.hpp"
+#include "Metrics.hpp"
 
 
 #define ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS 300
 #define ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS 300
 #define ZT_TCP_PROXY_TCP_PORT 443
 #define ZT_TCP_PROXY_TCP_PORT 443
@@ -127,6 +127,8 @@ struct TcpProxyService
 		if (!*uptr)
 		if (!*uptr)
 			return;
 			return;
 		if ((from->sa_family == AF_INET)&&(len >= 16)&&(len < 2048)) {
 		if ((from->sa_family == AF_INET)&&(len >= 16)&&(len < 2048)) {
+			Metrics::udp_bytes_in += len;
+
 			Client &c = *((Client *)*uptr);
 			Client &c = *((Client *)*uptr);
 			c.lastActivity = time((time_t *)0);
 			c.lastActivity = time((time_t *)0);
 
 
@@ -171,6 +173,7 @@ struct TcpProxyService
 		Client &c = clients[sockN];
 		Client &c = clients[sockN];
 		PhySocket *udp = getUnusedUdp((void *)&c);
 		PhySocket *udp = getUnusedUdp((void *)&c);
 		if (!udp) {
 		if (!udp) {
+			Metrics::udp_open_failed++;
 			phy->close(sockN);
 			phy->close(sockN);
 			clients.erase(sockN);
 			clients.erase(sockN);
 			printf("** TCP rejected, no more UDP ports to assign\n");
 			printf("** TCP rejected, no more UDP ports to assign\n");
@@ -184,6 +187,7 @@ struct TcpProxyService
 		c.newVersion = false;
 		c.newVersion = false;
 		*uptrN = (void *)&c;
 		*uptrN = (void *)&c;
 		printf("<< TCP from %s -> %.16llx\n",inet_ntoa(reinterpret_cast<const struct sockaddr_in *>(from)->sin_addr),(unsigned long long)&c);
 		printf("<< TCP from %s -> %.16llx\n",inet_ntoa(reinterpret_cast<const struct sockaddr_in *>(from)->sin_addr),(unsigned long long)&c);
+		Metrics::tcp_opened++;
 	}
 	}
 
 
 	void phyOnTcpClose(PhySocket *sock,void **uptr)
 	void phyOnTcpClose(PhySocket *sock,void **uptr)
@@ -194,6 +198,7 @@ struct TcpProxyService
 		phy->close(c.udp);
 		phy->close(c.udp);
 		clients.erase(sock);
 		clients.erase(sock);
 		printf("** TCP %.16llx closed\n",(unsigned long long)*uptr);
 		printf("** TCP %.16llx closed\n",(unsigned long long)*uptr);
+		Metrics::tcp_closed++;
 	}
 	}
 
 
 	void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
 	void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
@@ -201,6 +206,8 @@ struct TcpProxyService
 		Client &c = *((Client *)*uptr);
 		Client &c = *((Client *)*uptr);
 		c.lastActivity = time((time_t *)0);
 		c.lastActivity = time((time_t *)0);
 
 
+		Metrics::tcp_bytes_in += len;
+
 		for(unsigned long i=0;i<len;++i) {
 		for(unsigned long i=0;i<len;++i) {
 			if (c.tcpReadPtr >= sizeof(c.tcpReadBuf)) {
 			if (c.tcpReadPtr >= sizeof(c.tcpReadBuf)) {
 				phy->close(sock);
 				phy->close(sock);
@@ -246,6 +253,7 @@ struct TcpProxyService
 						if ((ntohs(dest.sin_port) > 1024)&&(payloadLen >= 16)) {
 						if ((ntohs(dest.sin_port) > 1024)&&(payloadLen >= 16)) {
 							phy->udpSend(c.udp,(const struct sockaddr *)&dest,payload,payloadLen);
 							phy->udpSend(c.udp,(const struct sockaddr *)&dest,payload,payloadLen);
 							printf(">> TCP %.16llx to %s:%d\n",(unsigned long long)*uptr,inet_ntoa(dest.sin_addr),(int)ntohs(dest.sin_port));
 							printf(">> TCP %.16llx to %s:%d\n",(unsigned long long)*uptr,inet_ntoa(dest.sin_addr),(int)ntohs(dest.sin_port));
+							Metrics::udp_bytes_out += payloadLen;
 						}
 						}
 					}
 					}
 
 
@@ -260,6 +268,7 @@ struct TcpProxyService
 		Client &c = *((Client *)*uptr);
 		Client &c = *((Client *)*uptr);
 		if (c.tcpWritePtr) {
 		if (c.tcpWritePtr) {
 			long n = phy->streamSend(sock,c.tcpWriteBuf,c.tcpWritePtr);
 			long n = phy->streamSend(sock,c.tcpWriteBuf,c.tcpWritePtr);
+			Metrics::tcp_bytes_out += n;
 			if (n > 0) {
 			if (n > 0) {
 				memmove(c.tcpWriteBuf,c.tcpWriteBuf + n,c.tcpWritePtr -= (unsigned long)n);
 				memmove(c.tcpWriteBuf,c.tcpWriteBuf + n,c.tcpWritePtr -= (unsigned long)n);
 				if (!c.tcpWritePtr)
 				if (!c.tcpWritePtr)
@@ -278,8 +287,10 @@ struct TcpProxyService
 				toClose.push_back(c->second.udp);
 				toClose.push_back(c->second.udp);
 			}
 			}
 		}
 		}
-		for(std::vector<PhySocket *>::iterator s(toClose.begin());s!=toClose.end();++s)
+		for(std::vector<PhySocket *>::iterator s(toClose.begin());s!=toClose.end();++s) {
 			phy->close(*s);
 			phy->close(*s);
+			Metrics::tcp_closed++;
+		}
 	}
 	}
 };
 };