Implemented lua caching to speedump hostname and interface mapping lookup

This commit is contained in:
Luca Deri 2025-10-23 22:28:53 +02:00
parent 8d13fe3411
commit e0e4e8ea96
11 changed files with 817 additions and 600 deletions

View file

@ -0,0 +1,106 @@
--
-- (C) 2013-25 - ntop.org
--
--
-- Volatile (across ntopng restarts) in-memory cache
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
local json = require "dkjson"
local cache_utils = {}
-- ##############################################
-- Loaded at startup
function cache_utils.initialize()
local basename = "ntopng.cachedsnmp."
local len = string.len(basename)
-- system
local keys = ntop.getKeysCache(basename.."*.system")
for k,_ in pairs(keys) do
local system = json.decode(ntop.getCache(k))
local ipaddr = string.sub(k, len+1, string.len(k)-7)
cache_utils.sethostname(ipaddr, system.name)
end
-- interfaces
keys = ntop.getKeysCache(basename.."*.interfaces")
for k,_ in pairs(keys) do
local ifaces = json.decode(ntop.getCache(k))
local ipaddr = string.sub(k, len+1, string.len(k)-11)
-- tprint("Loading "..ipaddr)
for if_id,if_v in pairs(ifaces) do
local val = if_v.alias or if_v.ifname or if_v.name
if(val ~= nil) then
cache_utils.setifname(ipaddr, if_id, val)
end
end
end
-- Debug
-- tprint(ntop.dumpLuaCache())
end
-- ##############################################
function cache_utils.gethostname(ipaddr)
return(ntop.getLuaCache("host."..ipaddr))
end
-- ##############################################
function cache_utils.sethostname(ipaddr, name)
if(debugme) then
tprint("cache_utils.sethostname(".. ipaddr .. ", ".. name..")")
end
return(ntop.setLuaCache("host."..ipaddr, name or ipaddr))
end
-- ##############################################
function cache_utils.getifname(ipaddr, ifid)
local ret = ntop.getLuaCache("iface."..ipaddr.."@"..ifid)
if(isEmptyString(ret)) then
ret = ifid
end
if(debugme) then
tprint(debug.traceback())
tprint("cache_utils.getifname(".. ipaddr .. ", ".. ifid..") = "..ret)
end
return(ret)
end
-- ##############################################
function cache_utils.setifname(ipaddr, ifid, ifname)
if(ifname == nil) then
tprint("cache_utils.setifname() ERROR on interface "..ipaddr.." / "..ifid)
tprint(debug.traceback())
end
return(ntop.setLuaCache("iface."..ipaddr.."@"..ifid, ifname or ifid))
end
-- ##############################################
function cache_utils.set(key, val)
ntop.setLuaCache(key, val)
end
-- ##############################################
return cache_utils

View file

@ -296,8 +296,7 @@ function flow_data_preset.getFormattedDataAndLink(key, value, values)
-- Get the formatter function
local formatter = columns[key]["formatter"]["funct"]
local link_formatter =
columns[key]["formatter"]["generateLink"]
local link_formatter = columns[key]["formatter"]["generateLink"]
-- See if there is some column from which is dependent,
-- e.g. SNMP Interface needs the SNMP IP
@ -306,12 +305,10 @@ function flow_data_preset.getFormattedDataAndLink(key, value, values)
if (#dependent_values > 1) then
-- TODO: Now it's limited to a single column, add multiple columns
formatted_value =
formatter(dependent_values[1], dependent_values[2])
formatted_value = formatter(dependent_values[1], dependent_values[2])
if link_formatter then
link = link_formatter(dependent_values[1],
dependent_values[2])
link = link_formatter(dependent_values[1], dependent_values[2])
end
else
-- new code

View file

@ -1,6 +1,7 @@
--
-- (C) 2013-24 - ntop.org
--
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path

View file

@ -13,6 +13,7 @@ pragma_once_lua_utils_get = true
require "ntop_utils"
require "label_utils"
require "check_redis_prefs"
local cache_utils = require "cache_utils"
local clock_start = os.clock()
local snmp_cached_devices = {}
@ -97,11 +98,19 @@ function getProbeName(exporter_ip, show_vlan, shorten_len, show_ip_and_alias)
if show_ip_and_alias == nil then
show_ip_and_alias = true
end
if tonumber(exporter_ip) then
exporter_ip = ntop.inet_ntoa(exporter_ip)
end
if(exporter_ip ~= nil) then
local ret = cache_utils.gethostname(exporter_ip)
if(not isEmptyString(ret)) then
return(ret)
end
end
local cached_device_name
local snmp_cached_dev
local probe_alias = probes_names[exporter_ip]

View file

@ -24,6 +24,8 @@ local dns_utils = require "dns_utils"
local http_utils = require "http_utils"
local rest_utils = require "rest_utils"
local snmp_utils
local cache_utils = require "cache_utils"
if ntop.isPro() then
package.path = dirs.installdir .. "/scripts/lua/pro/modules/?.lua;" ..
package.path
@ -987,7 +989,6 @@ end
local _snmp_devices = {}
local _exporters_ports_names = {}
local _portidx_name_cache = {}
-- @brief This function format the SNMP interface name.
-- @params device_ip: snmp device ip
@ -995,11 +996,17 @@ local _portidx_name_cache = {}
-- short_version: boolean, long formatting version (e.g. flow info) or short version (e.g. dropdown menu)
function format_portidx_name(device_ip, portidx, short_version)
local idx_name = portidx
local cache_key = device_ip .. "@" .. portidx
local cached_val = _portidx_name_cache[cache_key]
local cached_val = cache_utils.getifname(device_ip, portidx)
if (cached_val ~= nil) then return (cached_val) end
if (cached_val ~= nil) then
if(short_version) then
cached_val = shortenString(cached_val, 26)
end
return (cached_val)
end
-- DEAD CODE BEGIN
-- tprint("format_portidx_name("..device_ip .. ", " .. portidx..")")
-- SNMP is available only with Pro version at least
@ -1044,9 +1051,11 @@ function format_portidx_name(device_ip, portidx, short_version)
end
end
_portidx_name_cache[cache_key] = idx_name
cache_utils.setifname(device_ip, portidx, idx_name)
return idx_name
-- DEAD CODE END
end
-- ##############################################

File diff suppressed because it is too large Load diff

View file

@ -763,8 +763,7 @@ function predicates.exporters_SNMP_ratio_column(toast, container)
return
end
local cached_device = snmp_cached_dev:create(flow_device_ip)
local cached_device = snmp_cached_dev:get_interfaces(flow_device_ip)
local is_ratio_available = snmp_utils.is_snmp_ratio_available(cached_device)
if (is_ratio_available) then