Added critical and emergency status to alerts

This commit is contained in:
MatteoBiscosi 2022-10-19 10:18:36 +02:00
parent fb7da8667e
commit 692ae0bfcc
27 changed files with 178 additions and 76 deletions

View file

@ -13,9 +13,9 @@ local json = require "dkjson"
local script_manager = require("script_manager")
local endpoints = require("endpoints")
local checks = require("checks")
local alert_severities = require "alert_severities"
local alert_entities = require "alert_entities"
local am_utils = require "am_utils"
local alert_consts = require "alert_consts"
local host_pools = require "host_pools":create()
sendHTTPContentTypeHeader('text/html')
@ -127,7 +127,7 @@ local context = {
can_create_recipient = can_create_recipient,
check_categories = checks.check_categories,
check_entities = alert_entities,
alert_severities = alert_severities,
alert_severities = alert_consts.get_printable_severities(),
endpoints = endpoint_list,
endpoints_info = get_max_configs_available(),
am_hosts = am_hosts_list,

View file

@ -14,7 +14,6 @@ local template_utils = require "template_utils"
local widget_gui_utils = require "widget_gui_utils"
local tag_utils = require "tag_utils"
local alert_entities = require "alert_entities"
local alert_severities = require "alert_severities"
local Datasource = widget_gui_utils.datasource
local alert_store_utils = require "alert_store_utils"
local alert_utils = require "alert_utils"
@ -606,7 +605,7 @@ local filters_context = {
alert_utils = alert_utils,
alert_consts = alert_consts,
available_types = available_filter_types,
severities = alert_severities,
severities = alert_consts.get_printable_severities(),
alert_types = all_alert_types,
l7_protocols = interface.getnDPIProtocols(),
operators_by_filter = operators_by_filter,

View file

@ -294,7 +294,7 @@ print[[
}
if(rsp.alerted_flows_error > 0 && !(systemInterfaceEnabled)) {
msg += "<a href=\"]] print (ntop.getHttpPrefix()) print [[/lua/flows_stats.lua?alert_type_severity=error_or_higher\">"
msg += "<a href=\"]] print (ntop.getHttpPrefix()) print [[/lua/flows_stats.lua?alert_type_severity=error\">"
msg += "<span class=\"badge bg-danger\" title=']] print(i18n("flow_details.dangerous_flows")) print[['>"+NtopUtils.formatValue(rsp.alerted_flows_error, 1)+ " <i class=\"fas fa-stream\"></i> ]] print[[ <i class=\"fas fa-exclamation-triangle\"></i></span></a>";
}

View file

@ -119,6 +119,8 @@ end
function Alert:set_score_notice() self.score = ntop.mapSeverityToScore(alert_severities.notice.severity_id) end
function Alert:set_score_warning() self.score = ntop.mapSeverityToScore(alert_severities.warning.severity_id) end
function Alert:set_score_error() self.score = ntop.mapSeverityToScore(alert_severities.error.severity_id) end
function Alert:set_score_critical() self.score = ntop.mapSeverityToScore(alert_severities.critical.severity_id) end
function Alert:set_score_emergency() self.score = ntop.mapSeverityToScore(alert_severities.emergency.severity_id) end
-- ##############################################

View file

@ -38,22 +38,30 @@ alert_consts.ALL_ALERT_KEY = 0 -- Special ID to select 'all' alerts
-- NOTE: keep it in sync with ntop_typedefs.h AlertLevelGroup
--
alert_consts.severity_groups = {
group_none = {
severity_group_id = 0,
i18n_title = "severity_groups.group_none",
},
notice_or_lower = {
severity_group_id = 1,
i18n_title = "severity_groups.group_notice_or_lower",
},
warning = {
severity_group_id = 2,
i18n_title = "severity_groups.group_warning",
},
error_or_higher = {
severity_group_id = 3,
i18n_title = "severity_groups.group_error_or_higher",
},
group_none = {
severity_group_id = 0,
i18n_title = "severity_groups.group_none",
},
notice_or_lower = {
severity_group_id = 1,
i18n_title = "severity_groups.group_notice_or_lower",
},
warning = {
severity_group_id = 2,
i18n_title = "severity_groups.group_warning",
},
error = {
severity_group_id = 3,
i18n_title = "severity_groups.group_error",
},
critical = {
severity_group_id = 4,
i18n_title = "severity_groups.group_critical",
},
emergency = {
severity_group_id = 5,
i18n_title = "severity_groups.group_emergency",
},
}
-- ##############################################
@ -576,7 +584,21 @@ function alert_consts.alertSeverityRaw(severity_id)
return alert_severities_id_to_key[severity_id]
end
-- ################################################################################
-- ################################################################################
function alert_consts.get_printable_severities()
local severities = {}
for name, conf in pairs(alert_severities, "severity_id", asc) do
if (conf.severity_id > 2) and (conf.severity_id < 7) then
severities[name] = conf
end
end
return severities
end
-- ################################################################################
function alert_consts.alertSeverityLabel(score, nohtml, emoji)
local severity_id = alert_consts.alertSeverityRaw(map_score_to_severity(score))

View file

@ -74,7 +74,9 @@ function all_alert_store:__add_alert_stats(alert, alerts_by_entity, alerts_by_en
score = 0,
count_group_notice_or_lower = 0,
count_group_warning = 0,
count_group_error_or_higher = 0,
count_group_error = 0,
count_group_critical = 0,
count_group_emergency = 0,
count = 0,
tstamp = 0,
tstamp_end = 0,
@ -87,15 +89,18 @@ function all_alert_store:__add_alert_stats(alert, alerts_by_entity, alerts_by_en
alerts_by_entity[entity_id].score = alerts_by_entity[entity_id].score + alert.score
alerts_by_entity[entity_id].count = alerts_by_entity[entity_id].count + 1
local count_group
if alert.severity <= alert_severities.notice.severity_id then
count_group = "count_group_notice_or_lower"
elseif alert.severity == alert_severities.warning.severity_id then
count_group = "count_group_warning"
elseif alert.severity >= alert_severities.error.severity_id then
count_group = "count_group_error_or_higher"
end
if alert.severity <= alert_severities.notice.severity_id then
count_group = "count_group_notice_or_lower"
elseif alert.severity == alert_severities.warning.severity_id then
count_group = "count_group_warning"
elseif alert.severity == alert_severities.error.severity_id then
count_group = "count_group_error"
elseif alert.severity == alert_severities.critical.severity_id then
count_group = "count_group_critical"
elseif alert.severity >= alert_severities.emergency.severity_id then
count_group = "count_group_emergency"
end
alerts_by_entity[entity_id][count_group] = alerts_by_entity[entity_id][count_group] + 1
end
@ -136,7 +141,7 @@ function all_alert_store:select_engaged(filter)
if self._order_by and self._order_by.sort_column and alert[self._order_by.sort_column] then
sort_2_col[#sort_2_col + 1] = {idx = idx, val = tonumber(alert[self._order_by.sort_column]) or alert[self._order_by.sort_column]}
else
sort_2_col[#sort_2_col + 1] = {idx = idx, val = count_group_error_or_higher}
sort_2_col[#sort_2_col + 1] = {idx = idx, val = count_group_error}
end
total_rows = total_rows + 1
@ -207,13 +212,17 @@ function all_alert_store:select_historical(filter, fields)
local q = string.format(" SELECT entity_id, SUM(score) score, "..
"SUM(group_notice_or_lower) count_group_notice_or_lower, "..
"SUM(group_warning) count_group_warning, "..
"SUM(group_error_or_higher) count_group_error_or_higher, "..
"SUM(group_error) count_group_error, "..
"SUM(group_critical) count_group_critical, "..
"SUM(group_emergency) count_group_emergency, "..
"COUNT(*) count, "..
"0 tstamp, 0 tstamp_end, '{}' json FROM "..
" (SELECT entity_id, score, "..
" CASE WHEN severity <= 3 THEN 1 ELSE 0 END AS group_notice_or_lower, "..
" CASE WHEN severity = 4 THEN 1 ELSE 0 END AS group_warning, "..
" CASE WHEN severity >= 5 THEN 1 ELSE 0 END AS group_error_or_higher, "..
" CASE WHEN severity = 5 THEN 1 ELSE 0 END AS group_error, "..
" CASE WHEN severity = 6 THEN 1 ELSE 0 END AS group_critical, "..
" CASE WHEN severity >= 7 THEN 1 ELSE 0 END AS group_emergency, "..
" score FROM `%s` WHERE %s) "..
"GROUP BY entity_id %s %s %s ",
self._table_name, where_clause, order_by_clause, limit_clause, offset_clause)
@ -314,7 +323,9 @@ local RNAME = {
SCORE = { name = "score", export = true},
COUNT_GROUP_NOTICE_OR_LOWER = { name = "count_group_notice_or_lower", export = true},
COUNT_GROUP_WARNING = { name = "count_group_warning", export = true},
COUNT_GROUP_ERROR_OR_HIGHER = { name = "count_group_error_or_higher", export = true},
COUNT_GROUP_ERROR = { name = "count_group_error", export = true},
COUNT_GROUP_CRITICAL = { name = "count_group_critical", export = true},
COUNT_GROUP_EMERGENCY = { name = "count_group_emergency", export = true},
}
function all_alert_store:get_rnames()
@ -359,12 +370,24 @@ function all_alert_store:format_record(value, no_html)
url = url.."&severity=4" .. tag_utils.SEPARATOR .. "eq",
}
record[RNAME.COUNT_GROUP_ERROR_OR_HIGHER.name] = {
value = value["count_group_error_or_higher"],
record[RNAME.COUNT_GROUP_ERROR.name] = {
value = value["count_group_error"],
color = alert_severities.error.color,
url = url.."&severity=5" .. tag_utils.SEPARATOR .. "gte",
}
record[RNAME.COUNT_GROUP_CRITICAL.name] = {
value = value["count_group_critical"],
color = alert_severities.critical.color,
url = url.."&severity=5" .. tag_utils.SEPARATOR .. "gte",
}
record[RNAME.COUNT_GROUP_EMERGENCY.name] = {
value = value["count_group_emergency"],
color = alert_severities.emergency.color,
url = url.."&severity=5" .. tag_utils.SEPARATOR .. "gte",
}
return record
end

View file

@ -758,6 +758,7 @@ function am_utils.triggerAlert(numeric_ip, ip_label, current_value, upper_thresh
-- Unreachable
local host, measurement = key2amhost(ip_label)
local info = am_utils.getMeasurementInfo(measurement)
type_info:set_score_critical()
if info and info.unreachable_alert_i18n then
-- The measurement provides an alternative message for the alert

View file

@ -21,7 +21,7 @@ local function check_interface_activity(params)
local no_if_activity_type = alert_consts.alert_types.alert_no_if_activity.new(params.entity_info.name)
no_if_activity_type:set_score_error()
no_if_activity_type:set_score_critical()
no_if_activity_type:set_subtype(params.entity_info.name)
no_if_activity_type:set_granularity(params.granularity)

View file

@ -21,7 +21,7 @@ local function check_slow_periodic_activity(params)
(ps_stats["max_duration_secs"] or 0) * 1000
)
alert:set_score_warning()
alert:set_score_error()
alert:set_granularity(params.granularity)
alert:set_subtype(ps_name)

View file

@ -33,7 +33,7 @@ local function check_interface_idle(params)
threshold
)
alert:set_score_warning()
alert:set_score_error()
alert:set_subtype(getInterfaceName(interface.getId()))
alert:set_granularity(params.granularity)

View file

@ -20,7 +20,7 @@ local function check_periodic_activity_not_executed(params)
ps_stats["last_queued_time"] or 0
)
alert:set_score_warning()
alert:set_score_error()
alert:set_granularity(params.granularity)
alert:set_subtype(ps_name)
if delta > 0 then

View file

@ -503,7 +503,7 @@ local function validateAlertType(mode)
end
local function validateAlertTypeSeverity(mode)
local modes = {"group_none", "notice_or_lower", "warning", "error_or_higher"}
local modes = {"group_none", "notice_or_lower", "warning", "error", "critical", "emergency"}
return validateChoice(modes, mode)
end

View file

@ -661,8 +661,7 @@ function tag_utils.get_tag_info(id, entity)
elseif tag.value_type == "severity" then
filter.value_type = 'array'
filter.options = {}
local severities = alert_severities
for _, severity in pairsByValues(severities, alert_utils.severity_rev) do
for _, severity in pairsByField(alert_consts.get_printable_severities(), "severity_id", asc) do
filter.options[#filter.options+1] = {
value = severity.severity_id,
label = i18n(severity.i18n_title),