Separates L3 IPs from L2 MACs

Before this commit, multiple L3 hosts were allocated if the same ip
address was seen with multiple mac addresses. We have decided to keep
ip and macs logically sepatated. For this reason, after this commit,
the L3 ip always remains the same even if there are changes in the
mac. A user who wants to monitor mac addresses can use the L2 section
as well as the host pools.

This commit also reverts:
"Add MAC address to the serialized hosts key to fix bad MAC address association and stats"

This reverts commit 89a756fb9a.
This commit is contained in:
Simone Mainardi 2017-07-04 00:12:52 +02:00
parent 8f7d2eeba9
commit eaa7a0d313
18 changed files with 307 additions and 206 deletions

View file

@ -22,69 +22,23 @@ host_pools_utils = require "host_pools_utils"
local prefs = ntop.getPrefs()
-- move the old serialized hosts format to the new one
local json = require("dkjson")
local first_one = true
for _, ifname in pairs(interface.getIfNames()) do
interface.select(ifname)
local ifid = getInterfaceId(ifname)
local dumped_hosts = ntop.getKeysCache("ntopng.serialized_hosts.ifid_"..ifid.."__*")
if dumped_hosts ~= nil then
for hostkey, _ in pairs(dumped_hosts) do
if first_one then
io.write("Migrating hosts cache to new key format...\n")
first_one = false
end
local json_val = ntop.getCache(hostkey)
if json_val ~= nil then
local val = json.decode(json_val)
if (val ~= nil) and (val.ip ~= nil) and (not isEmptyString(val.ip.ip)) then
local mac = ""
local vlan_id = 0
if isMacAddress(val.mac_address) then
mac = val.mac_address
end
if tonumber(val.vlan_id) ~= nil then
vlan_id = val.vlan_id
end
ntop.setCache("ntopng.serialized_hosts.ifid_"..ifid.."_"..val.ip.ip.."_"..mac.."@"..vlan_id, json_val)
ntop.delCache(hostkey)
end
end
end
end
end
if not first_one then
io.write("Hosts cache migration completed successfully\n")
end
-- restore sticky hosts
if prefs.sticky_hosts ~= nil then
-- if the sticky hosts are set, then we try and restore them out of redis
for _, ifname in pairs(interface.getIfNames()) do
interface.select(ifname)
local ifid = getInterfaceId(ifname)
-- an example key is ntopng.serialized_hosts.ifid_6_192.168.2.136_22:12:13:14:15:34@0
local keys_pattern = "ntopng.serialized_hosts.ifid_"..ifid.."_*"
local dumped_hosts = ntop.getKeysCache("ntopng.serialized_hosts.ifid_"..ifid.."_*")
-- an example key is ntopng.serialized_hosts.ifid_6__192.168.2.136@0
local keys_pattern = "ntopng.serialized_hosts.ifid_"..ifid.."__*"
local dumped_hosts = ntop.getKeysCache("ntopng.serialized_hosts.ifid_"..ifid.."__*")
if dumped_hosts ~= nil then
for hostkey, _ in pairs(dumped_hosts) do
-- let's extract just the host name and vlan from the whole key;
-- restore host will do the rest ...
local key_parts = string.split(hostkey, "_")
if key_parts ~= nil and key_parts[4] ~= nil then
local hostkey = key_parts[4]
if key_parts[5] ~= nil then
local parts = string.split(key_parts[5], "@")
if #parts == 2 then
-- add vlan
hostkey = hostkey .. "@" .. parts[2]
end
end
local key_parts = string.split(hostkey, "__")
if key_parts ~= nil and key_parts[2] ~= nil then
local hostkey = key_parts[2]
interface.restoreHost(hostkey, true --[[ skip privileges checks: no web access --]])
end