[VS] Fix inconsistent state. (#7922)

This commit is contained in:
Nicolo Maio 2023-10-20 17:30:37 +02:00
parent e3752925b4
commit 7eabc4d3ce
2 changed files with 37 additions and 21 deletions

View file

@ -44,7 +44,7 @@ local host_to_scan_periodicity_key = "ntopng.prefs.host_to_scan.periodicity_sca
local host_scannned_count_key = "ntopng.prefs.host_to_scan.count_scanned"
local host_scan_queue_key = "ntopng.vs_scan_queue"
local scanned_hosts_changes_key = "ntopng.alerts.scanned_hosts_changes"
local host_in_scanning_key = "ntopng.vs_host_in_scanning.%s.%s"
local host_in_scanning_hash_key = "ntopng.prefs.host_to_scan.in_scanning"
local json = require("dkjson")
local format_utils = require("format_utils")
@ -60,9 +60,7 @@ function vs_utils.get_host_hash_key(host, scan_type)
return string.format("%s-%s",host,scan_type)
end
function vs_utils.get_host_in_scanning_key(host, scan_type)
return string.format(host_in_scanning_key, host, scan_type)
end
-- **********************************************************
@ -604,29 +602,41 @@ end
-- Function to remove scanning host
local function remove_scanning_host(host_info)
local redis_host_in_scanning_key = vs_utils.get_host_in_scanning_key(host_info.host, host_info.scan_type)
local host_info_redis_string = ntop.getCache(redis_host_in_scanning_key)
if (not isEmptyString(host_info_redis_string)) then
local host_info_redis = json.decode(host_info_redis_string)
-- check also the ports to be sure.
if (host_info_redis and
host_info.ports == host_info_redis.ports)
then
ntop.delCache(redis_host_in_scanning_key)
end
end
local host_to_scan_hash_key = vs_utils.get_host_hash_key(host_info.host, host_info.scan_type)
ntop.delHashCache(host_in_scanning_hash_key,host_to_scan_hash_key)
end
-- **********************************************************
-- Function to set the actual scanning host on a redis key
local function save_scanning_host(scan_info)
local redis_host_in_scanning_key = vs_utils.get_host_in_scanning_key(scan_info.host, scan_info.scan_type)
ntop.setCache(redis_host_in_scanning_key, json.encode({ports=scan_info.ports}))
local host_to_scan_hash_key = vs_utils.get_host_hash_key(scan_info.host, scan_info.scan_type)
ntop.setHashCache(host_in_scanning_hash_key, host_to_scan_hash_key, json.encode(scan_info))
end
-- **********************************************************
-- Function to restore scanning host
function vs_utils.restore_host_to_scan()
local hash_keys = ntop.getHashKeysCache(host_in_scanning_hash_key)
if hash_keys then
for k in pairs(hash_keys) do
local hash_value_string = ntop.getHashCache(host_in_scanning_hash_key, k)
if (not isEmptyString(hash_value_string)) then
local host_info_to_restore = json.decode(hash_value_string)
if (host_info_to_restore) then
-- enqueue to scan
ntop.lpushCache(host_scan_queue_key, hash_value_string)
-- set status to scheduled
vs_utils.set_status_scan(host_info_to_restore.scan_type, host_info_to_restore.host, host_info_to_restore.ports, host_info_to_restore.id, nil, vs_utils.scan_status.scheduled)
end
end
end
end
end
@ -955,6 +965,7 @@ function vs_utils.delete_host_to_scan(host, scan_type, all)
if all then
ntop.delCache(host_to_scan_key)
ntop.delCache(host_scan_queue_key)
ntop.delCache(host_in_scanning_hash_key)
ntop.delCache(host_to_scan_periodicity_key)
ntop.delCache(host_to_scan_periodicity_key.."type")