diff --git a/include/ContinuousPing.h b/include/ContinuousPing.h index 00e9b3f4ab..b84e595037 100644 --- a/include/ContinuousPing.h +++ b/include/ContinuousPing.h @@ -24,45 +24,6 @@ #ifndef WIN32 -struct cp_stats { - time_t last_refresh; - u_int32_t num_ping_sent, num_ping_rcvd; - float min_rtt, max_rtt, last_rtt; -}; - -/* ***************************************** */ - -class ContinuousPingStats { - private: - struct cp_stats stats; - - public: - ContinuousPingStats() { reset(); } - - inline void getStats(struct cp_stats *out) { memcpy(&out, &stats, sizeof(struct cp_stats)); } - inline void heartbeat() { stats.last_refresh = time(NULL); } - inline void incSent() { stats.num_ping_sent++; } - inline void update(float rtt) { - stats.num_ping_rcvd++; - - if(rtt > 0) { - stats.last_rtt = rtt; - stats.min_rtt = (stats.num_ping_rcvd == 1) ? rtt : min(stats.min_rtt, rtt); - stats.max_rtt = max(stats.max_rtt, rtt); - } - } - - inline float getSuccessRate(float *min_rtt, float *max_rtt) { - float pctg = min((stats.num_ping_sent == 0) ? 0 : (float)(stats.num_ping_rcvd*100)/(float)(stats.num_ping_sent), 100.f); - - *min_rtt = stats.min_rtt, *max_rtt = stats.max_rtt; - - return(pctg); - } - - inline void reset() { memset(&stats, 0, sizeof(stats)); heartbeat(); } -}; - /* ***************************************** */ class ContinuousPing { diff --git a/include/ContinuousPingStats.h b/include/ContinuousPingStats.h new file mode 100644 index 0000000000..ae07a87a09 --- /dev/null +++ b/include/ContinuousPingStats.h @@ -0,0 +1,53 @@ +/* + * + * (C) 2019 - ntop.org + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#ifndef _CONTINUOUS_PING_STATS_H_ +#define _CONTINUOUS_PING_STATS_H_ + +#ifndef WIN32 + +struct cp_stats { + time_t last_refresh; + u_int32_t num_ping_sent, num_ping_rcvd; + float min_rtt, max_rtt, last_rtt; +}; + +/* ***************************************** */ + +class ContinuousPingStats { + private: + struct cp_stats stats; + + public: + ContinuousPingStats() { reset(); } + + inline void getStats(struct cp_stats *out) { memcpy(&out, &stats, sizeof(struct cp_stats)); } + inline void heartbeat() { stats.last_refresh = time(NULL); } + inline void incSent() { stats.num_ping_sent++; } + void update(float rtt); + float getSuccessRate(float *min_rtt, float *max_rtt); + inline void reset() { memset(&stats, 0, sizeof(stats)); heartbeat(); } +}; + +#endif /* WIN32 */ + +#endif /* _CONTINUOUS_PING_STATS_H_ */ + diff --git a/include/ntop_includes.h b/include/ntop_includes.h index a5c5216496..a348a1d8d7 100644 --- a/include/ntop_includes.h +++ b/include/ntop_includes.h @@ -201,6 +201,7 @@ using namespace std; #include "BroadcastDomains.h" #include "IpAddress.h" #include "Ping.h" +#include "ContinuousPingStats.h" #include "ContinuousPing.h" #include "TrafficStats.h" #include "TcpPacketStats.h" diff --git a/src/ContinuousPing.cpp b/src/ContinuousPing.cpp index 600d83701f..8d1d6857b7 100644 --- a/src/ContinuousPing.cpp +++ b/src/ContinuousPing.cpp @@ -23,6 +23,8 @@ #include "ntop_includes.h" +#define TRACE_PING_DROPS + /* #define TRACE_PING */ /* @@ -114,10 +116,10 @@ void ContinuousPing::pingAll() { m.lock(__FILE__, __LINE__); for(std::map::iterator it=v4_results.begin(); it!=v4_results.end(); ++it) - pinger->ping((char*)it->first.c_str(), false), it->second->incSent(); + pinger->ping((char*)it->first.c_str(), false); for(std::map::iterator it=v6_results.begin(); it!=v6_results.end(); ++it) - pinger->ping((char*)it->first.c_str(), true), it->second->incSent(); + pinger->ping((char*)it->first.c_str(), true); m.unlock(__FILE__, __LINE__); } @@ -133,7 +135,15 @@ void ContinuousPing::readPingResults() { #ifdef TRACE_PING ntop->getTrace()->traceEvent(TRACE_NORMAL, "%s() [IPv4] %s=%f", __FUNCTION__, it->first.c_str(), f); #endif - if(f != -1) it->second->update(f); + + if(f != -1) + it->second->update(f); + else { + it->second->incSent(); +#ifdef TRACE_PING_DROPS + ntop->getTrace()->traceEvent(TRACE_NORMAL, "[IPv4] Missing ping response for %s", it->first.c_str()); +#endif + } } for(std::map::iterator it=v6_results.begin(); it!=v6_results.end(); ++it) { @@ -142,7 +152,15 @@ void ContinuousPing::readPingResults() { #ifdef TRACE_PING ntop->getTrace()->traceEvent(TRACE_NORMAL, "%s() [IPv6] %s=%f", __FUNCTION__, it->first.c_str(), f); #endif - if(f != -1) it->second->update(f); + + if(f != -1) + it->second->update(f); + else { + it->second->incSent(); +#ifdef TRACE_PING_DROPS + ntop->getTrace()->traceEvent(TRACE_NORMAL, "[IPv6] Missing ping response for %s", it->first.c_str()); +#endif + } } m.unlock(__FILE__, __LINE__); diff --git a/src/ContinuousPingStats.cpp b/src/ContinuousPingStats.cpp new file mode 100644 index 0000000000..73eacdd314 --- /dev/null +++ b/src/ContinuousPingStats.cpp @@ -0,0 +1,54 @@ +/* + * + * (C) 2013-20 - ntop.org + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#ifndef WIN32 + +#include "ntop_includes.h" + +/* ***************************************** */ + +void ContinuousPingStats::update(float rtt) { + stats.num_ping_sent++, stats.num_ping_rcvd++; + + if(rtt > 0) { + stats.last_rtt = rtt; + stats.min_rtt = (stats.num_ping_rcvd == 1) ? rtt : min(stats.min_rtt, rtt); + stats.max_rtt = max(stats.max_rtt, rtt); + } +} + +/* ***************************************** */ + +float ContinuousPingStats::getSuccessRate(float *min_rtt, float *max_rtt) { + float pctg = min((stats.num_ping_sent == 0) ? 0 : (float)(stats.num_ping_rcvd*100)/(float)(stats.num_ping_sent), 100.f); + +#ifdef TRACE_PING + ntop->getTrace()->traceEvent(TRACE_NORMAL, "[sent: %u][rcvd: %u][RTT: %.3f/%.3f]", + stats.num_ping_sent, stats.num_ping_rcvd, + stats.min_rtt, stats.max_rtt); +#endif + + *min_rtt = stats.min_rtt, *max_rtt = stats.max_rtt; + + return(pctg); +} + +#endif /* WIN32 */ diff --git a/src/Ping.cpp b/src/Ping.cpp index bb693f0a25..048c1006e9 100644 --- a/src/Ping.cpp +++ b/src/Ping.cpp @@ -330,6 +330,7 @@ float Ping::getRTT(std::string who) { void Ping::cleanup() { m.lock(__FILE__, __LINE__); results.clear(); + ping_id = rand(), cnt = 0; m.unlock(__FILE__, __LINE__); }