1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
/*
*
* (C) 2013-22 - 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 _NDPI_STATS_H_
#define _NDPI_STATS_H_
#include "ntop_includes.h"
#define MAX_NDPI_PROTOS (NDPI_MAX_SUPPORTED_PROTOCOLS + NDPI_MAX_NUM_CUSTOM_PROTOCOLS + 1)
/* *************************************** */
typedef struct {
u_int64_t sent, rcvd;
} TrafficCounter;
typedef struct {
TrafficCounter packets, bytes;
u_int32_t duration /* sec */, last_epoch_update; /* useful to avoid multiple updates */
u_int32_t total_flows;
} ProtoCounter;
typedef struct {
TrafficCounter bytes;
u_int32_t duration /* sec */, last_epoch_update; /* useful to avoid multiple updates */
} CategoryCounter;
class NetworkInterface;
class ThroughputStats;
/* *************************************** */
class nDPIStats {
private:
#ifdef NTOPNG_PRO
time_t nextMinPeriodicUpdate;
BehaviorAnalysis **behavior_bytes_traffic;
#endif
ProtoCounter *counters[MAX_NDPI_PROTOS];
ThroughputStats **bytes_thpt;
/* NOTE: category counters are not dumped to redis right now, they are only used internally */
CategoryCounter cat_counters[NDPI_PROTOCOL_NUM_CATEGORIES];
public:
nDPIStats(bool enable_throughput_stats = false, bool enable_behavior_stats = false);
nDPIStats(const nDPIStats &stats);
~nDPIStats();
void updateStats(const struct timeval *tv);
void incStats(u_int32_t when, u_int16_t proto_id,
u_int64_t sent_packets, u_int64_t sent_bytes,
u_int64_t rcvd_packets, u_int64_t rcvd_bytes);
void incCategoryStats(u_int32_t when, ndpi_protocol_category_t category_id,
u_int64_t sent_bytes, u_int64_t rcvd_bytes);
void incFlowsStats(u_int16_t proto_id);
void print(NetworkInterface *iface);
void lua(NetworkInterface *iface, lua_State* vm, bool with_categories = false, bool tsLua = false, bool diff = false);
char* serialize(NetworkInterface *iface);
json_object* getJSONObject(NetworkInterface *iface);
void deserialize(NetworkInterface *iface, json_object *o);
void sum(nDPIStats *s) const;
inline u_int64_t getProtoBytes(u_int16_t proto_id) {
if((proto_id < MAX_NDPI_PROTOS) && counters[proto_id]) {
TrafficCounter *tc = &counters[proto_id]->bytes;
return(tc ? tc->sent+tc->rcvd : 0);
} else
return(0);
}
inline u_int32_t getProtoDuration(u_int16_t proto_id) {
if((proto_id < MAX_NDPI_PROTOS) && counters[proto_id])
return counters[proto_id]->duration;
else
return(0);
}
inline u_int64_t getCategoryBytes(ndpi_protocol_category_t category_id) {
if (category_id < NDPI_PROTOCOL_NUM_CATEGORIES)
return(cat_counters[category_id].bytes.sent + cat_counters[category_id].bytes.rcvd);
else
return(0);
}
inline u_int32_t getCategoryDuration(ndpi_protocol_category_t category_id) {
if (category_id < NDPI_PROTOCOL_NUM_CATEGORIES)
return(cat_counters[category_id].duration);
else
return(0);
}
void resetStats();
};
#endif /* _NDPI_STATS_H_ */
|