Implement user scripts templates

This commit is contained in:
emanuele-f 2020-01-23 17:31:39 +01:00
parent 30ff92d472
commit 040afcb4bb
3 changed files with 81 additions and 16 deletions

View file

@ -50,9 +50,12 @@ function DefaultTemplate:parseConfig(script, conf)
return true, conf
end
function DefaultTemplate:describeConfig(hooks_conf)
-- TODO
return ""
function DefaultTemplate:describeConfig(script, hooks_conf)
if(hooks_conf.all and hooks_conf.all.enabled) then
return i18n("enabled")
end
return i18n("disabled")
end
-- ##############################################
@ -84,9 +87,37 @@ function ThresholdCrossTemplate:parseConfig(script, conf)
return true, conf
end
function ThresholdCrossTemplate:describeConfig(hooks_conf)
-- TODO
return ""
function ThresholdCrossTemplate:describeConfig(script, hooks_conf)
local alert_consts = require("alert_consts")
local granularities_order = {"min", "5mins", "hour", "day"}
local items = {}
local is_enabled = false
-- 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.enabled 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
items[#items + 1] = string.format("%s %s%s (%s)", op,
hook.script_conf.threshold, unit, i18n(granularity.i18n_title) or granularity.i18n_title)
is_enabled = true
end
end
if is_enabled then
return table.concat(items, ", ")
else
return i18n("disabled")
end
end
-- ##############################################
@ -110,9 +141,14 @@ function ItemsList:parseConfig(script, conf)
return http_lint.validateListItems(script, conf)
end
function ItemsList:describeConfig(hooks_conf)
-- TODO
return ""
function ItemsList:describeConfig(script, hooks_conf)
if(not hooks_conf.all) or (not hooks_conf.all.enabled) then
return i18n("disabled")
end
local items = hooks_conf.all.script_conf.items or {}
return table.concat(items, ", ")
end
-- ##############################################
@ -144,9 +180,23 @@ function ElephantFlowsTemplate:parseConfig(script, conf)
return http_lint.validateListItems(script, conf)
end
function ThresholdCrossTemplate:describeConfig(hooks_conf)
-- TODO
return ""
function ElephantFlowsTemplate:describeConfig(script, hooks_conf)
if(not hooks_conf.all) or (not hooks_conf.all.enabled) then
return i18n("disabled")
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
-- ##############################################
@ -174,9 +224,21 @@ function LongLivedTemplate:parseConfig(script, conf)
return http_lint.validateListItems(script, conf)
end
function LongLivedTemplate:describeConfig(hooks_conf)
-- TODO
return ""
function LongLivedTemplate:describeConfig(script, hooks_conf)
if(not hooks_conf.all) or (not hooks_conf.all.enabled) then
return i18n("disabled")
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
-- ##############################################