Add rest/v1/get/host/l7/stats.lua REST API (#3953)

This commit is contained in:
Alfredo Cardigliano 2020-05-28 10:44:07 +02:00
parent 462a7d4a5a
commit 9348e51924
4 changed files with 205 additions and 100 deletions

View file

@ -21,10 +21,9 @@ local rc = rest_utils.consts_ok
local res = {}
local ifid = _GET["ifid"]
local host_info = url2hostinfo(_GET)
local ndpistats_mode = _GET["ndpistats_mode"]
local breed = _GET["breed"]
local ndpi_category = _GET["ndpi_category"]
local ndpistats_mode = _GET["ndpistats_mode"]
if isEmptyString(ifid) then
rc = rest_utils.consts_invalid_interface
@ -53,8 +52,8 @@ local function getAppUrl(app)
return nil
end
local stats
local tot = 0
local _ifstats = {}
if ndpistats_mode == "sinceStartup" then
stats = interface.getStats()
@ -62,12 +61,8 @@ if ndpistats_mode == "sinceStartup" then
elseif ndpistats_mode == "count" then
stats = interface.getnDPIFlowsCount()
else
stats = interface.getHostInfo(host_info["host"], host_info["vlan"])
if stats == nil then
print(rest_utils.rc(rest_utils.consts_not_found))
return
end
tot = stats["bytes.sent"] + stats["bytes.rcvd"]
print(rest_utils.rc(rest_utils.consts_invalid_args))
return
end
if stats == nil then
@ -115,70 +110,7 @@ if(ndpistats_mode == "count") then
return
end
if(show_breed) then
local breed_stats = {}
for key, value in pairs(stats["ndpi"]) do
local b = stats["ndpi"][key]["breed"]
local traffic = stats["ndpi"][key]["bytes.sent"] + stats["ndpi"][key]["bytes.rcvd"]
if(breed_stats[b] == nil) then
breed_stats[b] = traffic
else
breed_stats[b] = breed_stats[b] + traffic
end
end
for key, value in pairs(breed_stats) do
_ifstats[key] = value
end
elseif(show_ndpi_category) then
local ndpi_category_stats = {}
for key, value in pairs(stats["ndpi_categories"]) do
key = getCategoryLabel(key)
local traffic = value["bytes"]
if(ndpi_category_stats[key] == nil) then
ndpi_category_stats[key] = traffic
else
ndpi_category_stats[key] = ndpi_category_stats[key] + traffic
end
end
for key, value in pairs(ndpi_category_stats) do
_ifstats[key] = value
end
else
-- Add ARP to stats
if(stats["eth"] ~= nil) then
arpBytes = stats["eth"]["ARP_bytes"]
else
arpBytes = 0
end
if(arpBytes > 0) then
_ifstats["ARP"] = arpBytes
end
for key, value in pairs(stats["ndpi"]) do
local traffic = value["bytes.sent"] + value["bytes.rcvd"]
if(key == "Unknown") then
traffic = traffic - arpBytes
end
if(traffic > 0) then
if(show_breed) then
_ifstats[value["breed"]] = traffic
else
_ifstats[key] = traffic
end
end
end
end
local _ifstats = computeL7Stats(stats)
-- Print up to this number of entries
local max_num_entries = 5
@ -194,27 +126,11 @@ for key, value in pairsByValues(_ifstats, rev) do
break
end
if(host_info["host"] == nil) then
res[#res + 1] = {
label = key,
value = value,
url = getAppUrl(key),
}
else
local duration
if(stats["ndpi"][key] ~= nil) then
duration = stats["ndpi"][key]["duration"]
else
duration = 0
end
res[#res + 1] = {
label = key,
value = value,
duration = duration,
}
end
res[#res + 1] = {
label = key,
value = value,
url = getAppUrl(key),
}
accumulate = accumulate + value
num = num + 1