Rework network threshold alerts definition

This commit is contained in:
emanuele-f 2019-07-11 19:35:34 +02:00
parent 55cf8ffcf4
commit e26a7949ac
5 changed files with 105 additions and 129 deletions

View file

@ -12,6 +12,7 @@ alerts_api = require("alerts_api")
local do_trace = false
local config_alerts = nil
local ifname = nil
local available_modules = nil
-- The function below ia called once (#pragma once)
function setup(str_granularity)
@ -20,76 +21,7 @@ function setup(str_granularity)
config_alerts = getNetworksConfiguredAlertThresholds(ifname, str_granularity)
-- Load the threshold checking functions
package.path = dirs.installdir .. "/scripts/callbacks/interface/alerts/network/?.lua;" .. package.path
checks = require("check")
end
-- #################################################################
-- The function below is called once per network
local function checkNetworkAlertsThreshold(network_key, network_info, granularity, num_granularity, rules)
if do_trace then print("checkNetworkAlertsThreshold()\n") end
for function_name, params in pairs(rules) do
-- IMPORTANT: do not use "local" with the variables below
-- as they need to be accessible by the evaluated function
threshold_value = params["edge"]
alert_key_name = params["key"]
threshold_operator = params["operator"]
metric_name = params["metric"]
threshold_gran = granularity
threshold_num_gran = num_granularity
n_info = network_info
if do_trace then print("[Alert @ "..granularity.."] ".. network_key .." ["..function_name.."]\n") end
if true then
-- This is where magic happens: load() evaluates the string
local what = 'return checks.'..function_name..'(metric_name, n_info, threshold_gran, threshold_num_gran)'
local func, err = load(what, 't')
if func then
local ok, value = pcall(func)
if ok then
local alarmed = false
local network_alert = alerts_api:newAlert({entity = "network", type = "threshold_cross", severity = "error"})
if do_trace then print("Execution OK. value: "..tostring(value)..", operator: "..threshold_operator..", threshold: "..threshold_value.."]\n") end
threshold_value = tonumber(threshold_value)
if threshold_operator == "lt" then
if value < threshold_value then alarmed = true end
else
if value > threshold_value then alarmed = true end
end
if alarmed then
if do_trace then print("Trigger alert [value: "..tostring(value).."]\n") end
alerts_api.new_trigger(
alerts_api.networkAlertEntity(network_key),
alerts_api.thresholdCrossType(granularity, function_name, value, threshold_operator, threshold_value)
)
else
if do_trace then print("DON'T trigger alert [value: "..tostring(value).."]\n") end
alerts_api.new_trigger(
alerts_api.networkAlertEntity(network_key),
alerts_api.thresholdCrossType(granularity, function_name, value, threshold_operator, threshold_value)
)
end
else
if do_trace then print("Execution error: "..tostring(rc).."\n") end
end
else
print("Compilation error:", err)
end
end
if do_trace then print("=============\n") end
end
available_modules = alerts_api.load_check_modules("network")
end
-- #################################################################
@ -103,17 +35,23 @@ function checkNetworkAlerts(granularity)
return
end
local network_alert = config_alerts[network_key]
local num_granularity = granularity2id(granularity)
local network_config = config_alerts[network_key]
local global_config = config_alerts["local_networks"]
local has_configured_alerts = (table.len(network_config or global_config or {}) > 0)
-- specific network alerts
if network_alert and table.len(network_alert) > 0 then
checkNetworkAlertsThreshold(network_key, info, granularity, num_granularity, network_alert)
end
if(has_configured_alerts) then
for _, check in pairs(available_modules) do
local config = network_config[check.key] or global_config[check.key]
-- generic network alerts
network_alert = config_alerts["local_networks"]
if network_alert and table.len(network_alert) > 0 then
checkNetworkAlertsThreshold(network_key, info, granularity, num_granularity, network_alert)
if config then
check.check_function({
granularity = granularity,
alert_entity = alerts_api.networkAlertEntity(network_key),
entity_info = info,
alert_config = config,
check_module = check,
})
end
end
end
end