Skip to content

Commit 5a24ad8

Browse files
committed
Add daily and hourly (test) stats
1 parent b8f486b commit 5a24ad8

File tree

1 file changed

+63
-10
lines changed

1 file changed

+63
-10
lines changed

netflowbot.py

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,42 @@ def jobs(self):
5858
}
5959
yield job_id, intervals, NetFlowBot.perform_job, job_params
6060

61+
job_id = '1h'
62+
intervals = [3600]
63+
job_params = {
64+
"job_id": job_id,
65+
"entity_info": {
66+
"account_id": 129104112,
67+
"entity_id": 236477687,
68+
"entity_type": "device",
69+
"details": {
70+
"ipv4": "1.2.3.4"
71+
},
72+
},
73+
"backend_url": self.backend_url,
74+
"bot_token": self.bot_token,
75+
}
76+
yield job_id, intervals, NetFlowBot.perform_job, job_params
77+
78+
job_id = 'daily'
79+
intervals = [3600 * 24]
80+
job_params = {
81+
"job_id": job_id,
82+
"entity_info": {
83+
"account_id": 129104112,
84+
"entity_id": 236477687,
85+
"entity_type": "device",
86+
"details": {
87+
"ipv4": "1.2.3.4"
88+
},
89+
},
90+
"backend_url": self.backend_url,
91+
"bot_token": self.bot_token,
92+
}
93+
yield job_id, intervals, NetFlowBot.perform_job, job_params
94+
95+
96+
6197
# This method is called whenever the job needs to be done. It gets the parameters and performs fetching of data.
6298
@staticmethod
6399
def perform_job(*args, **job_params):
@@ -86,18 +122,35 @@ def perform_job(*args, **job_params):
86122
# }
87123
# https://www.cisco.com/en/US/technologies/tk648/tk362/technologies_white_paper09186a00800a3db9.html#wp9001622
88124

125+
affecting_intervals, = args
126+
values = []
89127
entity_info = job_params["entity_info"]
90-
output_path_prefix = f'entity.{entity_info["entity_id"]}.netflow'
91128

92-
minute_ago = datetime.now() - timedelta(minutes=1)
93-
two_minutes_ago = minute_ago - timedelta(minutes=1)
129+
if 60 in affecting_intervals:
130+
output_path_prefix = f'entity.{entity_info["entity_id"]}.netflow.traffic_in'
94131

95-
values = []
96-
# Traffic in and out: (per interface)
97-
values.extend(NetFlowBot.get_values_traffic_in(output_path_prefix, two_minutes_ago, minute_ago))
98-
values.extend(NetFlowBot.get_values_traffic_out(output_path_prefix, two_minutes_ago, minute_ago))
99-
values.extend(NetFlowBot.get_top_N_IPs(output_path_prefix, two_minutes_ago, minute_ago, 18, is_direction_in=True))
100-
values.extend(NetFlowBot.get_top_N_IPs(output_path_prefix, two_minutes_ago, minute_ago, 18, is_direction_in=False))
132+
minute_ago = datetime.now() - timedelta(minutes=1)
133+
two_minutes_ago = minute_ago - timedelta(minutes=1)
134+
135+
# Traffic in and out: (per interface)
136+
values.extend(NetFlowBot.get_values_traffic_in(output_path_prefix, two_minutes_ago, minute_ago))
137+
values.extend(NetFlowBot.get_values_traffic_out(output_path_prefix, two_minutes_ago, minute_ago))
138+
values.extend(NetFlowBot.get_top_N_IPs(output_path_prefix, two_minutes_ago, minute_ago, 18, is_direction_in=True))
139+
values.extend(NetFlowBot.get_top_N_IPs(output_path_prefix, two_minutes_ago, minute_ago, 18, is_direction_in=False))
140+
141+
# every hour, collect stats for the whole hour:
142+
if 3600 in affecting_intervals:
143+
output_path_prefix_1hour = f'entity.{entity_info["entity_id"]}.netflow.traffic.in.1hour'
144+
hour_ago = minute_ago - timedelta(hours=1)
145+
values.extend(NetFlowBot.get_top_N_IPs(output_path_prefix_1hour, hour_ago, minute_ago, 18, is_direction_in=True))
146+
values.extend(NetFlowBot.get_top_N_IPs(output_path_prefix_1hour, hour_ago, minute_ago, 18, is_direction_in=False))
147+
148+
# every 24h, also collect stats for the whole day:
149+
if 3600 * 24 in affecting_intervals:
150+
output_path_prefix_1day = f'entity.{entity_info["entity_id"]}.netflow.traffic.in.1day'
151+
day_ago = minute_ago - timedelta(days=1)
152+
values.extend(NetFlowBot.get_top_N_IPs(output_path_prefix_1day, day_ago, minute_ago, 18, is_direction_in=True))
153+
values.extend(NetFlowBot.get_top_N_IPs(output_path_prefix_1day, day_ago, minute_ago, 18, is_direction_in=False))
101154

102155
if not values:
103156
log.warning("No values found to be sent to Grafolean")
@@ -133,7 +186,7 @@ def get_values_traffic_in(output_path_prefix, from_time, to_time):
133186

134187
values = []
135188
for interface_index, traffic_bytes in c.fetchall():
136-
output_path = f'{output_path_prefix}.traffic_in.{interface_index}.if{interface_index}'
189+
output_path = f'{output_path_prefix}.{interface_index}.if{interface_index}'
137190
values.append({
138191
'p': output_path,
139192
'v': traffic_bytes / 60., # Bps

0 commit comments

Comments
 (0)