Reworked host traffic page adding vue

This commit is contained in:
MatteoBiscosi 2022-10-27 23:32:31 +02:00
parent 7e3562f1c4
commit 7692a0aa3d
10 changed files with 399 additions and 115 deletions

View file

@ -1297,7 +1297,7 @@ else
end
elseif((page == "traffic")) then
-- template render
template.render("htmlPages/hostDetails/hosttraffic.html", {})
template.render("pages/hosts/traffic.template", {})
elseif((page == "listening_ports")) then
template.render("htmlPages/hostDetails/listening-ports.template", {

View file

@ -0,0 +1,47 @@
--
-- (C) 2013-22 - ntop.org
--
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
-- Imports
require "lua_utils"
local rest_utils = require "rest_utils"
local graph_utils = require "graph_utils"
-- Local variables
local ifid = _GET["ifid"] or interface.getId()
local host_ip = _GET["host"]
local host_vlan = _GET["vlan"] or 0
interface.select(tostring(ifid))
local host = interface.getHostInfo(host_ip, host_vlan)
local series = {}
local labels = {}
local colors = {}
local rsp = {
labels = {},
series = {},
colors = {},
}
if(host and host.cardinality) then
series[#series + 1] = tonumber(host.cardinality.num_contacted_hosts_as_client or 0)
series[#series + 1] = tonumber(host.cardinality.num_contacted_hosts_as_server or 0)
else
series[#series + 1] = {}
series[#series + 1] = {}
end
colors[#colors + 1] = graph_utils.get_html_color(#colors + 1)
colors[#colors + 1] = graph_utils.get_html_color(#colors + 1)
labels[#labels + 1] = i18n("traffic_page.num_contacted_hosts_as_client")
labels[#labels + 1] = i18n("traffic_page.num_contacted_hosts_as_server")
rsp["series"] = series
rsp["labels"] = labels
rsp["colors"] = colors
rest_utils.answer(rest_utils.consts.success.ok, rsp)

View file

@ -0,0 +1,71 @@
--
-- (C) 2013-22 - ntop.org
--
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
-- Imports
require "lua_utils"
local graph_utils = require "graph_utils"
local rest_utils = require "rest_utils"
-- Local variables
local ifid = _GET["ifid"] or interface.getId()
local host_ip = _GET["host"]
local host_vlan = _GET["vlan"] or 0
interface.select(tostring(ifid))
local host = interface.getHostInfo(host_ip, host_vlan)
local series = {}
local labels = {}
local colors = {}
local max_num_entries = 5
local proto_stats = {}
local rsp = {
series = {},
labels = {},
colors = {},
}
-- ##################################
if host then
for id, _ in ipairs(l4_keys) do
local key = l4_keys[id][2]
local traffic = 0
if(host[key..".bytes.sent"] ~= nil) then
traffic = traffic + host[key..".bytes.sent"]
end
if(host[key..".bytes.rcvd"] ~= nil) then
traffic = traffic + host[key..".bytes.rcvd"]
end
if traffic > 0 then
proto_stats[l4_keys[id][1]] = traffic
end
end
end
for key, value in pairsByValues(proto_stats, rev) do
series[#series + 1] = value
labels[#labels + 1] = key
colors[#colors + 1] = graph_utils.get_html_color(#colors + 1)
max_num_entries = max_num_entries - 1
-- Just return the top 5 l4 protocols
if max_num_entries == 1 then
break
end
end
rsp["series"] = series
rsp["labels"] = labels
rsp["colors"] = colors
-- ##################################
rest_utils.answer(rest_utils.consts.success.ok, rsp)

View file

@ -0,0 +1,54 @@
--
-- (C) 2013-22 - ntop.org
--
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
-- Imports
require "lua_utils"
local graph_utils = require "graph_utils"
local rest_utils = require "rest_utils"
-- Local variables
local ifid = _GET["ifid"] or interface.getId()
local host_ip = _GET["host"]
local host_vlan = _GET["vlan"] or 0
interface.select(tostring(ifid))
local host = interface.getHostInfo(host_ip, host_vlan)
local total_bytes_rcvd = 0
local total_bytes_sent = 0
local colors = {}
if host then
for id, _ in ipairs(l4_keys) do
local key = l4_keys[id][2]
local traffic = 0
if(host[key..".bytes.sent"] ~= nil) then
traffic = traffic + host[key..".bytes.sent"]
total_bytes_sent = total_bytes_sent + host[key..".bytes.sent"]
end
if(host[key..".bytes.rcvd"] ~= nil) then
traffic = traffic + host[key..".bytes.rcvd"]
total_bytes_rcvd = total_bytes_rcvd + host[key..".bytes.rcvd"]
end
end
end
colors[#colors + 1] = graph_utils.get_html_color(#colors + 1)
colors[#colors + 1] = graph_utils.get_html_color(#colors + 1)
-- Bytes sent vs Bytes rcvd
local rsp = {
["series"] = { total_bytes_sent, total_bytes_rcvd },
["labels"] = { i18n("traffic_page.bytes_sent"), i18n("traffic_page.bytes_rcvd") },
["colors"] = colors -- Still two colors like the second serie, reuse it
}
-- ##################################
rest_utils.answer(rest_utils.consts.success.ok, rsp)

View file

@ -1,105 +0,0 @@
--
-- (C) 2013-22 - ntop.org
--
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local graph_utils = require "graph_utils"
local rest_utils = require "rest_utils"
local ifid = _GET["ifid"]
if isEmptyString(ifid) then
ifid = interface.getId()
end
interface.select(tostring(ifid))
local host_ip = _GET["host"]
local host_vlan = _GET["vlan"] or 0
local host = interface.getHostInfo(host_ip, host_vlan)
local total = 0
local total_bytes_sent = 0
local total_bytes_rcvd = 0
local proto_stats = {}
local max_num_entries = 5
local colors1 = {}
local labels1 = {}
local series1 = {}
local colors2 = {}
local labels2 = {}
local series2 = {}
local rsp = {}
local rc = rest_utils.consts.success.ok
for id, _ in ipairs(l4_keys) do
local key = l4_keys[id][2]
local traffic = 0
if(host[key..".bytes.sent"] ~= nil) then
traffic = traffic + host[key..".bytes.sent"]
total_bytes_sent = total_bytes_sent + host[key..".bytes.sent"]
end
if(host[key..".bytes.rcvd"] ~= nil) then
traffic = traffic + host[key..".bytes.rcvd"]
total_bytes_rcvd = total_bytes_rcvd + host[key..".bytes.rcvd"]
end
if traffic > 0 then
proto_stats[l4_keys[id][1]] = traffic
total = total + traffic
end
end
for key, value in pairsByValues(proto_stats, rev) do
series1[#series1 + 1] = value
labels1[#labels1 + 1] = key
colors1[#colors1 + 1] = graph_utils.get_html_color(#colors1 + 1)
max_num_entries = max_num_entries - 1
-- Just return the top 5 l4 protocols
if max_num_entries == 1 then
break
end
end
if(host.cardinality) then
series2[#series2 + 1] = tonumber(host.cardinality.num_contacted_hosts_as_client or 0)
series2[#series2 + 1] = tonumber(host.cardinality.num_contacted_hosts_as_server or 0)
else
series2[#series2 + 1] = {}
series2[#series2 + 1] = {}
end
colors2[#colors2 + 1] = graph_utils.get_html_color(#colors2 + 1)
colors2[#colors2 + 1] = graph_utils.get_html_color(#colors2 + 1)
labels2[#labels2 + 1] = i18n("traffic_page.num_contacted_hosts_as_client")
labels2[#labels2 + 1] = i18n("traffic_page.num_contacted_hosts_as_server")
-- L4 proto total distribution
rsp["serie1"] = {
["series"] = series1,
["labels"] = labels1,
["colors"] = colors1
}
-- Host contacts
rsp["serie2"] = {
["series"] = series2,
["labels"] = labels2,
["colors"] = colors2
}
-- Bytes sent vs Bytes rcvd
rsp["serie3"] = {
["series"] = { total_bytes_sent, total_bytes_rcvd },
["labels"] = { i18n("traffic_page.bytes_sent"), i18n("traffic_page.bytes_rcvd") },
["colors"] = colors2 -- Still two colors like the second serie, reuse it
}
rest_utils.answer(rc, rsp)

View file

@ -65,14 +65,7 @@ for id, _ in ipairs(l4_keys) do
end
if table.len(proto_info) > 0 then
rsp["records"] = proto_info
end
if(total > 0) then
if(host.cardinality) then
rsp["contactedHostsAsCli"] = formatValue(host.cardinality.num_contacted_hosts_as_client)
rsp["contactedHostsAsSrv"] = formatValue(host.cardinality.num_contacted_hosts_as_server)
end
rsp = proto_info
end
rest_utils.answer(rc, rsp)