Skip to content

Commit 1616687

Browse files
committed
Test top_n IPs for a given interface (traffic in/out)
1 parent 65ff85d commit 1616687

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

netflowbot.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def perform_job(*args, **job_params):
9696
# Traffic in and out: (per interface)
9797
values.extend(NetFlowBot.get_values_traffic_in(output_path_prefix, two_minutes_ago, minute_ago))
9898
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))
99101

100102
if not values:
101103
log.warning("No values found to be sent to Grafolean")
@@ -167,6 +169,41 @@ def get_values_traffic_out(output_path_prefix, from_time, to_time):
167169
})
168170
return values
169171

172+
@staticmethod
173+
def get_top_N_IPs(output_path_prefix, from_time, to_time, interface_index, is_direction_in=True):
174+
with db.cursor() as c:
175+
# TODO: missing check for IP: r.client_ip = %s AND
176+
c.execute(f"""
177+
SELECT
178+
f.data->'IPV4_DST_ADDR',
179+
sum((f.data->'IN_BYTES')::integer) "traffic"
180+
FROM
181+
netflow_records "r",
182+
netflow_flows "f"
183+
WHERE
184+
r.ts >= %s AND
185+
r.ts < %s AND
186+
r.seq = f.record AND
187+
(f.data->'{'INPUT_SNMP' if is_direction_in else 'OUTPUT_SNMP'}')::integer = %s AND
188+
(f.data->'DIRECTION')::integer = {'0' if is_direction_in else '1'}
189+
GROUP BY
190+
f.data->'IPV4_DST_ADDR'
191+
ORDER BY
192+
traffic desc
193+
LIMIT 10;
194+
""", (from_time, to_time, interface_index,))
195+
196+
#SELECT f.data->'IPV4_DST_ADDR', sum((f.data->'IN_BYTES')::integer) "traffic" FROM netflow_records "r", netflow_flows "f" WHERE r.ts >= now() - interval '1 minute' AND r.seq = f.record AND (f.data->'INPUT_SNMP')::integer = 18 AND (f.data->'DIRECTION')::integer = '0' GROUP BY f.data->'IPV4_DST_ADDR' ORDER BY traffic desc LIMIT 10;
197+
198+
values = []
199+
for top_ip, traffic_bytes in c.fetchall():
200+
output_path = f"{output_path_prefix}.topip.{'in' if is_direction_in else 'out'}.{interface_index}.if{interface_index}.{top_ip}"
201+
values.append({
202+
'p': output_path,
203+
'v': traffic_bytes / 60., # Bps
204+
})
205+
return values
206+
170207

171208
def wait_for_grafolean(backend_url):
172209
while True:

0 commit comments

Comments
 (0)