Improves the responsiveness and interactivity of historical exploration

Uses ajax to handle (possibly) long queries on historical data
with the aim of improving user experience.
The user is notified when a query is taking too long so that
he/she can get feedback of what is going on.
This commit is contained in:
Simone Mainardi 2016-10-31 15:56:11 +01:00
parent 0793d8666a
commit 8f87f11b26
4 changed files with 568 additions and 485 deletions

View file

@ -6,13 +6,14 @@ dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
require "db_utils"
local json = require ("dkjson")
local ifId = _GET["ifId"]
local ip_version = _GET["version"]
local host = _GET["host"]
local epoch_begin = _GET["epoch_begin"]
local epoch_end = _GET["epoch_end"]
local epoch_end = tonumber(_GET["epoch_end"] or os.time())
local epoch_begin = tonumber(_GET["epoch_begin"] or epoch_end - 3600)
local l4proto = _GET["l4proto"]
local l7proto = _GET["l7proto"]
local profile = _GET["profile"]
@ -22,74 +23,42 @@ local limit = _GET["limit"]
local format = _GET["format"]
local action = _GET["action"]
if(ip_version == nil) then ip_version = "4" end
if ip_version == nil then ip_version = "4" end
ip_version = tonumber(ip_version)
function top_peers_query(interface_id, version, host, protocol, port, l7proto, info, begin_epoch, end_epoch)
if(host == nil or host == "") then return nil end
if(version == nil) then version = 4 end
if epoch_begin> epoch_end then
local tmp = epoch_end
epoch_end = epoch_begin
epoch_being = epoch_end
end
local timediff = epoch_end - epoch_begin + 1
if(info == "") then info = nil end
if(l7proto == "") then l7proto = nil end
if(protocol == "") then protocol = nil end
local totals = { ["count"] = {}, ["timespan"] = timediff, ["status"] = "ok" }
local versions = { [4] = 'IPv4', [6] = 'IPv6' }
sql = " SELECT "
if(version == 4) then
sql = sql.." CASE WHEN IP_SRC_ADDR = INET_ATON('"..host.."') THEN INET_NTOA(IP_DST_ADDR) ELSE INET_NTOA(IP_SRC_ADDR) END peer, "
else
sql = sql.." CASE WHEN IP_SRC_ADDR = '"..host.."' THEN IP_DST_ADDR ELSE IP_SRC_ADDR END peer, "
headerShown = false
-- os.execute("sleep 30") -- this is to test slow responses
for k,v in pairs(versions) do
local res = getNumFlows(_GET["ifId"], k, _GET["host"], _GET["l4proto"], _GET["port"], _GET["protocol"], _GET["info"], _GET["epoch_begin"], _GET["epoch_end"])
if res == nil or res[1] == nil then
totals["status"] = "error"
totals["statusText"] = "Empty query response (database down or query killed/timed out?)"
goto continue
end
sql = sql.."BYTES as bytes, PACKETS as packets, PROTOCOL as l4proto, L7_PROTO as l7proto, "
sql = sql.."FIRST_SWITCHED as first_switched, LAST_SWITCHED as last_switched "
sql = sql.." FROM flowsv"..version
res = res[1] -- only one row is present in the result that contains the aggregate counters
sql = sql.." WHERE FIRST_SWITCHED <= "..end_epoch.." and FIRST_SWITCHED >= "..begin_epoch
sql = sql.." AND (NTOPNG_INSTANCE_NAME='"..ntop.getPrefs()["instance_name"].."'OR NTOPNG_INSTANCE_NAME IS NULL)"
sql = sql.." AND (INTERFACE_ID='"..tonumber(interface_id).."')"
totals["count"][v] = {
["tot_flows"] = tonumber(res["TOT_FLOWS"]) or 0,
["tot_bytes"] = tonumber(res["TOT_BYTES"]) or 0,
["tot_packets"] = tonumber(res["TOT_PACKETS"]) or 0
}
if((l7proto ~= nil) and (l7proto ~= "")) then sql = sql .." AND L7_PROTO="..l7proto end
if((protocol ~= nil) and (protocol ~= "")) then sql = sql .." AND PROTOCOL="..protocol end
if(info ~= nil) then sql = sql .." AND (INFO='"..info.."')" end
if((port ~= nil) and (port ~= "")) then sql = sql .." AND (L4_SRC_PORT="..port.." OR L4_DST_PORT="..port..")" end
if((host ~= nil) and (host ~= "")) then
if(version == 4) then
sql = sql .." AND (IP_SRC_ADDR=INET_ATON('"..host.."') OR IP_DST_ADDR=INET_ATON('"..host.."'))"
else
sql = sql .." AND (IP_SRC_ADDR='"..host.."' OR IP_DST_ADDR='"..host.."')"
end
end
sql = sql.." order by bytes desc limit 2001"
if(db_debug == true) then io.write(sql.."\n") end
res = interface.execSQLQuery(sql)
if(type(res) == "string") then
if(db_debug == true) then io.write(res.."\n") end
return nil
else
return(res)
end
::continue::
end
if action == "get_peers" then
local res = top_peers_query(ifId, ip_version, host, l4proto, port, l7proto, info, epoch_begin, epoch_end)
local ndpi_protocols = {}
for proto_name, proto_id in pairs(interface.getnDPIProtocols()) do
ndpi_protocols[proto_id] = proto_name
end
for _,flow in pairs(res) do
local peer_info = interface.getHostInfo(flow["peer"])
if peer_info ~= nil then
flow["peer"] = ntop.getResolvedAddress(hostinfo2hostkey(peer_info))
end
if ndpi_protocols[flow["l7proto"]] ~= "" then
flow["l7proto"] = ndpi_protocols[flow["l7proto"]]
end
end
sendHTTPHeader('application/json')
print(json.encode(res, nil))
end
sendHTTPHeader('application/json')
print(json.encode(totals, nil))