Fixed ASN sankey and added total-traffic ASN sankey (#9313)

* Fixed ASN sankey and added total-traffic ASN sankey

* Minor fixes for the ASN sankey
This commit is contained in:
Manuel Ceroni 2025-06-26 14:49:21 +02:00 committed by GitHub
parent f6ad1aa70f
commit dc4c5ece2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 90 additions and 51 deletions

View file

@ -22,11 +22,19 @@ local rsp = {}
local edges = {}
local nodes = {}
local unit
local traffic_criteria = {
INGRESS = 0,
EGRESS = 1,
TOTAL = 2,
}
local criteria
if criteria_as == "egress_traffic_criteria" then
unit = "Egress"
criteria = traffic_criteria.EGRESS
elseif criteria_as == "total_traffic_criteria" then
criteria = traffic_criteria.TOTAL
else
unit = "Ingress"
criteria = traffic_criteria.INGRESS
end
-- ################################################
@ -87,60 +95,26 @@ end
-- ####################
local tot_bytes = {}
local tot_bytes_exporter = {}
function callback (_, flow)
-- tprint(flow) io.write("-------------------------------\n")
local exporter_ip = getProbeName(flow.device_ip) or "unknown"
local bytes
local exporter_node_id = find_node_id(exporter_ip)
local port_index = format_portidx_name(flow.device_ip, flow.in_index) or "?"
local n_id = exporter_ip .. "@" .. port_index
-- TODO: handle all directions
if(tot_bytes[n_id] == nil) then tot_bytes[n_id] = {
sent = 0, rcvd = 0, exp_ip = exporter_ip, port = port_index
} end
if(tot_bytes_exporter[exporter_ip] == nil) then tot_bytes_exporter[exporter_ip] = { sent = 0, rcvd = 0 } end
if unit == "Ingress" and flow.dst_as == asn then
local port_index = format_portidx_name(flow.device_ip, flow.in_index) or "?"
local n_id = exporter_ip .. "@" .. port_index
local port_node_id = find_node_id(n_id)
bytes = tonumber(flow.bytes_sent) -- TODO haandle directions
add_unique_node(exporter_node_id, exporter_ip, "#")
add_unique_node(port_node_id, port_index, "#")
if(tot_bytes[n_id] == nil) then tot_bytes[n_id] = { sent = 0, rcvd = 0 } end
-- TODO sum bytes
table.insert(links, {
source_node_id = port_node_id,
target_node_id = exporter_node_id,
value = bytes -- TODO set bytes total
})
table.insert(links, {
source_node_id = exporter_node_id,
target_node_id = as_root_key,
value = bytes -- TODO set bytes total
})
if(flow.src_as == asn) then
tot_bytes[n_id].sent = tot_bytes[n_id].sent + flow.bytes_sent
tot_bytes_exporter[exporter_ip].sent = tot_bytes_exporter[exporter_ip].sent + flow.bytes_sent
end
if unit == "Egress" and flow.src_as == asn then
local port_index = format_portidx_name(flow.device_ip, flow.out_index) or "?"
local n_id = exporter_ip .. "@" .. port_index
local port_node_id = find_node_id(n_id)
bytes = tonumber(flow.bytes_rcvd) or 0
add_unique_node(exporter_node_id, exporter_ip, "#")
add_unique_node(port_node_id, port_index, "#")
table.insert(links, {
source_node_id = as_root_key,
target_node_id = exporter_node_id,
value = bytes
})
table.insert(links, {
source_node_id = exporter_node_id,port_node_id,
target_node_id = port_node_id,
value = bytes
})
if(flow.dst_as == asn) then
tot_bytes[n_id].rcvd = tot_bytes[n_id].rcvd + flow.bytes_rcvd
tot_bytes_exporter[exporter_ip].rcvd = tot_bytes_exporter[exporter_ip].rcvd + flow.bytes_rcvd
end
end
@ -149,6 +123,69 @@ callback_utils.foreachFlow(ifid,
os.time()+30, -- deadline
callback, flows_filter)
local exporter_nodes = {}
for n_id, data in pairs(tot_bytes) do
if (criteria == traffic_criteria.INGRESS and data.sent > 0) or
(criteria == traffic_criteria.EGRESS and data.rcvd > 0) or
(criteria == traffic_criteria.TOTAL and data.rcvd+data.sent > 0) then
local exporter_ip = data.exp_ip
local port_index = data.port
local exporter_node_id = find_node_id(exporter_ip)
if(exporter_nodes[exporter_ip] == nil) then exporter_nodes[exporter_ip] = exporter_node_id end
local port_node_id = find_node_id(n_id)
add_unique_node(exporter_node_id, exporter_ip, "#")
add_unique_node(port_node_id, port_index, "#")
if criteria == traffic_criteria.INGRESS then
table.insert(links, {
source_node_id = port_node_id,
target_node_id = exporter_node_id,
label = bytesToSize(data.sent),
value = data.sent
})
elseif criteria == traffic_criteria.EGRESS then
table.insert(links, {
source_node_id = exporter_node_id,
target_node_id = port_node_id,
label = bytesToSize(data.rcvd),
value = data.rcvd
})
else
table.insert(links, {
source_node_id = port_node_id,
target_node_id = exporter_node_id,
label = bytesToSize(data.rcvd+data.sent),
value = data.rcvd+data.sent
})
end
end
end
for exporter_ip, exporter_node_id in pairs(exporter_nodes) do
local sent = tot_bytes_exporter[exporter_ip].sent
local rcvd = tot_bytes_exporter[exporter_ip].rcvd
if criteria == traffic_criteria.INGRESS and sent > 0 then
table.insert(links, {
source_node_id = exporter_node_id,
target_node_id = as_root_key,
label = bytesToSize(sent),
value = sent
})
elseif criteria == traffic_criteria.EGRESS and rcvd > 0 then
table.insert(links, {
source_node_id = as_root_key,
target_node_id = exporter_node_id,
label = bytesToSize(rcvd),
value = rcvd
})
else
table.insert(links, {
source_node_id = exporter_node_id,
target_node_id = as_root_key,
label = bytesToSize(rcvd+sent),
value = rcvd+sent
})
end
end
rsp["nodes"] = nodes
rsp["links"] = links