mirror of
https://github.com/ntop/ntopng.git
synced 2026-04-30 07:59:35 +00:00
Fixes wrong response of REST endpoint when no host is found
Fixes #3709
This commit is contained in:
parent
1e10fbe06a
commit
53a4d796f5
1 changed files with 54 additions and 65 deletions
|
|
@ -2,7 +2,7 @@
|
|||
-- (C) 2013-20 - ntop.org
|
||||
--
|
||||
|
||||
dirs = ntop.getDirs()
|
||||
local dirs = ntop.getDirs()
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
|
||||
|
||||
require "lua_utils"
|
||||
|
|
@ -18,18 +18,8 @@ local host_stats_flows = _GET["host_stats_flows"]
|
|||
local host_stats_flows_num = _GET["limit"]
|
||||
|
||||
local host_info = url2hostinfo(_GET)
|
||||
local host = _GET["host"]
|
||||
|
||||
if(host_info["host"] == nil) then
|
||||
sendHTTPContentTypeHeader('text/html')
|
||||
|
||||
page_utils.print_header()
|
||||
dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")
|
||||
print("<div class=\"alert alert-danger\"><img src=".. ntop.getHttpPrefix() .. "/img/warning.png> Host parameter is missing (internal error ?)</div>")
|
||||
return
|
||||
end
|
||||
|
||||
function flows2protocolthpt(flows)
|
||||
local function flows2protocolthpt(flows)
|
||||
local protocol_thpt = {}
|
||||
for _, flow in pairs(flows) do
|
||||
local proto_ndpi = ""
|
||||
|
|
@ -61,72 +51,71 @@ local ifid = _GET["ifid"]
|
|||
-- priority goes to the interface id
|
||||
if ifid ~= nil and ifid ~= "" then
|
||||
if_name = getInterfaceName(ifid)
|
||||
-- finally, we fall back to the default selected interface name
|
||||
-- finally, we fall back to the default selected interface name
|
||||
else
|
||||
-- fall-back to the default interface
|
||||
if_name = ifname
|
||||
ifid = interface.name2id(ifname)
|
||||
end
|
||||
|
||||
interface.select(if_name)
|
||||
host = interface.getHostInfo(host_info["host"], host_info["vlan"])
|
||||
local res = {}
|
||||
|
||||
if(host == nil) then
|
||||
sendHTTPContentTypeHeader('text/html')
|
||||
sendHTTPHeader('application/json')
|
||||
|
||||
page_utils.print_header()
|
||||
dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")
|
||||
print("<div class=\"alert alert-danger\"><img src=".. ntop.getHttpPrefix() .. "/img/warning.png> Host ".. host_info["host"] .. " Vlan" ..host_info["vlan"].." cannot be found (expired ?)</div>")
|
||||
return
|
||||
else
|
||||
sendHTTPHeader('application/json')
|
||||
local hj = host
|
||||
if host_info["host"] then
|
||||
interface.select(if_name)
|
||||
local host = interface.getHostInfo(host_info["host"], host_info["vlan"])
|
||||
|
||||
-- hosts stats are on by default, one must explicitly disable them
|
||||
if not (host_stats == nil or host_stats == "" or host_stats == "true" or host_stats == "1") then
|
||||
hj = {}
|
||||
end
|
||||
if host then
|
||||
local hj = host
|
||||
|
||||
-- host flow stats are off by default and must be explicitly enabled
|
||||
if host_stats_flows ~= nil and host_stats_flows ~= "" then
|
||||
if host_stats_flows_num == nil or tonumber(host_stats_flows_num) == nil then
|
||||
-- default: do not limit the number of flows
|
||||
host_stats_flows_num = 99999
|
||||
else
|
||||
-- ... unless otherwise specified
|
||||
host_stats_flows_num = tonumber(host_stats_flows_num)
|
||||
-- hosts stats are on by default, one must explicitly disable them
|
||||
if not (host_stats == nil or host_stats == "" or host_stats == "true" or host_stats == "1") then
|
||||
hj = {}
|
||||
end
|
||||
local total = 0
|
||||
|
||||
local pageinfo = {["sortColumn"]="column_bytes", ["a2zSortOrder"]=false,
|
||||
["maxHits"]=host_stats_flows_num, ["toSkip"]=0, ["detailedResults"]=true}
|
||||
--local flows = interface.getFlowsInfo(host_info["host"], nil, "column_bytes", host_stats_flows_num, 0, false)
|
||||
local flows = interface.getFlowsInfo(host_info["host"], pageinfo)
|
||||
flows = flows["flows"]
|
||||
for i, fl in ipairs(flows) do
|
||||
flows[i] = {
|
||||
["srv.ip"] = fl["srv.ip"], ["cli.ip"] = fl["cli.ip"],
|
||||
["srv.port"] = fl["srv.port"], ["cli.port"] = fl["cli.port"],
|
||||
["proto.ndpi_id"] = fl["proto.ndpi_id"], ["proto.ndpi"] = fl["proto.ndpi"],
|
||||
["bytes"] = fl["bytes"],
|
||||
["cli2srv.throughput_bps"] = round(fl["throughput_cli2srv_bps"], 2),
|
||||
["srv2cli.throughput_bps"] = round(fl["throughput_srv2cli_bps"], 2),
|
||||
["cli2srv.throughput_pps"] = round(fl["throughput_cli2srv_pps"], 2),
|
||||
["srv2cli.throughput_pps"] = round(fl["throughput_srv2cli_pps"], 2),
|
||||
}
|
||||
if fl["proto.l4"] == "TCP" then
|
||||
flows[i]["cli2srv.tcp_flags"] = TCPFlags2table(fl["cli2srv.tcp_flags"])
|
||||
flows[i]["srv2cli.tcp_flags"] = TCPFlags2table(fl["srv2cli.tcp_flags"])
|
||||
flows[i]["tcp_established"] = fl["tcp_established"]
|
||||
-- host flow stats are off by default and must be explicitly enabled
|
||||
if host_stats_flows ~= nil and host_stats_flows ~= "" then
|
||||
if host_stats_flows_num == nil or tonumber(host_stats_flows_num) == nil then
|
||||
-- default: do not limit the number of flows
|
||||
host_stats_flows_num = 99999
|
||||
else
|
||||
-- ... unless otherwise specified
|
||||
host_stats_flows_num = tonumber(host_stats_flows_num)
|
||||
end
|
||||
end
|
||||
hj["ndpiThroughputStats"] = flows2protocolthpt(flows)
|
||||
hj["flows"] = flows
|
||||
hj["flows_count"] = total
|
||||
end
|
||||
print(json.encode(hj, nil))
|
||||
local total = 0
|
||||
|
||||
-- TRACKER HOOK
|
||||
tracker.log("host_get_json", {host_info["host"], host_info["vlan"]})
|
||||
local pageinfo = {["sortColumn"]="column_bytes", ["a2zSortOrder"]=false,
|
||||
["maxHits"]=host_stats_flows_num, ["toSkip"]=0, ["detailedResults"]=true}
|
||||
--local flows = interface.getFlowsInfo(host_info["host"], nil, "column_bytes", host_stats_flows_num, 0, false)
|
||||
local flows = interface.getFlowsInfo(host_info["host"], pageinfo)
|
||||
flows = flows["flows"]
|
||||
for i, fl in ipairs(flows) do
|
||||
flows[i] = {
|
||||
["srv.ip"] = fl["srv.ip"], ["cli.ip"] = fl["cli.ip"],
|
||||
["srv.port"] = fl["srv.port"], ["cli.port"] = fl["cli.port"],
|
||||
["proto.ndpi_id"] = fl["proto.ndpi_id"], ["proto.ndpi"] = fl["proto.ndpi"],
|
||||
["bytes"] = fl["bytes"],
|
||||
["cli2srv.throughput_bps"] = round(fl["throughput_cli2srv_bps"], 2),
|
||||
["srv2cli.throughput_bps"] = round(fl["throughput_srv2cli_bps"], 2),
|
||||
["cli2srv.throughput_pps"] = round(fl["throughput_cli2srv_pps"], 2),
|
||||
["srv2cli.throughput_pps"] = round(fl["throughput_srv2cli_pps"], 2),
|
||||
}
|
||||
if fl["proto.l4"] == "TCP" then
|
||||
flows[i]["cli2srv.tcp_flags"] = TCPFlags2table(fl["cli2srv.tcp_flags"])
|
||||
flows[i]["srv2cli.tcp_flags"] = TCPFlags2table(fl["srv2cli.tcp_flags"])
|
||||
flows[i]["tcp_established"] = fl["tcp_established"]
|
||||
end
|
||||
end
|
||||
hj["ndpiThroughputStats"] = flows2protocolthpt(flows)
|
||||
hj["flows"] = flows
|
||||
hj["flows_count"] = total
|
||||
end
|
||||
|
||||
res = hj
|
||||
end
|
||||
end
|
||||
|
||||
tracker.log("host_get_json", {host_info["host"], host_info["vlan"]})
|
||||
print(json.encode(res))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue