mirror of
https://github.com/ntop/ntopng.git
synced 2026-05-02 00:40:10 +00:00
Initial work to implement user script templates
This commit is contained in:
parent
ee8ffaca7d
commit
80ae202ad1
8 changed files with 420 additions and 8 deletions
72
scripts/lua/modules/user_script_templates/elephant_flows.lua
Normal file
72
scripts/lua/modules/user_script_templates/elephant_flows.lua
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
--
|
||||
-- (C) 2019-21 - ntop.org
|
||||
--
|
||||
|
||||
-- ##############################################
|
||||
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/user_script_templates/?.lua;" .. package.path
|
||||
|
||||
-- Import the classes library.
|
||||
local classes = require "classes"
|
||||
-- Make sure to import the Superclass!
|
||||
local user_script_template = require "user_script_template"
|
||||
local http_lint = require "http_lint"
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local elephant_flows = classes.class(user_script_template)
|
||||
|
||||
-- ##############################################
|
||||
|
||||
elephant_flows.meta = {
|
||||
}
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- @brief Prepare an instance of the template
|
||||
-- @return A table with the template built
|
||||
function elephant_flows:init()
|
||||
-- Call the parent constructor
|
||||
self.super:init()
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
function elephant_flows:parseConfig(script, conf)
|
||||
if(tonumber(conf.l2r_bytes_value) == nil) then
|
||||
return false, "bad l2r_bytes_value value"
|
||||
end
|
||||
|
||||
if(tonumber(conf.r2l_bytes_value) == nil) then
|
||||
return false, "bad r2l_bytes_value value"
|
||||
end
|
||||
|
||||
return http_lint.validateListItems(script, conf)
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
function elephant_flows:describeConfig(script, hooks_conf)
|
||||
if not hooks_conf.all then
|
||||
return '' -- disabled, nothing to show
|
||||
end
|
||||
|
||||
-- E.g. '> 1 GB (L2R), > 2 GB (R2L), except: Datatransfer, Git'
|
||||
local conf = hooks_conf.all.script_conf
|
||||
local msg = i18n("user_scripts.elephant_flows_descr", {
|
||||
l2r_bytes = bytesToSize(conf.l2r_bytes_value),
|
||||
r2l_bytes = bytesToSize(conf.r2l_bytes_value),
|
||||
})
|
||||
|
||||
if not table.empty(conf.items) then
|
||||
msg = msg .. ". " .. i18n("user_scripts.exceptions", {exceptions = table.concat(conf.items, ', ')})
|
||||
end
|
||||
|
||||
return(msg)
|
||||
end
|
||||
|
||||
|
||||
-- #######################################################
|
||||
|
||||
return elephant_flows
|
||||
52
scripts/lua/modules/user_script_templates/items_list.lua
Normal file
52
scripts/lua/modules/user_script_templates/items_list.lua
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
--
|
||||
-- (C) 2019-21 - ntop.org
|
||||
--
|
||||
|
||||
-- ##############################################
|
||||
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/user_script_templates/?.lua;" .. package.path
|
||||
|
||||
-- Import the classes library.
|
||||
local classes = require "classes"
|
||||
-- Make sure to import the Superclass!
|
||||
local user_script_template = require "user_script_template"
|
||||
local http_lint = require "http_lint"
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local items_list = classes.class(user_script_template)
|
||||
|
||||
-- ##############################################
|
||||
|
||||
items_list.meta = {
|
||||
}
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- @brief Prepare an instance of the template
|
||||
-- @return A table with the template built
|
||||
function items_list:init()
|
||||
-- Call the parent constructor
|
||||
self.super:init()
|
||||
end
|
||||
|
||||
function items_list:parseConfig(script, conf)
|
||||
return http_lint.validateListItems(script, conf)
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
function items_list:describeConfig(script, hooks_conf)
|
||||
if((not hooks_conf.all) or (not hooks_conf.all.script_conf)) then
|
||||
return '' -- disabled, nothing to show
|
||||
end
|
||||
|
||||
local items = hooks_conf.all.script_conf.items or {}
|
||||
|
||||
return table.concat(items, ", ")
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
return items_list
|
||||
65
scripts/lua/modules/user_script_templates/long_lived.lua
Normal file
65
scripts/lua/modules/user_script_templates/long_lived.lua
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
--
|
||||
-- (C) 2019-21 - ntop.org
|
||||
--
|
||||
|
||||
-- ##############################################
|
||||
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/user_script_templates/?.lua;" .. package.path
|
||||
|
||||
-- Import the classes library.
|
||||
local classes = require "classes"
|
||||
-- Make sure to import the Superclass!
|
||||
local user_script_template = require "user_script_template"
|
||||
local http_lint = require "http_lint"
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local long_lived = classes.class(user_script_template)
|
||||
|
||||
-- ##############################################
|
||||
|
||||
long_lived.meta = {
|
||||
}
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- @brief Prepare an instance of the template
|
||||
-- @return A table with the template built
|
||||
function long_lived:init()
|
||||
-- Call the parent constructor
|
||||
self.super:init()
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
function long_lived:parseConfig(script, conf)
|
||||
if(tonumber(conf.min_duration) == nil) then
|
||||
return false, "bad min_duration value"
|
||||
end
|
||||
|
||||
return http_lint.validateListItems(script, conf)
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
function long_lived:describeConfig(script, hooks_conf)
|
||||
if not hooks_conf.all then
|
||||
return '' -- disabled, nothing to show
|
||||
end
|
||||
|
||||
local conf = hooks_conf.all.script_conf
|
||||
local msg = i18n("user_scripts.long_lived_flows_descr", {
|
||||
duration = secondsToTime(conf.min_duration),
|
||||
})
|
||||
|
||||
if(not table.empty(conf.items)) then
|
||||
msg = msg .. ". " .. i18n("user_scripts.exceptions", {exceptions = table.concat(conf.items, ', ')})
|
||||
end
|
||||
|
||||
return(msg)
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
return long_lived
|
||||
79
scripts/lua/modules/user_script_templates/multi_select.lua
Normal file
79
scripts/lua/modules/user_script_templates/multi_select.lua
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
--
|
||||
-- (C) 2019-21 - ntop.org
|
||||
--
|
||||
|
||||
-- ##############################################
|
||||
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/user_script_templates/?.lua;" .. package.path
|
||||
|
||||
-- Import the classes library.
|
||||
local classes = require "classes"
|
||||
-- Make sure to import the Superclass!
|
||||
local user_script_template = require "user_script_template"
|
||||
local http_lint = require "http_lint"
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local multi_select = classes.class(user_script_template)
|
||||
|
||||
-- ##############################################
|
||||
|
||||
multi_select.meta = {
|
||||
}
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- @brief Prepare an instance of the template
|
||||
-- @return A table with the template built
|
||||
function multi_select:init()
|
||||
-- Call the parent constructor
|
||||
self.super:init()
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
function multi_select:parseConfig(script, conf)
|
||||
return http_lint.validateListItems(script, conf)
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
function multi_select:describeConfig(script, hooks_conf)
|
||||
|
||||
if (not hooks_conf.all) then
|
||||
return '' -- disabled, nothing to show
|
||||
end
|
||||
|
||||
local conf = hooks_conf.all.script_conf
|
||||
|
||||
local msg = ''
|
||||
if not table.empty(conf.items) then
|
||||
|
||||
local temp_msg = {}
|
||||
local groups = script.gui.groups
|
||||
|
||||
-- build a string containing selected elements separated by comma
|
||||
for _, group in ipairs(groups) do
|
||||
local elements = group.elements
|
||||
for _, element in ipairs(elements) do
|
||||
|
||||
local id = element[1]
|
||||
local label = element[2]
|
||||
|
||||
if table.has_key(conf.items, id) then
|
||||
-- if the label is nil then use the id
|
||||
table.insert(temp_msg, label or id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
msg = table.concat(temp_msg, ', ')
|
||||
end
|
||||
|
||||
return (msg)
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
return multi_select
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
--
|
||||
-- (C) 2019-21 - ntop.org
|
||||
--
|
||||
|
||||
-- ##############################################
|
||||
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/user_script_templates/?.lua;" .. package.path
|
||||
|
||||
-- Import the classes library.
|
||||
local classes = require "classes"
|
||||
-- Make sure to import the Superclass!
|
||||
local user_script_template = require "user_script_template"
|
||||
local http_lint = require "http_lint"
|
||||
local format_utils = require "format_utils"
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local threshold_cross = classes.class(user_script_template)
|
||||
|
||||
-- ##############################################
|
||||
|
||||
threshold_cross.meta = {
|
||||
}
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- @brief Prepare an instance of the template
|
||||
-- @return A table with the template built
|
||||
function threshold_cross:init()
|
||||
-- Call the parent constructor
|
||||
self.super:init()
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
function threshold_cross:parseConfig(script, conf)
|
||||
if(not http_lint.validateOperator(conf.operator)) then
|
||||
return false, "bad operator"
|
||||
end
|
||||
|
||||
if(tonumber(conf.threshold) == nil) then
|
||||
return false, "bad threshold"
|
||||
end
|
||||
|
||||
return true, conf
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
function threshold_cross:describeConfig(script, hooks_conf)
|
||||
local alert_consts = require("alert_consts")
|
||||
local granularities_order = {"min", "5mins", "hour", "day"}
|
||||
local items = {}
|
||||
|
||||
-- E.g. "> 50 Sec (Minute), > 300 Sec (Hourly)"
|
||||
for _, granularity in ipairs(granularities_order) do
|
||||
local hook = hooks_conf[granularity]
|
||||
local granularity = alert_consts.alerts_granularities[granularity]
|
||||
|
||||
if granularity and hook and hook.script_conf.threshold then
|
||||
local unit = ""
|
||||
local op = ternary(hook.script_conf.operator == "gt", ">", "<")
|
||||
|
||||
if(script.gui and script.gui.i18n_field_unit) then
|
||||
unit = " " .. i18n(script.gui.i18n_field_unit)
|
||||
end
|
||||
|
||||
-- Note: it would be desirable to export a 'script.unit' field
|
||||
-- instead of 'script.gui.i18n_field_unit' to properly format
|
||||
-- numeri values as with bytes below.
|
||||
if script.gui.i18n_field_unit == 'field_units.bytes' then
|
||||
local formatted_threshold = format_utils.bytesToSize(hook.script_conf.threshold)
|
||||
items[#items + 1] = string.format("%s (%s)", op,
|
||||
formatted_threshold, i18n(granularity.i18n_title) or granularity.i18n_title)
|
||||
else
|
||||
items[#items + 1] = string.format("%s %s%s (%s)", op,
|
||||
hook.script_conf.threshold, unit, i18n(granularity.i18n_title) or granularity.i18n_title)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return table.concat(items, ", ")
|
||||
end
|
||||
|
||||
-- #######################################################
|
||||
|
||||
return threshold_cross
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
--
|
||||
-- (C) 2013-21 - ntop.org
|
||||
--
|
||||
|
||||
local dirs = ntop.getDirs()
|
||||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
|
||||
|
||||
-- Import the classes library.
|
||||
local classes = require "classes"
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local user_script_template = classes.class()
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function user_script_template:init()
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function user_script_template:parseConfig(script, conf)
|
||||
return true, conf
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function user_script_template:describeConfig(script, hooks_conf)
|
||||
return ''
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
return user_script_template
|
||||
|
||||
-- ##############################################
|
||||
|
|
@ -10,6 +10,7 @@ local dirs = ntop.getDirs()
|
|||
package.path = dirs.installdir .. "/scripts/lua/modules/pools/?.lua;" .. package.path
|
||||
|
||||
require "lua_utils"
|
||||
|
||||
local os_utils = require("os_utils")
|
||||
local json = require("dkjson")
|
||||
local plugins_utils = require("plugins_utils")
|
||||
|
|
@ -586,9 +587,32 @@ end
|
|||
|
||||
-- ##############################################
|
||||
|
||||
local function init_user_script(user_script, mod_fname, full_path, plugin, script_type, subdir)
|
||||
local user_scripts_templates = require("user_scripts_templates")
|
||||
-- @brief Tries and load the script template, returning a new instance (if found)
|
||||
local function loadAndCheckScriptTemplate(script_template)
|
||||
local res
|
||||
local template
|
||||
|
||||
if not script_template then
|
||||
-- Default
|
||||
script_template = "user_script_template"
|
||||
end
|
||||
|
||||
-- Attempt at locating the template class under modules
|
||||
local template_path = dirs.installdir .. "/scripts/lua/modules/user_script_templates/"..script_template..".lua"
|
||||
if ntop.exists(template_path) then
|
||||
-- Require the template file
|
||||
template = require("user_script_templates."..script_template)
|
||||
|
||||
-- Create an instance of the template
|
||||
res = template.new()
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local function init_user_script(user_script, mod_fname, full_path, plugin, script_type, subdir)
|
||||
user_script.key = mod_fname
|
||||
user_script.path = full_path
|
||||
user_script.subdir = subdir
|
||||
|
|
@ -600,8 +624,8 @@ local function init_user_script(user_script, mod_fname, full_path, plugin, scrip
|
|||
user_script.category = checkCategory(user_script.category)
|
||||
user_script.num_filtered = tonumber(ntop.getCache(string.format(NUM_FILTERED_KEY, subdir, mod_fname))) or 0 -- math.random(1000,2000)
|
||||
|
||||
if(user_script.gui and user_script.gui.input_builder) then
|
||||
user_script.template = user_scripts_templates[user_script.gui.input_builder]
|
||||
if user_script.gui then
|
||||
user_script.template = loadAndCheckScriptTemplate(user_script.gui.input_builder)
|
||||
|
||||
if(user_script.template == nil) then
|
||||
traceError(TRACE_WARNING, TRACE_CONSOLE, string.format("Unknown template '%s' for user script '%s'", user_script.gui.input_builder, mod_fname))
|
||||
|
|
@ -616,10 +640,6 @@ local function init_user_script(user_script, mod_fname, full_path, plugin, scrip
|
|||
end
|
||||
end
|
||||
|
||||
if(user_script.template == nil) then
|
||||
user_script.template = user_scripts_templates.default
|
||||
end
|
||||
|
||||
-- Expand hooks
|
||||
if(user_script.hooks["all"] ~= nil) then
|
||||
local callback = user_script.hooks["all"]
|
||||
|
|
|
|||
|
|
@ -1,310 +0,0 @@
|
|||
--
|
||||
-- (C) 2020 - ntop.org
|
||||
--
|
||||
|
||||
local http_lint = require("http_lint")
|
||||
local format_utils = require("format_utils")
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local Template = {}
|
||||
|
||||
function Template:new(key)
|
||||
local obj = {
|
||||
key = key
|
||||
}
|
||||
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
-- @brief Validates and parses the given configuration.
|
||||
-- @return (true,conf) if configuration is valid, (false,errmsg) otherwise
|
||||
function Template:parseConfig(script, conf)
|
||||
traceError(TRACE_WARNING, TRACE_CONSOLE, "Template:parseConfig implementation is missing: " .. self.key)
|
||||
return true, conf
|
||||
end
|
||||
|
||||
-- @brief Get a short string describing the current configuration
|
||||
-- @return a descriptive string
|
||||
function Template:describeConfig(hooks_conf)
|
||||
traceError(TRACE_WARNING, TRACE_CONSOLE, "Template:describeConfig implementation is missing: " .. self.key)
|
||||
return ""
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local DefaultTemplate = {}
|
||||
|
||||
function DefaultTemplate:new()
|
||||
local obj = Template:new("default")
|
||||
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
function DefaultTemplate:parseConfig(script, conf)
|
||||
return true, conf
|
||||
end
|
||||
|
||||
function DefaultTemplate:describeConfig(script, hooks_conf)
|
||||
return ''
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
--
|
||||
-- Threshold cross template
|
||||
--
|
||||
|
||||
local ThresholdCrossTemplate = {}
|
||||
|
||||
function ThresholdCrossTemplate:new()
|
||||
local obj = Template:new("threshold_cross")
|
||||
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
function ThresholdCrossTemplate:parseConfig(script, conf)
|
||||
if(not http_lint.validateOperator(conf.operator)) then
|
||||
return false, "bad operator"
|
||||
end
|
||||
|
||||
if(tonumber(conf.threshold) == nil) then
|
||||
return false, "bad threshold"
|
||||
end
|
||||
|
||||
return true, conf
|
||||
end
|
||||
|
||||
function ThresholdCrossTemplate:describeConfig(script, hooks_conf)
|
||||
local alert_consts = require("alert_consts")
|
||||
local granularities_order = {"min", "5mins", "hour", "day"}
|
||||
local items = {}
|
||||
|
||||
-- E.g. "> 50 Sec (Minute), > 300 Sec (Hourly)"
|
||||
for _, granularity in ipairs(granularities_order) do
|
||||
local hook = hooks_conf[granularity]
|
||||
local granularity = alert_consts.alerts_granularities[granularity]
|
||||
|
||||
if granularity and hook and hook.script_conf.threshold then
|
||||
local unit = ""
|
||||
local op = ternary(hook.script_conf.operator == "gt", ">", "<")
|
||||
|
||||
if(script.gui and script.gui.i18n_field_unit) then
|
||||
unit = " " .. i18n(script.gui.i18n_field_unit)
|
||||
end
|
||||
|
||||
-- Note: it would be desirable to export a 'script.unit' field
|
||||
-- instead of 'script.gui.i18n_field_unit' to properly format
|
||||
-- numeri values as with bytes below.
|
||||
if script.gui.i18n_field_unit == 'field_units.bytes' then
|
||||
local formatted_threshold = format_utils.bytesToSize(hook.script_conf.threshold)
|
||||
items[#items + 1] = string.format("%s (%s)", op,
|
||||
formatted_threshold, i18n(granularity.i18n_title) or granularity.i18n_title)
|
||||
else
|
||||
items[#items + 1] = string.format("%s %s%s (%s)", op,
|
||||
hook.script_conf.threshold, unit, i18n(granularity.i18n_title) or granularity.i18n_title)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return table.concat(items, ", ")
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
--
|
||||
-- Items List
|
||||
--
|
||||
|
||||
local ItemsList = {}
|
||||
|
||||
function ItemsList:new()
|
||||
local obj = Template:new("items_list")
|
||||
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
function ItemsList:parseConfig(script, conf)
|
||||
return http_lint.validateListItems(script, conf)
|
||||
end
|
||||
|
||||
function ItemsList:describeConfig(script, hooks_conf)
|
||||
if((not hooks_conf.all) or (not hooks_conf.all.script_conf)) then
|
||||
return '' -- disabled, nothing to show
|
||||
end
|
||||
|
||||
local items = hooks_conf.all.script_conf.items or {}
|
||||
|
||||
return table.concat(items, ", ")
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
--
|
||||
-- Elephant flows template
|
||||
--
|
||||
|
||||
local ElephantFlowsTemplate = {}
|
||||
|
||||
function ElephantFlowsTemplate:new()
|
||||
local obj = Template:new("elephant_flows")
|
||||
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
function ElephantFlowsTemplate:parseConfig(script, conf)
|
||||
if(tonumber(conf.l2r_bytes_value) == nil) then
|
||||
return false, "bad l2r_bytes_value value"
|
||||
end
|
||||
|
||||
if(tonumber(conf.r2l_bytes_value) == nil) then
|
||||
return false, "bad r2l_bytes_value value"
|
||||
end
|
||||
|
||||
return http_lint.validateListItems(script, conf)
|
||||
end
|
||||
|
||||
function ElephantFlowsTemplate:describeConfig(script, hooks_conf)
|
||||
if not hooks_conf.all then
|
||||
return '' -- disabled, nothing to show
|
||||
end
|
||||
|
||||
-- E.g. '> 1 GB (L2R), > 2 GB (R2L), except: Datatransfer, Git'
|
||||
local conf = hooks_conf.all.script_conf
|
||||
local msg = i18n("user_scripts.elephant_flows_descr", {
|
||||
l2r_bytes = bytesToSize(conf.l2r_bytes_value),
|
||||
r2l_bytes = bytesToSize(conf.r2l_bytes_value),
|
||||
})
|
||||
|
||||
if not table.empty(conf.items) then
|
||||
msg = msg .. ". " .. i18n("user_scripts.exceptions", {exceptions = table.concat(conf.items, ', ')})
|
||||
end
|
||||
|
||||
return(msg)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
--
|
||||
-- MultiSelect template
|
||||
--
|
||||
|
||||
local MultiSelectTemplate = {}
|
||||
|
||||
function MultiSelectTemplate:new()
|
||||
|
||||
local object =Template:new("multi_select")
|
||||
setmetatable(object, self)
|
||||
self.__index = self
|
||||
|
||||
return object
|
||||
end
|
||||
|
||||
function MultiSelectTemplate:parseConfig(script, conf)
|
||||
return http_lint.validateListItems(script, conf)
|
||||
end
|
||||
|
||||
function MultiSelectTemplate:describeConfig(script, hooks_conf)
|
||||
|
||||
if (not hooks_conf.all) then
|
||||
return '' -- disabled, nothing to show
|
||||
end
|
||||
|
||||
local conf = hooks_conf.all.script_conf
|
||||
|
||||
local msg = ''
|
||||
if not table.empty(conf.items) then
|
||||
|
||||
local temp_msg = {}
|
||||
local groups = script.gui.groups
|
||||
|
||||
-- build a string containing selected elements separated by comma
|
||||
for _, group in ipairs(groups) do
|
||||
local elements = group.elements
|
||||
for _, element in ipairs(elements) do
|
||||
|
||||
local id = element[1]
|
||||
local label = element[2]
|
||||
|
||||
if table.has_key(conf.items, id) then
|
||||
-- if the label is nil then use the id
|
||||
table.insert(temp_msg, label or id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
msg = table.concat(temp_msg, ', ')
|
||||
end
|
||||
|
||||
return (msg)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
--
|
||||
-- Long lived template
|
||||
--
|
||||
|
||||
local LongLivedTemplate = {}
|
||||
|
||||
function LongLivedTemplate:new()
|
||||
local obj = Template:new("long_lived")
|
||||
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
function LongLivedTemplate:parseConfig(script, conf)
|
||||
if(tonumber(conf.min_duration) == nil) then
|
||||
return false, "bad min_duration value"
|
||||
end
|
||||
|
||||
return http_lint.validateListItems(script, conf)
|
||||
end
|
||||
|
||||
function LongLivedTemplate:describeConfig(script, hooks_conf)
|
||||
if not hooks_conf.all then
|
||||
return '' -- disabled, nothing to show
|
||||
end
|
||||
|
||||
local conf = hooks_conf.all.script_conf
|
||||
local msg = i18n("user_scripts.long_lived_flows_descr", {
|
||||
duration = secondsToTime(conf.min_duration),
|
||||
})
|
||||
|
||||
if(not table.empty(conf.items)) then
|
||||
msg = msg .. ". " .. i18n("user_scripts.exceptions", {exceptions = table.concat(conf.items, ', ')})
|
||||
end
|
||||
|
||||
return(msg)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- Available templates
|
||||
return {
|
||||
default = DefaultTemplate:new(),
|
||||
|
||||
threshold_cross = ThresholdCrossTemplate:new(),
|
||||
items_list = ItemsList:new(),
|
||||
elephant_flows = ElephantFlowsTemplate:new(),
|
||||
long_lived = LongLivedTemplate:new(),
|
||||
multi_select = MultiSelectTemplate:new()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue