mirror of
https://github.com/ntop/ntopng.git
synced 2026-04-29 07:29:32 +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
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue