/src/ntopng/include/NetworkStats.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * (C) 2015-26 - ntop.org |
4 | | * |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, write to the Free Software Foundation, |
18 | | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 | | * |
20 | | */ |
21 | | |
22 | | #ifndef _NETWORK_STATS_H_ |
23 | | #define _NETWORK_STATS_H_ |
24 | | |
25 | | #include "ntop_includes.h" |
26 | | |
27 | | class NetworkStats : public InterfaceMemberAlertableEntity, |
28 | | public GenericTrafficElement, |
29 | | public Score { |
30 | | private: |
31 | | u_int32_t network_id; |
32 | | u_int32_t numHosts; |
33 | | u_int32_t alerted_flows_as_client, alerted_flows_as_server; |
34 | | TrafficStats ingress, ingress_broadcast; /* outside -> network */ |
35 | | TrafficStats egress, egress_broadcast; /* network -> outside */ |
36 | | TrafficStats inner, inner_broadcast; /* network -> network (local traffic) */ |
37 | | TcpPacketStats tcp_packet_stats_ingress, tcp_packet_stats_egress, |
38 | | tcp_packet_stats_inner; |
39 | | AlertCounter syn_flood_victim_alert; |
40 | | AlertCounter flow_flood_victim_alert; |
41 | | u_int32_t syn_recvd_last_min, |
42 | | synack_sent_last_min; /* syn scan counters (victim) */ |
43 | | u_int32_t round_trip_time; |
44 | | |
45 | | #if defined(NTOPNG_PRO) |
46 | | QoEStats qoe_stats; |
47 | | InOutTraffic* network_matrix; |
48 | | time_t nextMinPeriodicUpdate; |
49 | | #endif |
50 | | |
51 | | static inline void incTcp(TcpPacketStats* tps, u_int32_t ooo_pkts, |
52 | | u_int32_t retr_pkts, u_int32_t lost_pkts, |
53 | 721 | u_int32_t keep_alive_pkts) { |
54 | 721 | if (ooo_pkts) tps->incOOO(ooo_pkts); |
55 | 721 | if (retr_pkts) tps->incRetr(retr_pkts); |
56 | 721 | if (lost_pkts) tps->incLost(lost_pkts); |
57 | 721 | if (keep_alive_pkts) tps->incKeepAlive(keep_alive_pkts); |
58 | 721 | } |
59 | | |
60 | | #ifdef NTOPNG_PRO |
61 | | void updateBehaviorStats(const struct timeval* tv); |
62 | | #endif |
63 | | |
64 | | public: |
65 | | NetworkStats(NetworkInterface* iface, u_int32_t _network_id); |
66 | | virtual ~NetworkStats(); |
67 | | |
68 | 0 | inline bool trafficSeen() { |
69 | 0 | return ingress.getNumPkts() || egress.getNumPkts() || inner.getNumPkts(); |
70 | 0 | }; |
71 | | |
72 | | inline void incIngress(time_t t, u_int64_t num_pkts, u_int64_t num_bytes, |
73 | 7.59k | bool broadcast) { |
74 | 7.59k | rcvd.incStats(t, num_pkts, num_bytes); |
75 | 7.59k | ingress.incStats(t, num_pkts, num_bytes); |
76 | 7.59k | if (broadcast) ingress_broadcast.incStats(t, num_pkts, num_bytes); |
77 | 7.59k | }; |
78 | | |
79 | | inline void incEgress(time_t t, u_int64_t num_pkts, u_int64_t num_bytes, |
80 | 7.59k | bool broadcast) { |
81 | 7.59k | sent.incStats(t, num_pkts, num_bytes); |
82 | 7.59k | egress.incStats(t, num_pkts, num_bytes); |
83 | 7.59k | if (broadcast) egress_broadcast.incStats(t, num_pkts, num_bytes); |
84 | 7.59k | }; |
85 | | |
86 | | inline void incInner(time_t t, u_int64_t num_pkts, u_int64_t num_bytes, |
87 | 2.22k | bool broadcast) { |
88 | 2.22k | sent.incStats(t, num_pkts, num_bytes); |
89 | 2.22k | inner.incStats(t, num_pkts, num_bytes); |
90 | 2.22k | if (broadcast) inner_broadcast.incStats(t, num_pkts, num_bytes); |
91 | 2.22k | }; |
92 | | |
93 | | inline void incIngressTcp(u_int32_t ooo_pkts, u_int32_t retr_pkts, |
94 | 213 | u_int32_t lost_pkts, u_int32_t keep_alive_pkts) { |
95 | 213 | incTcp(&tcp_packet_stats_ingress, ooo_pkts, retr_pkts, lost_pkts, |
96 | 213 | keep_alive_pkts); |
97 | 213 | }; |
98 | | |
99 | | inline void incEgressTcp(u_int32_t ooo_pkts, u_int32_t retr_pkts, |
100 | 156 | u_int32_t lost_pkts, u_int32_t keep_alive_pkts) { |
101 | 156 | incTcp(&tcp_packet_stats_egress, ooo_pkts, retr_pkts, lost_pkts, |
102 | 156 | keep_alive_pkts); |
103 | 156 | }; |
104 | | |
105 | | inline void incInnerTcp(u_int32_t ooo_pkts, u_int32_t retr_pkts, |
106 | 352 | u_int32_t lost_pkts, u_int32_t keep_alive_pkts) { |
107 | 352 | incTcp(&tcp_packet_stats_inner, ooo_pkts, retr_pkts, lost_pkts, |
108 | 352 | keep_alive_pkts); |
109 | 352 | }; |
110 | | |
111 | | #ifdef NTOPNG_PRO |
112 | | void incTrafficBetweenNets(u_int32_t net_id, u_int32_t bytes_sent, |
113 | | u_int32_t bytes_rcvd); |
114 | | void resetTrafficBetweenNets(); |
115 | | #endif |
116 | | |
117 | 5.09k | inline void incNumHosts() { numHosts++; }; |
118 | 0 | inline void incNumAlertedFlows(bool as_client) { |
119 | 0 | if (as_client) |
120 | 0 | alerted_flows_as_client++; |
121 | 0 | else |
122 | 0 | alerted_flows_as_server++; |
123 | 0 | }; |
124 | 0 | inline void decNumHosts() { numHosts--; }; |
125 | 0 | inline u_int32_t getNumHosts() const { return numHosts; }; |
126 | 0 | inline u_int32_t getTotalAlertedNumFlowsAsClient() const { |
127 | 0 | return (alerted_flows_as_client); |
128 | 0 | }; |
129 | 0 | inline u_int32_t getTotalAlertedNumFlowsAsServer() const { |
130 | 0 | return (alerted_flows_as_server); |
131 | 0 | }; |
132 | | |
133 | | void setNetworkId(u_int8_t id); |
134 | | bool match(AddressTree* tree); |
135 | | void lua(lua_State* vm, bool diff = false, bool fullStats = true); |
136 | | bool serialize(json_object* my_object); |
137 | | void housekeepAlerts(ScriptPeriodicity p); |
138 | | |
139 | | virtual void updateStats(const struct timeval* tv); |
140 | | |
141 | | void updateSynAlertsCounter(time_t when, bool syn_sent); |
142 | | void updateSynAckAlertsCounter(time_t when, bool synack_sent); |
143 | | void updateRoundTripTime(u_int32_t rtt_msecs); |
144 | | void incNumFlows(time_t t, bool as_client); |
145 | | #ifdef NTOPNG_PRO |
146 | | void incQoEStats(QoEType qoe_type) { qoe_stats.incQoEStats(qoe_type); }; |
147 | | #endif |
148 | | }; |
149 | | |
150 | | #endif /* _NETWORK_STATS_H_ */ |