Reworks implementation of {DNS,HTTP}RepliesRequestsRatio

This commit is contained in:
Simone Mainardi 2021-04-08 10:02:35 +02:00
parent 32a2778f28
commit 7d7e84a7a9
19 changed files with 230 additions and 189 deletions

View file

@ -16,13 +16,13 @@ local alert = require "alert"
-- ##############################################
local host_alert_replies_requests_ratio = classes.class(alert)
local host_alert_dns_replies_requests_ratio = classes.class(alert)
-- ##############################################
host_alert_replies_requests_ratio.meta = {
alert_key = host_alert_keys.host_alert_replies_requests_ratio,
i18n_title = "entity_thresholds.request_reply_ratio_title",
host_alert_dns_replies_requests_ratio.meta = {
alert_key = host_alert_keys.host_alert_dns_replies_requests_ratio,
i18n_title = "entity_thresholds.dns_request_reply_ratio_title",
icon = "fas fa-exclamation",
}
@ -32,7 +32,7 @@ host_alert_replies_requests_ratio.meta = {
-- @param requests The number of requests
-- @param replies The number of replies
-- @return A table with the alert built
function host_alert_replies_requests_ratio:init(requests, replies)
function host_alert_dns_replies_requests_ratio:init(requests, replies)
-- Call the parent constructor
self.super:init()
@ -49,33 +49,19 @@ end
-- @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 host_alert_replies_requests_ratio.format(ifid, alert, alert_type_params)
function host_alert_dns_replies_requests_ratio.format(ifid, alert, alert_type_params)
local alert_consts = require("alert_consts")
local entity = firstToUpper(alert_consts.formatAlertEntity(ifid, alert_consts.alertEntityRaw(alert["alert_entity"]), alert["alert_entity_val"]))
local ratio
if((alert_type_params.replies ~= nil) and (alert_type_params.requests ~= nil)) then
ratio = round(math.min((alert_type_params.replies * 100) / (alert_type_params.requests + 1), 100), 1)
else
ratio = 1
end
local labels = {i18n("alerts_dashboard.reqs_repls_ratio_for", {
entity = entity,
host_category = format_utils.formatAddressCategory((json.decode(alert.alert_json)).alert_generation.host_info)})}
if alert_type_params.dns_sent_rcvd_ratio < alert_type_params.ratio then
labels[#labels + 1] = i18n("alerts_dashboard.dns_sent_rcvd_ratio", { ratio = alert_type_params.dns_sent_rcvd_ratio, threshold = alert_type_params.ratio})
end
if alert_type_params.dns_rcvd_sent_ratio < alert_type_params.ratio then
labels[#labels + 1] = i18n("alerts_dashboard.dns_rcvd_sent_ratio", { ratio = alert_type_params.dns_rcvd_sent_ratio, threshold = alert_type_params.ratio})
end
if alert_type_params.http_sent_rcvd_ratio < alert_type_params.ratio then
labels[#labels + 1] = i18n("alerts_dashboard.http_sent_rcvd_ratio", { ratio = alert_type_params.http_sent_rcvd_ratio, threshold = alert_type_params.ratio})
end
if alert_type_params.http_rcvd_sent_ratio < alert_type_params.ratio then
labels[#labels + 1] = i18n("alerts_dashboard.http_rcvd_sent_ratio", { ratio = alert_type_params.http_rcvd_sent_ratio, threshold = alert_type_params.ratio})
if not alert_type_params.is_sent_rcvd then -- Responses received vs. Requests Sent
labels[#labels + 1] = i18n("alerts_dashboard.dns_rcvd_sent_ratio", { ratio = alert_type_params.ratio, threshold = alert_type_params.ratio_threshold, replies = formatValue(alert_type_params.replies), requests = formatValue(alert_type_params.requests) })
else -- Responses sent vs. Requests Received
labels[#labels + 1] = i18n("alerts_dashboard.dns_sent_rcvd_ratio", { ratio = alert_type_params.ratio, threshold = alert_type_params.ratio_threshold, replies = formatValue(alert_type_params.replies), requests = formatValue(alert_type_params.requests) })
end
return table.concat(labels, " ")
@ -83,4 +69,4 @@ end
-- #######################################################
return host_alert_replies_requests_ratio
return host_alert_dns_replies_requests_ratio

View file

@ -1,67 +0,0 @@
--
-- (C) 2019-21 - ntop.org
--
-- ##############################################
local host_alert_keys = require "host_alert_keys"
-- Import the classes library.
local classes = require "classes"
-- Make sure to import the Superclass!
local alert = require "alert"
-- ##############################################
local host_alert_dns_requests_errors_ratio = classes.class(alert)
-- ##############################################
host_alert_dns_requests_errors_ratio.meta = {
alert_key = host_alert_keys.host_alert_dns_requests_errors_ratio,
i18n_title = "dns_positive_error_ratio.title",
icon = "fas fa-exclamation",
}
-- ##############################################
-- @brief Prepare an alert table used to generate the alert
-- @param requests The number of requests
-- @param replies The number of replies
-- @return A table with the alert built
function host_alert_dns_requests_errors_ratio:init(type, positives, errors)
-- Call the parent constructor
self.super:init()
self.alert_type_params = {
type = type,
positives = positives,
errors = errors,
}
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 host_alert_dns_requests_errors_ratio.format(ifid, alert, alert_type_params)
local type = ""
if alert_type_params.type == "dns_rcvd" then
type = "Received"
else
type = "Sent"
end
return(i18n("dns_positive_error_ratio.positive_error_ratio_descr", {
type = type,
positives = alert_type_params.positives,
errors = alert_type_params.errors,
}))
end
-- #######################################################
return host_alert_dns_requests_errors_ratio

View file

@ -0,0 +1,72 @@
--
-- (C) 2019-21 - ntop.org
--
-- ##############################################
local host_alert_keys = require "host_alert_keys"
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
local format_utils = require "format_utils"
local json = require("dkjson")
-- Import the classes library.
local classes = require "classes"
-- Make sure to import the Superclass!
local alert = require "alert"
-- ##############################################
local host_alert_http_replies_requests_ratio = classes.class(alert)
-- ##############################################
host_alert_http_replies_requests_ratio.meta = {
alert_key = host_alert_keys.host_alert_http_replies_requests_ratio,
i18n_title = "entity_thresholds.http_request_reply_ratio_title",
icon = "fas fa-exclamation",
}
-- ##############################################
-- @brief Prepare an alert table used to generate the alert
-- @param requests The number of requests
-- @param replies The number of replies
-- @return A table with the alert built
function host_alert_http_replies_requests_ratio:init(requests, replies)
-- Call the parent constructor
self.super:init()
self.alert_type_params = {
requests = requests,
replies = replies,
}
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 host_alert_http_replies_requests_ratio.format(ifid, alert, alert_type_params)
local alert_consts = require("alert_consts")
local entity = firstToUpper(alert_consts.formatAlertEntity(ifid, alert_consts.alertEntityRaw(alert["alert_entity"]), alert["alert_entity_val"]))
local labels = {i18n("alerts_dashboard.reqs_repls_ratio_for", {
entity = entity,
host_category = format_utils.formatAddressCategory((json.decode(alert.alert_json)).alert_generation.host_info)})}
if not alert_type_params.is_sent_rcvd then -- Responses received vs. Requests Sent
labels[#labels + 1] = i18n("alerts_dashboard.http_rcvd_sent_ratio", { ratio = alert_type_params.ratio, threshold = alert_type_params.ratio_threshold, replies = formatValue(alert_type_params.replies), requests = formatValue(alert_type_params.requests) })
else -- Responses sent vs. Requests Received
labels[#labels + 1] = i18n("alerts_dashboard.http_sent_rcvd_ratio", { ratio = alert_type_params.ratio, threshold = alert_type_params.ratio_threshold, replies = formatValue(alert_type_params.replies), requests = formatValue(alert_type_params.requests) })
end
return table.concat(labels, " ")
end
-- #######################################################
return host_alert_http_replies_requests_ratio