Fixes maps saved using mac addresses (#5625)

This commit is contained in:
MatteoBiscosi 2021-07-08 12:19:41 +02:00
parent 6e0a4839e8
commit b941752b0a

View file

@ -3,10 +3,15 @@
--
local rest_utils = require("rest_utils")
local json = require("dkjson")
local callback_utils = require("callback_utils")
local behavior_utils = {}
local redis_key = "changed_behavior_alert_setup"
local redis_key = "changed_behavior_learning_setup"
local behavior_maps_key = "ntopng.prefs.is_behaviour_analysis_enabled"
local behavior_mac_ip_mapping = "ntopng.cache.mac_ip_mapping"
local mac_ip_table = {}
local SEC_BEFORE_EXPIRE = 420 -- 7 mins
-- ##############################################
@ -37,7 +42,9 @@ function behavior_utils.get_behavior_timeseries_utils(family_key)
return behavior_table[family_key]
end
function behavior_utils.change_behavior_alert_status()
-- ##############################################
function behavior_utils.change_behavior_learning_status()
-- Set the redis key for the restart
ntop.setCache(redis_key, true)
rest_utils.answer(rest_utils.consts.success.ok, res)
@ -67,30 +74,61 @@ local maps_utils = {}
-- ##############################################
local function areMapsEnabled()
return(ntop.isEnterpriseL() and ntop.isAdministrator() and (ntop.getPref(behavior_maps_key) == "1"))
return(ntop.isEnterpriseL() and ntop.isAdministrator() and (ntop.getPref(behavior_maps_key) == "1"))
end
-- ##############################################
function behavior_utils.mapMacIp(hostname, hoststats)
if not mac_ip_table[hoststats["mac"]] then
mac_ip_table[hoststats["mac"]] = {}
end
mac_ip_table[hoststats["mac"]][#mac_ip_table[hoststats["mac"]] + 1] = hostname
end
-- ##############################################
function behavior_utils.updateMacs()
mac_ip_table = {}
callback_utils.foreachLocalHost(interface.getName(), behavior_utils.mapMacIp)
ntop.setCache(behavior_mac_ip_mapping, json.encode(mac_ip_table), SEC_BEFORE_EXPIRE)
return mac_ip_table
end
-- ##############################################
function behavior_utils.getMacs()
mac_ip_table = ntop.getCache(behavior_mac_ip_mapping)
if not isEmptyString(mac_ip_table) then
return json.decode(mac_ip_table)
end
return {}
end
-- ##############################################
-- Returns two bools value, one for service map and the other for periodicity map
function behavior_utils.mapsAvailable()
local service_map_available = false
local periodic_map_available = false
local service_map_available = false
local periodic_map_available = false
if areMapsEnabled() then
local service_map = interface.serviceMap()
local periodicity_map = interface.periodicityMap()
if areMapsEnabled() then
local service_map = interface.serviceMap(nil, 0, 0, false, 0, '') or {}
local periodicity_map = interface.periodicityMap(nil, 0, 0, false, 0, '') or {}
if service_map and (table.len(service_map) > 0) then
service_map_available = true
if service_map and (table.len(service_map) > 0) then
service_map_available = true
end
if periodicity_map and (table.len(periodicity_map) > 0) then
periodic_map_available = true
end
end
if periodicity_map and (table.len(periodicity_map) > 0) then
periodic_map_available = true
end
end
return service_map_available, periodic_map_available
return service_map_available, periodic_map_available
end
-- ##############################################