Migrate C flow status alerts to Lua user scripts

- Alerts and flow status cleanup
- Community flow user scripts migration
- Implement scripts filters by l7 proto and packet interface only
- Migrate flow2statusinfojson
- Lower flow periodic update to 30 seconds if there is flow activity
- Display flow scripts without a gui section
This commit is contained in:
emanuele-f 2019-10-17 17:37:15 +02:00
parent bfdf5773d3
commit b3a8c6d49a
37 changed files with 1071 additions and 1287 deletions

View file

@ -2,7 +2,7 @@
-- (C) 2019 - ntop.org
--
-- Check modules provide a scriptable way to interact with the ntopng
-- User scripts provide a scriptable way to interact with the ntopng
-- core. Users can provide their own modules to trigger custom alerts,
-- export data, or perform periodic tasks.
@ -87,7 +87,7 @@ end
-- ##############################################
-- @brief Enables a check module
-- @brief Enables a user script
function user_scripts.enableModule(ifid, subdir, module_key)
local hkey = getCheckModuleConfHash(ifid, subdir, module_key)
ntop.delHashCache(hkey, "disabled")
@ -95,7 +95,7 @@ end
-- ##############################################
-- @brief Disables a check module
-- @brief Disables a user script
function user_scripts.disableModule(ifid, subdir, module_key)
local hkey = getCheckModuleConfHash(ifid, subdir, module_key)
ntop.setHashCache(hkey, "disabled", "1")
@ -103,7 +103,7 @@ end
-- ##############################################
-- @brief Checks if a check module is enabled.
-- @brief Checks if a user script is enabled.
-- @return true if disabled, false otherwise
-- @notes Modules are neabled by default. The user can manually turn them off.
function user_scripts.isEnabled(ifid, subdir, module_key)
@ -113,7 +113,7 @@ end
-- ##############################################
-- @brief Get the default configuration value for the given check module
-- @brief Get the default configuration value for the given user script
-- and granularity.
-- @param user_script a user_script returned by user_scripts.load
-- @param granularity_str the target granularity
@ -243,18 +243,23 @@ end
-- ##############################################
-- @brief Load the check modules.
-- @brief Load the user scripts.
-- @params script_type one of user_scripts.script_types
-- @params ifid the interface ID
-- @params subdir the modules subdir
-- @params hook_filter if non nil, only load the check modules for the specified hook
-- @params ignore_disabled if true, also returns disabled check modules
-- @params hook_filter if non nil, only load the user scripts for the specified hook
-- @params ignore_disabled if true, also returns disabled user scripts
-- @param do_benchmark if true, computes benchmarks for every hook
-- @return {modules = key->user_script, hooks = user_script->function}
function user_scripts.load(script_type, ifid, subdir, hook_filter, ignore_disabled, do_benchmark)
local rv = {modules = {}, hooks = {}}
local is_nedge = ntop.isnEdge()
local alerts_disabled = (not areAlertsEnabled())
local old_ifid = interface.getId()
if(old_ifid ~= ifid) then
interface.select(ifid) -- required for interface.isPacketInterface() below
end
local check_dirs = {
user_scripts.getSubdirectoryPath(script_type, subdir),
@ -264,6 +269,10 @@ function user_scripts.load(script_type, ifid, subdir, hook_filter, ignore_disabl
if ntop.isPro() then
check_dirs[#check_dirs + 1] = user_scripts.getSubdirectoryPath(script_type, subdir, true --[[ pro ]])
check_dirs[#check_dirs + 1] = user_scripts.getSubdirectoryPath(script_type, subdir, true --[[ pro ]]) .. "/alerts"
if ntop.isEnterprise() then
check_dirs[#check_dirs + 1] = user_scripts.getSubdirectoryPath(script_type, subdir, true --[[ pro ]]) .. "/enterprise"
end
end
for _, hook in pairs(script_type.hooks) do
@ -281,14 +290,15 @@ function user_scripts.load(script_type, ifid, subdir, hook_filter, ignore_disabl
local user_script = require(mod_fname)
local setup_ok = true
traceError(TRACE_DEBUG, TRACE_CONSOLE, string.format("Loading check module '%s'", mod_fname))
traceError(TRACE_DEBUG, TRACE_CONSOLE, string.format("Loading user script '%s'", mod_fname))
if(user_script == nil) then
if(type(user_script) ~= "table") then
traceError(TRACE_ERROR, TRACE_CONSOLE, string.format("Loading '%s' failed", checks_dir.."/"..fname))
goto next_module
end
if(user_script.key == nil) then
traceError(TRACE_ERROR, TRACE_CONSOLE, string.format("Missing 'key' in check module '%s'", mod_fname))
traceError(TRACE_ERROR, TRACE_CONSOLE, string.format("Missing 'key' in user script '%s'", mod_fname))
goto next_module
end
@ -297,12 +307,16 @@ function user_scripts.load(script_type, ifid, subdir, hook_filter, ignore_disabl
goto next_module
end
if(user_script.nedge_exclude and is_nedge) then
if(user_script.packet_interface_only and (not interface.isPacketInterface())) then
traceError(TRACE_DEBUG, TRACE_CONSOLE, string.format("Skipping module '%s' for non packet interface", user_script.key))
end
if((user_script.nedge_exclude and is_nedge) or (user_script.nedge_only and (not is_nedge))) then
goto next_module
end
if(table.empty(user_script.hooks)) then
traceError(TRACE_WARNING, TRACE_CONSOLE, string.format("No 'hooks' defined in check module '%s'", user_script.key))
traceError(TRACE_WARNING, TRACE_CONSOLE, string.format("No 'hooks' defined in user script '%s'", user_script.key))
-- This guarantees that the "hooks" field is always available
user_script.hooks = {}
end
@ -378,6 +392,10 @@ function user_scripts.load(script_type, ifid, subdir, hook_filter, ignore_disabl
end
end
if(old_ifid ~= ifid) then
interface.select(old_ifid)
end
return(rv)
end