Skip to content

Commit faa0b3f

Browse files
committed
Add top N dstports reporting to netflowbot
1 parent c1e622d commit faa0b3f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

netflowbot.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def job_perform_account_aggr(*args, **job_params):
241241
values.extend(NetFlowBot.get_top_N_protocols_for_entity(interval_label, last_used_ts, max_ts, time_between, direction, entity_id, entity_ip))
242242
values.extend(NetFlowBot.get_top_N_protocols_for_entity_interfaces(interval_label, last_used_ts, max_ts, time_between, direction, entity_id, entity_ip))
243243
values.extend(NetFlowBot.get_top_N_connections_for_entity(interval_label, last_used_ts, max_ts, time_between, direction, entity_id, entity_ip))
244+
values.extend(NetFlowBot.get_top_N_dstports_for_entity(interval_label, last_used_ts, max_ts, time_between, direction, entity_id, entity_ip))
244245

245246
if not values:
246247
log.warning("No values found to be sent to Grafolean")
@@ -519,6 +520,39 @@ def get_top_N_connections_for_entity(interval_label, last_used_ts, max_ts, time_
519520

520521
return values
521522

523+
@staticmethod
524+
@slow_down
525+
def get_top_N_dstports_for_entity(interval_label, last_used_ts, max_ts, time_between, direction, entity_id, entity_ip):
526+
with get_db_cursor() as c:
527+
values = []
528+
c.execute(f"""
529+
SELECT
530+
f.l4_dst_port,
531+
sum(f.in_bytes) "traffic"
532+
FROM
533+
{DB_PREFIX}flows2 "f"
534+
WHERE
535+
f.client_ip = %s AND
536+
f.ts > %s AND
537+
f.ts <= %s AND
538+
f.direction = %s
539+
GROUP BY
540+
f.l4_dst_port
541+
ORDER BY
542+
traffic desc
543+
LIMIT {TOP_N_MAX};
544+
""", (entity_ip, last_used_ts, max_ts, direction,))
545+
546+
output_path_entity = NetFlowBot.construct_output_path_prefix(interval_label, direction, entity_id, interface=None)
547+
for l4_dst_port, traffic_bytes in c.fetchall():
548+
output_path = f"{output_path_entity}.topdstports.{path_part_encode(l4_dst_port)}"
549+
values.append({
550+
'p': output_path,
551+
'v': traffic_bytes / time_between, # Bps
552+
})
553+
554+
return values
555+
522556
# @staticmethod
523557
# @slow_down
524558
# def get_top_N_protocols(output_path_prefix, from_time, to_time, interface_index, is_direction_in=True):

0 commit comments

Comments
 (0)