Migrates alerts to an object-oriented implementation

This commit is contained in:
matteo 2020-12-22 12:58:51 +01:00
parent c1a7ff08ae
commit fbc283f12f
108 changed files with 2793 additions and 1737 deletions

View file

@ -2,53 +2,63 @@
-- (C) 2019-20 - ntop.org
--
-- ##############################################
local dirs = ntop.getDirs()
local alert_keys = require "alert_keys"
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
local format_utils = require "format_utils"
-- #######################################################
-- Import the classes library.
local classes = require "classes"
-- Make sure to import the Superclass!
local alert = require "alert"
-- ##############################################
local alert_slow_periodic_activity = classes.class(alert)
-- ##############################################
alert_slow_periodic_activity.meta = {
alert_key = alert_keys.ntopng.alert_slow_periodic_activity,
i18n_title = "alerts_dashboard.slow_periodic_activity",
icon = "fas fa-undo",
}
-- ##############################################
-- @brief Prepare an alert table used to generate the alert
-- @param alert_severity A severity as defined in `alert_severities`
-- @param alert_granularity A granularity as defined in `alert_consts.alerts_granularities`
-- @param ps_name A string with the name of the periodic activity
-- @param max_duration_ms The maximum duration taken by this periodic activity to run, in milliseconds
-- @return A table with the alert built
local function createSlowPeriodicActivity(alert_severity, alert_granularity, ps_name, max_duration_ms)
local threshold_type = {
alert_severity = alert_severity,
alert_granularity = alert_granularity,
alert_subtype = ps_name,
alert_type_params = {
ps_name = ps_name,
max_duration_ms = max_duration_ms,
},
}
function alert_slow_periodic_activity:init(ps_name, max_duration_ms)
-- Call the paren constructor
self.super:init()
return threshold_type
self.alert_type_params = {
ps_name = ps_name,
max_duration_ms = max_duration_ms,
}
end
-- #######################################################
local function slowPeriodicActivityFormatter(ifid, alert, info)
-- @brief Format an alert into a human-readable string
-- @param ifid The integer interface id of the generated alert
-- @param alert The alert description table, including alert data such as the generating entity, timestamp, granularity, type
-- @param alert_type_params Table `alert_type_params` as built in the `:init` method
-- @return A human-readable string
function alert_slow_periodic_activity.format(ifid, alert, alert_type_params)
local max_duration
max_duration = format_utils.secondsToTime(info.max_duration_ms / 1000)
max_duration = format_utils.secondsToTime(alert_type_params.max_duration_ms / 1000)
return(i18n("alert_messages.slow_periodic_activity", {
script = info.ps_name,
script = alert_type_params.ps_name,
max_duration = max_duration,
}))
end
-- #######################################################
return {
alert_key = alert_keys.ntopng.alert_slow_periodic_activity,
i18n_title = "alerts_dashboard.slow_periodic_activity",
i18n_description = slowPeriodicActivityFormatter,
icon = "fas fa-undo",
creator = createSlowPeriodicActivity,
}
return alert_slow_periodic_activity