From 95d0944b9f168588f3dc4eb99f922fdf131459ab Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Wed, 15 Jan 2020 16:12:34 -0800 Subject: [PATCH 1/3] add /metrics endpoint for exposing root metrics to Prometheus --- root/root.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/root/root.cpp b/root/root.cpp index 8db77c5de..d430b999b 100644 --- a/root/root.cpp +++ b/root/root.cpp @@ -972,6 +972,33 @@ int main(int argc,char **argv) res.set_content(o.str(),"text/plain"); }); + apiServ.Get("/metrics",[](const httplib::Request &req, httplib::Response &res) { + std::ostringstream o; + int64_t now = OSUtils::now(); + + char buf[11]; + const char *root_id = s_self.address().toString(buf); + o << "# HELP root_peers_online Number of active peers online" << ZT_EOL_S; + o << "# TYPE root_peers_online gauge" << ZT_EOL_S; + s_peersByIdentity_l.lock(); + o << "root_peers_online{root_id=\"" << root_id << "\"} " << s_peersByIdentity.size() << ZT_EOL_S; + s_peersByIdentity_l.unlock(); + o << "# HELP root_input_rate Input rate MiB/s" << ZT_EOL_S; + o << "# TYPE root_input_rate gauge" << ZT_EOL_S; + o << "root_input_rate{root_id=\"" << root_id << "\"} " << std::setprecision(5) << (s_inputRate.perSecond(now)/1048576.0) << ZT_EOL_S; + o << "# HELP root_output_rate Output rate MiB/s" << ZT_EOL_S; + o << "# TYPE root_output_rate gauge" << ZT_EOL_S; + o << "root_output_rate{root_id=\"" << root_id << "\"} " << std::setprecision(5) << (s_outputRate.perSecond(now)/1048576.0) << ZT_EOL_S; + o << "# HELP root_forwarded_rate Forwarded packet rate MiB/s" << ZT_EOL_S; + o << "# TYPE root_forwarded_rate gauge" << ZT_EOL_S; + o << "root_forwarded_rate{root_id=\"" << root_id << "\"} " << std::setprecision(5) << (s_forwardRate.perSecond(now)/1048576.0) << ZT_EOL_S; + o << "# HELP root_discarded_rate Discarded forwards MiB/s" << ZT_EOL_S; + o << "# TYPE root_discarded_rate gauge" << ZT_EOL_S; + o << "root_discarded_rate{root_id=\"" << root_id << "\"} " << std::setprecision(5) << (s_discardedForwardRate.perSecond(now)/1048576.0) << ZT_EOL_S; + + res.set_content(o.str(), "text/plain"); + }); + // Peer list for compatibility with software that monitors regular nodes apiServ.Get("/peer",[](const httplib::Request &req,httplib::Response &res) { char tmp[256]; From 2558bd1b9b0655127b635111f9843a7c3d3a877b Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Thu, 16 Jan 2020 09:11:12 -0800 Subject: [PATCH 2/3] include iomanip --- root/root.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/root/root.cpp b/root/root.cpp index d430b999b..26e2e1446 100644 --- a/root/root.cpp +++ b/root/root.cpp @@ -99,6 +99,7 @@ #include #include #include +#include #include "geoip-html.h" From ff655292fba09c0a0d1ed4b4dda738c165ad4404 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 21 Jan 2020 10:58:41 -0800 Subject: [PATCH 3/3] Allocate packet on heap, add extra sanity checks on packet size. --- root/root.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/root/root.cpp b/root/root.cpp index 26e2e1446..d60a4cd96 100644 --- a/root/root.cpp +++ b/root/root.cpp @@ -901,16 +901,16 @@ int main(int argc,char **argv) threads.push_back(std::thread([s6,s4]() { struct sockaddr_in6 in6; - Packet pkt; - memset(&in6,0,sizeof(in6)); + Packet *pkt = new Packet(); for(;;) { + memset(&in6,0,sizeof(in6)); socklen_t sl = sizeof(in6); - const int pl = (int)recvfrom(s6,pkt.unsafeData(),pkt.capacity(),RECVFROM_FLAGS,(struct sockaddr *)&in6,&sl); + const int pl = (int)recvfrom(s6,pkt->unsafeData(),pkt->capacity(),RECVFROM_FLAGS,(struct sockaddr *)&in6,&sl); if (pl > 0) { - if (pl >= ZT_PROTO_MIN_FRAGMENT_LENGTH) { + if ((pl >= ZT_PROTO_MIN_FRAGMENT_LENGTH)&&(pl <= ZT_PROTO_MAX_PACKET_LENGTH)) { try { - pkt.setSize((unsigned int)pl); - handlePacket(s4,s6,reinterpret_cast(&in6),pkt); + pkt->setSize((unsigned int)pl); + handlePacket(s4,s6,reinterpret_cast(&in6),*pkt); } catch (std::exception &exc) { char ipstr[128]; printf("WARNING: unexpected exception handling packet from %s: %s" ZT_EOL_S,reinterpret_cast(&in6)->toString(ipstr),exc.what()); @@ -926,20 +926,21 @@ int main(int argc,char **argv) break; } } + delete pkt; })); threads.push_back(std::thread([s6,s4]() { struct sockaddr_in in4; - Packet pkt; - memset(&in4,0,sizeof(in4)); + Packet *pkt = new Packet(); for(;;) { + memset(&in4,0,sizeof(in4)); socklen_t sl = sizeof(in4); - const int pl = (int)recvfrom(s4,pkt.unsafeData(),pkt.capacity(),RECVFROM_FLAGS,(struct sockaddr *)&in4,&sl); + const int pl = (int)recvfrom(s4,pkt->unsafeData(),pkt->capacity(),RECVFROM_FLAGS,(struct sockaddr *)&in4,&sl); if (pl > 0) { - if (pl >= ZT_PROTO_MIN_FRAGMENT_LENGTH) { + if ((pl >= ZT_PROTO_MIN_FRAGMENT_LENGTH)&&(pl <= ZT_PROTO_MAX_PACKET_LENGTH)) { try { - pkt.setSize((unsigned int)pl); - handlePacket(s4,s6,reinterpret_cast(&in4),pkt); + pkt->setSize((unsigned int)pl); + handlePacket(s4,s6,reinterpret_cast(&in4),*pkt); } catch (std::exception &exc) { char ipstr[128]; printf("WARNING: unexpected exception handling packet from %s: %s" ZT_EOL_S,reinterpret_cast(&in4)->toString(ipstr),exc.what()); @@ -955,6 +956,7 @@ int main(int argc,char **argv) break; } } + delete pkt; })); } }