Update aggregated live flows analysis page (#7233) (#7293)

* Update aggregated live analysis flows page with server side pagination. (#7233)

* Lua rest refactor.
This commit is contained in:
Nicolò Maio 2023-03-08 17:03:57 +01:00 committed by GitHub
parent 0af065ef69
commit 628d1adb47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 488 additions and 228 deletions

View file

@ -5,67 +5,46 @@
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
-- ############################################
-- Requires
require "lua_utils"
local rest_utils = require("rest_utils")
-- ############################################
local rc = rest_utils.consts.success.ok
local res = {}
local debugger = false
local ifid = _GET["ifid"]
local vlan = _GET["vlan_id"]
local criteria = _GET["aggregation_criteria"] or ""
local function build_response()
-- ############################################
-- Set up input variables
local ifid = _GET["ifid"]
local vlan = _GET["vlan_id"]
local criteria = _GET["aggregation_criteria"] or ""
if isEmptyString(ifid) then
rc = rest_utils.consts.err.invalid_interface
rest_utils.answer(rc)
return
end
local filters = {}
filters["page"] = tonumber(_GET["draw"] or 0)
filters["sort_column"] = _GET["sort"]
filters["sort_order"] = _GET["order"] or 'asc'
filters["start"] = tonumber(_GET["start"] or 0)
filters["length"] = tonumber(_GET["length"] or 10)
-- ############################################
if isEmptyString(criteria) or criteria == "application_protocol" then
interface.select(ifid)
local aggregated_info = interface.getProtocolFlowsStats(1)
for _, data in pairs(aggregated_info) do
if vlan and not isEmptyString(vlan) and tonumber(vlan) ~= tonumber(data.vlan_id) then
goto continue
end
local bytes_sent = data.bytes_sent
local bytes_rcvd = data.bytes_rcvd
local total_bytes = bytes_rcvd + bytes_sent
res[#res + 1] = {
flows = format_high_num_value_for_tables(data, 'num_flows'),
application = {
label = data.proto_name,
id = data.proto_id,
},
breakdown = {
percentage_bytes_sent = (bytes_sent * 100) / total_bytes,
percentage_bytes_rcvd = (bytes_rcvd * 100) / total_bytes,
},
bytes_rcvd = bytes_rcvd,
bytes_sent = bytes_sent,
tot_traffic = total_bytes,
tot_score = format_high_num_value_for_tables(data, 'total_score'),
num_servers = format_high_num_value_for_tables(data, 'num_servers'),
num_clients = format_high_num_value_for_tables(data, 'num_clients'),
vlan_id = {
id = data.vlan_id,
label = data.vlan_id
}
}
::continue::
local rc = rest_utils.consts.success.ok
if isEmptyString(ifid) then
rc = rest_utils.consts.err.invalid_interface
rest_utils.answer(rc)
return
end
else
interface.select(ifid)
local res = {}
local criteria_type_id = 1
local num_entries = 0
-- Discovery analysis type
local criteria_type_id = 1
-- by default -> application_protocol
if criteria == "client" then
criteria_type_id = 2
elseif criteria == "server" then
@ -73,51 +52,148 @@ else
elseif criteria == "client_server" then
criteria_type_id = 4
end
local aggregated_info = interface.getProtocolFlowsStats(criteria_type_id)
interface.select(ifid)
for _, data in pairs(aggregated_info) do
local x = 0
local bytes_sent = data.bytes_sent
local bytes_rcvd = data.bytes_rcvd
local total_bytes = bytes_rcvd + bytes_sent
-- retrieve stats
local aggregated_info = interface.getProtocolFlowsStats(criteria_type_id,
filters["page"],
filters["sort_column"],
filters["sort_order"],
filters["start"],
filters["length"])
if vlan and not isEmptyString(vlan) and tonumber(vlan) ~= tonumber(data.vlan_id) then
goto continue
if( criteria_type_id == 1) then
if(debugger and x < 2 and filters["sort_column"] == "application") then
tprint("FROM C++")
tprint(aggregated_info)
tprint("---------------------------------")
x = x + 1
end
-- application_protocol case
local client_ip = ternary(tonumber(data.vlan_id) ~= 0, string.format("%s@%s",data.client_ip,data.vlan_id), data.client_ip)
local server_ip = ternary(tonumber(data.vlan_id) ~= 0, string.format("%s@%s",data.server_ip,data.vlan_id), data.server_ip)
for _, data in pairs(aggregated_info) do
if vlan and not isEmptyString(vlan) and tonumber(vlan) ~= tonumber(data.vlan_id) then
goto continue
end
num_entries = data.num_entries
res[#res + 1] = {
flows = format_high_num_value_for_tables(data, 'num_flows'),
client = {
label = client_ip,
id = client_ip,
},
server = {
label = server_ip,
id = server_ip,
},
breakdown = {
percentage_bytes_sent = (bytes_sent * 100) / total_bytes,
percentage_bytes_rcvd = (bytes_rcvd * 100) / total_bytes,
},
bytes_rcvd = bytes_rcvd,
bytes_sent = bytes_sent,
tot_traffic = total_bytes,
tot_score = format_high_num_value_for_tables(data, 'total_score'),
num_servers = format_high_num_value_for_tables(data, 'num_servers'),
num_clients = format_high_num_value_for_tables(data, 'num_clients'),
vlan_id = {
id = data.vlan_id,
label = data.vlan_id
}
}
local bytes_sent = data.bytes_sent
local bytes_rcvd = data.bytes_rcvd
local total_bytes = bytes_rcvd + bytes_sent
res[#res + 1] = {
flows = format_high_num_value_for_tables(data, 'num_flows'),
application = {
label = data.proto_name,
id = data.proto_id,
},
breakdown = {
percentage_bytes_sent = (bytes_sent * 100) / total_bytes,
percentage_bytes_rcvd = (bytes_rcvd * 100) / total_bytes,
},
bytes_rcvd = bytes_rcvd,
bytes_sent = bytes_sent,
tot_traffic = total_bytes,
tot_score = format_high_num_value_for_tables(data, 'total_score'),
num_servers = format_high_num_value_for_tables(data, 'num_servers'),
num_clients = format_high_num_value_for_tables(data, 'num_clients'),
vlan_id = {
id = data.vlan_id,
label = data.vlan_id
}
}
if(debugger and x < 2 and filters["sort_column"] == "application") then
tprint("LUA RESP")
tprint(res)
tprint("---------------------------------")
x = x + 1
end
::continue::
end
else
-- other criteria cases
for _, data in pairs(aggregated_info) do
local bytes_sent = data.bytes_sent
local bytes_rcvd = data.bytes_rcvd
local total_bytes = bytes_rcvd + bytes_sent
if vlan and not isEmptyString(vlan) and tonumber(vlan) ~= tonumber(data.vlan_id) then
goto continue
end
local client_ip = ternary(tonumber(data.vlan_id) ~= 0, string.format("%s@%s",data.client_ip,data.vlan_id), data.client_ip)
local server_ip = ternary(tonumber(data.vlan_id) ~= 0, string.format("%s@%s",data.server_ip,data.vlan_id), data.server_ip)
local server_name = data.server_name or " "
if (not isEmptyString(server_name)) then
server_name = ternary(server_name ~= server_ip, server_name, "")
end
local client_name = data.client_name or " "
if (not isEmptyString(client_name)) then
client_name = ternary(client_name ~= client_ip, client_name, "")
end
num_entries = data.num_entries
res[#res + 1] = {
flows = format_high_num_value_for_tables(data, 'num_flows'),
client = {
label = client_ip,
id = client_ip,
},
client_name = {
label = client_name,
id = client_ip
},
server = {
label = server_ip,
id = server_ip,
},
server_name = {
label = server_name,
id = server_ip
},
breakdown = {
percentage_bytes_sent = (bytes_sent * 100) / total_bytes,
percentage_bytes_rcvd = (bytes_rcvd * 100) / total_bytes,
},
bytes_rcvd = bytes_rcvd,
bytes_sent = bytes_sent,
tot_traffic = total_bytes,
tot_score = format_high_num_value_for_tables(data, 'total_score'),
num_servers = format_high_num_value_for_tables(data, 'num_servers'),
num_clients = format_high_num_value_for_tables(data, 'num_clients'),
vlan_id = {
id = data.vlan_id,
label = data.vlan_id
}
}
::continue::
end
end
local extra_rsp_data = {
["draw"] = tonumber(_GET["draw"]),
["recordsFiltered"] = tonumber(num_entries),
["recordsTotal"] = tonumber(num_entries),
}
rest_utils.extended_answer(rc, res, extra_rsp_data)
end
rest_utils.answer(rc, res)
build_response()