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,52 +2,34 @@
-- (C) 2019-20 - ntop.org
--
-- ##############################################
local alert_keys = require "alert_keys"
local json = require "dkjson"
local alert_keys = require "alert_keys"
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 json = require("dkjson")
-- ##############################################
local alert_iec104_error = classes.class(alert)
-- ##############################################
alert_iec104_error.meta = {
alert_key = alert_keys.ntopng.alert_iec104_error,
i18n_title = "alerts_dashboard.iec104_error",
icon = "fas fa-subway",
}
-- ##############################################
-- #######################################################
-- @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 last_error A string with the lastest influxdb error
-- @return A table with the alert built
function alert_iec104_error:init(last_error)
-- Call the paren constructor
self.super:init()
self.alert_type_params = {
error_msg = last_error
local function createIEC104Error(alert_severity, alert_granularity, alert_subtype, last_error)
local threshold_type = {
alert_severity = alert_severity,
alert_subtype = alert_subtype,
alert_granularity = alert_granularity,
alert_type_params = {
error_msg = last_error
},
}
return threshold_type
end
-- #######################################################
-- @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_iec104_error.format(ifid, alert, alert_type_params)
local msg = json.decode(alert_type_params.error_msg)
local function formatIEC104ErrorMessage(ifid, alert, status)
local msg = json.decode(status.error_msg)
local vlanId = alert.vlanId or 0
local client = ip2label(msg.client.ip, msg.vlanId)
local server = ip2label(msg.server.ip, msg.vlanId)
@ -71,4 +53,10 @@ end
-- #######################################################
return alert_iec104_error
return {
alert_key = alert_keys.ntopng.alert_iec104_error,
i18n_title = "alerts_dashboard.iec104_error",
i18n_description = formatIEC104ErrorMessage,
icon = "fas fa-subway",
creator = createIEC104Error,
}