diff --git a/include/L4Stats.h b/include/L4Stats.h new file mode 100644 index 0000000000..6ccb73491d --- /dev/null +++ b/include/L4Stats.h @@ -0,0 +1,21 @@ +#ifndef _L4_STATS_H_ +#define _L4_STATS_H_ + +class L4Stats { + private: + TrafficStats tcp_sent, tcp_rcvd; + TrafficStats udp_sent, udp_rcvd; + TrafficStats icmp_sent, icmp_rcvd; + TrafficStats other_ip_sent, other_ip_rcvd; + + public: + void luaStats(lua_State* vm); + void luaAnomalies(lua_State* vm); + void serialize(json_object *obj); + void deserialize(json_object *obj); + void incStats(time_t when, u_int8_t l4_proto, + u_int64_t rcvd_packets, u_int64_t rcvd_bytes, + u_int64_t sent_packets, u_int64_t sent_bytes); +}; + +#endif diff --git a/include/TimeseriesStats.h b/include/TimeseriesStats.h index 5c41f7b857..23e0ef2df2 100644 --- a/include/TimeseriesStats.h +++ b/include/TimeseriesStats.h @@ -33,10 +33,7 @@ class TimeseriesStats: public GenericTrafficElement { u_int32_t anomalous_flows_as_client, anomalous_flows_as_server; u_int32_t host_unreachable_flows_as_client, host_unreachable_flows_as_server; u_int64_t udp_sent_unicast, udp_sent_non_unicast; - TrafficStats tcp_sent, tcp_rcvd; - TrafficStats udp_sent, udp_rcvd; - TrafficStats icmp_sent, icmp_rcvd; - TrafficStats other_ip_sent, other_ip_rcvd; + L4Stats l4stats; public: TimeseriesStats(Host * _host); diff --git a/include/ntop_includes.h b/include/ntop_includes.h index 050c00f504..7e6c0e65dd 100644 --- a/include/ntop_includes.h +++ b/include/ntop_includes.h @@ -328,6 +328,7 @@ using namespace std; #include "Country.h" #include "MacStats.h" #include "Mac.h" +#include "L4Stats.h" #include "TimeseriesStats.h" #include "HostStats.h" #include "LocalHostStats.h" diff --git a/src/HostStats.cpp b/src/HostStats.cpp index b773091193..15de72c060 100644 --- a/src/HostStats.cpp +++ b/src/HostStats.cpp @@ -107,14 +107,7 @@ void HostStats::lua(lua_State* vm, bool mask_host, DetailsLevel details_level, b if(details_level >= details_higher) { /* Bytes anomalies */ - lua_push_uint64_table_entry(vm, "tcp.bytes.sent.anomaly_index", tcp_sent.getBytesAnomaly()); - lua_push_uint64_table_entry(vm, "tcp.bytes.rcvd.anomaly_index", tcp_sent.getBytesAnomaly()); - lua_push_uint64_table_entry(vm, "udp.bytes.sent.anomaly_index", udp_sent.getBytesAnomaly()); - lua_push_uint64_table_entry(vm, "udp.bytes.rcvd.anomaly_index", udp_sent.getBytesAnomaly()); - lua_push_uint64_table_entry(vm, "icmp.bytes.sent.anomaly_index", icmp_sent.getBytesAnomaly()); - lua_push_uint64_table_entry(vm, "icmp.bytes.rcvd.anomaly_index", icmp_sent.getBytesAnomaly()); - lua_push_uint64_table_entry(vm, "other_ip.bytes.sent.anomaly_index", other_ip_sent.getBytesAnomaly()); - lua_push_uint64_table_entry(vm, "other_ip.bytes.rcvd.anomaly_index", other_ip_sent.getBytesAnomaly()); + l4stats.luaAnomalies(vm); lua_push_uint64_table_entry(vm, "total_activity_time", total_activity_time); lua_push_uint64_table_entry(vm, "flows.as_client", getTotalNumFlowsAsClient()); @@ -141,14 +134,7 @@ bool HostStats::serializeCheckpoint(json_object *my_object, DetailsLevel details json_object_object_add(my_object, "flows.as_server", json_object_new_int(getTotalNumFlowsAsServer())); /* Protocol stats */ - json_object_object_add(my_object, "tcp_sent", tcp_sent.getJSONObject()); - json_object_object_add(my_object, "tcp_rcvd", tcp_rcvd.getJSONObject()); - json_object_object_add(my_object, "udp_sent", udp_sent.getJSONObject()); - json_object_object_add(my_object, "udp_rcvd", udp_rcvd.getJSONObject()); - json_object_object_add(my_object, "icmp_sent", icmp_sent.getJSONObject()); - json_object_object_add(my_object, "icmp_rcvd", icmp_rcvd.getJSONObject()); - json_object_object_add(my_object, "other_ip_sent", other_ip_sent.getJSONObject()); - json_object_object_add(my_object, "other_ip_rcvd", other_ip_rcvd.getJSONObject()); + l4stats.serialize(my_object); /* packet stats */ json_object_object_add(my_object, "pktStats.sent", sent_stats.getJSONObject()); @@ -224,28 +210,7 @@ void HostStats::incStats(time_t when, u_int8_t l4_proto, u_int ndpi_proto, total_activity_time += ntop->getPrefs()->get_housekeeping_frequency(), last_epoch_update = when; /* Packet stats sent_stats and rcvd_stats are incremented in Flow::incStats */ - - switch(l4_proto) { - case 0: - /* Unknown protocol */ - break; - case IPPROTO_UDP: - udp_rcvd.incStats(when, rcvd_packets, rcvd_bytes), - udp_sent.incStats(when, sent_packets, sent_bytes); - break; - case IPPROTO_TCP: - tcp_rcvd.incStats(when, rcvd_packets, rcvd_bytes), - tcp_sent.incStats(when, sent_packets, sent_bytes); - break; - case IPPROTO_ICMP: - icmp_rcvd.incStats(when, rcvd_packets, rcvd_bytes), - icmp_sent.incStats(when, sent_packets, sent_bytes); - break; - default: - other_ip_rcvd.incStats(when, rcvd_packets, rcvd_bytes), - other_ip_sent.incStats(when, sent_packets, sent_bytes); - break; - } + l4stats.incStats(when, l4_proto, rcvd_packets, rcvd_bytes, sent_packets, sent_bytes); } #ifdef NTOPNG_PRO diff --git a/src/L4Stats.cpp b/src/L4Stats.cpp new file mode 100644 index 0000000000..ef301dd416 --- /dev/null +++ b/src/L4Stats.cpp @@ -0,0 +1,115 @@ +/* + * + * (C) 2013-19 - ntop.org + * + *o + * 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. + * + */ + +#include "ntop_includes.h" + +/* **************************************************** */ + +void L4Stats::luaStats(lua_State* vm) { + lua_push_uint64_table_entry(vm, "tcp.packets.sent", tcp_sent.getNumPkts()); + lua_push_uint64_table_entry(vm, "tcp.packets.rcvd", tcp_rcvd.getNumPkts()); + lua_push_uint64_table_entry(vm, "tcp.bytes.sent", tcp_sent.getNumBytes()); + lua_push_uint64_table_entry(vm, "tcp.bytes.rcvd", tcp_rcvd.getNumBytes()); + + lua_push_uint64_table_entry(vm, "udp.packets.sent", udp_sent.getNumPkts()); + lua_push_uint64_table_entry(vm, "udp.bytes.sent", udp_sent.getNumBytes()); + lua_push_uint64_table_entry(vm, "udp.packets.rcvd", udp_rcvd.getNumPkts()); + lua_push_uint64_table_entry(vm, "udp.bytes.rcvd", udp_rcvd.getNumBytes()); + + lua_push_uint64_table_entry(vm, "icmp.packets.sent", icmp_sent.getNumPkts()); + lua_push_uint64_table_entry(vm, "icmp.bytes.sent", icmp_sent.getNumBytes()); + lua_push_uint64_table_entry(vm, "icmp.packets.rcvd", icmp_rcvd.getNumPkts()); + lua_push_uint64_table_entry(vm, "icmp.bytes.rcvd", icmp_rcvd.getNumBytes()); + + lua_push_uint64_table_entry(vm, "other_ip.packets.sent", other_ip_sent.getNumPkts()); + lua_push_uint64_table_entry(vm, "other_ip.bytes.sent", other_ip_sent.getNumBytes()); + lua_push_uint64_table_entry(vm, "other_ip.packets.rcvd", other_ip_rcvd.getNumPkts()); + lua_push_uint64_table_entry(vm, "other_ip.bytes.rcvd", other_ip_rcvd.getNumBytes()); +} + +/* **************************************************** */ + +void L4Stats::luaAnomalies(lua_State* vm) { + lua_push_uint64_table_entry(vm, "tcp.bytes.sent.anomaly_index", tcp_sent.getBytesAnomaly()); + lua_push_uint64_table_entry(vm, "tcp.bytes.rcvd.anomaly_index", tcp_rcvd.getBytesAnomaly()); + lua_push_uint64_table_entry(vm, "udp.bytes.sent.anomaly_index", udp_sent.getBytesAnomaly()); + lua_push_uint64_table_entry(vm, "udp.bytes.rcvd.anomaly_index", udp_rcvd.getBytesAnomaly()); + lua_push_uint64_table_entry(vm, "icmp.bytes.sent.anomaly_index", icmp_sent.getBytesAnomaly()); + lua_push_uint64_table_entry(vm, "icmp.bytes.rcvd.anomaly_index", icmp_rcvd.getBytesAnomaly()); + lua_push_uint64_table_entry(vm, "other_ip.bytes.sent.anomaly_index", other_ip_sent.getBytesAnomaly()); + lua_push_uint64_table_entry(vm, "other_ip.bytes.rcvd.anomaly_index", other_ip_rcvd.getBytesAnomaly()); +} + +/* **************************************************** */ + +void L4Stats::serialize(json_object *obj) { + json_object_object_add(obj, "tcp_sent", tcp_sent.getJSONObject()); + json_object_object_add(obj, "tcp_rcvd", tcp_rcvd.getJSONObject()); + json_object_object_add(obj, "udp_sent", udp_sent.getJSONObject()); + json_object_object_add(obj, "udp_rcvd", udp_rcvd.getJSONObject()); + json_object_object_add(obj, "icmp_sent", icmp_sent.getJSONObject()); + json_object_object_add(obj, "icmp_rcvd", icmp_rcvd.getJSONObject()); + json_object_object_add(obj, "other_ip_sent", other_ip_sent.getJSONObject()); + json_object_object_add(obj, "other_ip_rcvd", other_ip_rcvd.getJSONObject()); +} + +/* **************************************************** */ + +void L4Stats::deserialize(json_object *o) { + json_object *obj; + + if(json_object_object_get_ex(o, "tcp_sent", &obj)) tcp_sent.deserialize(obj); + if(json_object_object_get_ex(o, "tcp_rcvd", &obj)) tcp_rcvd.deserialize(obj); + if(json_object_object_get_ex(o, "udp_sent", &obj)) udp_sent.deserialize(obj); + if(json_object_object_get_ex(o, "udp_rcvd", &obj)) udp_rcvd.deserialize(obj); + if(json_object_object_get_ex(o, "icmp_sent", &obj)) icmp_sent.deserialize(obj); + if(json_object_object_get_ex(o, "icmp_rcvd", &obj)) icmp_rcvd.deserialize(obj); + if(json_object_object_get_ex(o, "other_ip_sent", &obj)) other_ip_sent.deserialize(obj); + if(json_object_object_get_ex(o, "other_ip_rcvd", &obj)) other_ip_rcvd.deserialize(obj); +} + +/* **************************************************** */ + +void L4Stats::incStats(time_t when, u_int8_t l4_proto, + u_int64_t rcvd_packets, u_int64_t rcvd_bytes, + u_int64_t sent_packets, u_int64_t sent_bytes) { + switch(l4_proto) { + case 0: + /* Unknown protocol */ + break; + case IPPROTO_UDP: + udp_rcvd.incStats(when, rcvd_packets, rcvd_bytes), + udp_sent.incStats(when, sent_packets, sent_bytes); + break; + case IPPROTO_TCP: + tcp_rcvd.incStats(when, rcvd_packets, rcvd_bytes), + tcp_sent.incStats(when, sent_packets, sent_bytes); + break; + case IPPROTO_ICMP: + icmp_rcvd.incStats(when, rcvd_packets, rcvd_bytes), + icmp_sent.incStats(when, sent_packets, sent_bytes); + break; + default: + other_ip_rcvd.incStats(when, rcvd_packets, rcvd_bytes), + other_ip_sent.incStats(when, sent_packets, sent_bytes); + break; + } +} diff --git a/src/LocalHostStats.cpp b/src/LocalHostStats.cpp index 8307786c20..65ebf25c58 100644 --- a/src/LocalHostStats.cpp +++ b/src/LocalHostStats.cpp @@ -142,15 +142,7 @@ void LocalHostStats::deserialize(json_object *o) { json_object *obj; HostStats::deserialize(o); - - if(json_object_object_get_ex(o, "tcp_sent", &obj)) tcp_sent.deserialize(obj); - if(json_object_object_get_ex(o, "tcp_rcvd", &obj)) tcp_rcvd.deserialize(obj); - if(json_object_object_get_ex(o, "udp_sent", &obj)) udp_sent.deserialize(obj); - if(json_object_object_get_ex(o, "udp_rcvd", &obj)) udp_rcvd.deserialize(obj); - if(json_object_object_get_ex(o, "icmp_sent", &obj)) icmp_sent.deserialize(obj); - if(json_object_object_get_ex(o, "icmp_rcvd", &obj)) icmp_rcvd.deserialize(obj); - if(json_object_object_get_ex(o, "other_ip_sent", &obj)) other_ip_sent.deserialize(obj); - if(json_object_object_get_ex(o, "other_ip_rcvd", &obj)) other_ip_rcvd.deserialize(obj); + l4stats.deserialize(obj); /* packet stats */ if(json_object_object_get_ex(o, "pktStats.sent", &obj)) sent_stats.deserialize(obj); diff --git a/src/TimeseriesStats.cpp b/src/TimeseriesStats.cpp index a2d54d6f5c..d9bf59f6dc 100644 --- a/src/TimeseriesStats.cpp +++ b/src/TimeseriesStats.cpp @@ -63,26 +63,7 @@ void TimeseriesStats::luaStats(lua_State* vm, NetworkInterface *iface, bool host lua_push_uint64_table_entry(vm, "contacts.as_server", host->getNumActiveContactsAsServer()); lua_push_uint64_table_entry(vm, "total_alerts", total_alerts); - lua_push_uint64_table_entry(vm, "tcp.packets.sent", tcp_sent.getNumPkts()); - lua_push_uint64_table_entry(vm, "tcp.packets.rcvd", tcp_rcvd.getNumPkts()); - lua_push_uint64_table_entry(vm, "tcp.bytes.sent", tcp_sent.getNumBytes()); - lua_push_uint64_table_entry(vm, "tcp.bytes.rcvd", tcp_rcvd.getNumBytes()); - - lua_push_uint64_table_entry(vm, "udp.packets.sent", udp_sent.getNumPkts()); - lua_push_uint64_table_entry(vm, "udp.bytes.sent", udp_sent.getNumBytes()); - lua_push_uint64_table_entry(vm, "udp.packets.rcvd", udp_rcvd.getNumPkts()); - lua_push_uint64_table_entry(vm, "udp.bytes.rcvd", udp_rcvd.getNumBytes()); - - lua_push_uint64_table_entry(vm, "icmp.packets.sent", icmp_sent.getNumPkts()); - lua_push_uint64_table_entry(vm, "icmp.bytes.sent", icmp_sent.getNumBytes()); - lua_push_uint64_table_entry(vm, "icmp.packets.rcvd", icmp_rcvd.getNumPkts()); - lua_push_uint64_table_entry(vm, "icmp.bytes.rcvd", icmp_rcvd.getNumBytes()); - - lua_push_uint64_table_entry(vm, "other_ip.packets.sent", other_ip_sent.getNumPkts()); - lua_push_uint64_table_entry(vm, "other_ip.bytes.sent", other_ip_sent.getNumBytes()); - lua_push_uint64_table_entry(vm, "other_ip.packets.rcvd", other_ip_rcvd.getNumPkts()); - lua_push_uint64_table_entry(vm, "other_ip.bytes.rcvd", other_ip_rcvd.getNumBytes()); - + l4stats.luaStats(vm); lua_push_uint64_table_entry(vm, "udpBytesSent.unicast", udp_sent_unicast); lua_push_uint64_table_entry(vm, "udpBytesSent.non_unicast", udp_sent_non_unicast);