Recheck the alert when a monitored host config changes

This commit is contained in:
emanuele-f 2020-04-21 10:33:06 +02:00
parent 073cc7cf61
commit c750a8422e
4 changed files with 109 additions and 76 deletions

View file

@ -8,7 +8,7 @@ package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local json = require("dkjson")
local plugins_utils = require("plugins_utils")
local active_monitoring_utils = plugins_utils.loadModule("active_monitoring", "am_utils")
local am_utils = plugins_utils.loadModule("active_monitoring", "am_utils")
sendHTTPContentTypeHeader('application/json')
@ -74,29 +74,29 @@ end
if(action == "add") then
local existing
local am_value = _POST["threshold"]
local threshold = _POST["threshold"]
local granularity = _POST["granularity"]
local url = active_monitoring_utils.formatAmHost(host, measurement)
local url = am_utils.formatAmHost(host, measurement)
if(isValidHostMeasurementCombination(host, measurement) == false) then
-- NOTE: reportError already called
return
end
existing = active_monitoring_utils.hasHost(host, measurement)
existing = am_utils.hasHost(host, measurement)
if existing then
reportError(i18n("active_monitoring_stats.host_exists", {host=url}))
return
end
active_monitoring_utils.addHost(host, measurement, am_value, granularity)
am_utils.addHost(host, measurement, threshold, granularity)
rv.message = i18n("active_monitoring_stats.host_add_ok", {host=url})
elseif(action == "edit") then
local existing
local am_value = _POST["threshold"]
local threshold = _POST["threshold"]
local granularity = _POST["granularity"]
local url = active_monitoring_utils.formatAmHost(host, measurement)
local url = am_utils.formatAmHost(host, measurement)
local old_am_host = _POST["old_am_host"]
local old_measurement = _POST["old_measurement"]
local old_granularity = _POST["old_granularity"]
@ -121,9 +121,9 @@ elseif(action == "edit") then
return
end
local old_url = active_monitoring_utils.formatAmHost(old_am_host, old_measurement)
local old_url = am_utils.formatAmHost(old_am_host, old_measurement)
existing = active_monitoring_utils.hasHost(old_am_host, old_measurement)
existing = am_utils.hasHost(old_am_host, old_measurement)
if not existing then
reportError(i18n("active_monitoring_stats.host_not_exists", {host=old_url}))
@ -132,35 +132,60 @@ elseif(action == "edit") then
if((old_am_host ~= host) or (old_measurement ~= measurement)) then
-- The key has changed, delete the old host and create a new one
existing = active_monitoring_utils.hasHost(host, measurement)
existing = am_utils.hasHost(host, measurement)
if existing then
reportError(i18n("active_monitoring_stats.host_exists", {host=url}))
return
end
active_monitoring_utils.deleteHost(old_am_host, old_measurement) -- also calls discardHostTimeseries
active_monitoring_utils.addHost(host, measurement, am_value, granularity)
am_utils.deleteHost(old_am_host, old_measurement) -- also calls discardHostTimeseries
am_utils.addHost(host, measurement, threshold, granularity)
else
-- The key is the same, only update the value/granularity
if(old_granularity ~= granularity) then
-- Need to discard the old timeseries as the granularity has changed
active_monitoring_utils.discardHostTimeseries(host, measurement)
am_utils.discardHostTimeseries(host, measurement)
end
active_monitoring_utils.addHost(host, measurement, am_value, granularity)
am_utils.addHost(host, measurement, threshold, granularity)
local m_info = am_utils.getMeasurementInfo(measurement)
local last_update = am_utils.getLastAmUpdate(host, measurement)
if m_info and last_update then
-- Recheck the threshold
local key = am_utils.getAmHostKey(host, measurement)
local value = last_update.value
threshold = tonumber(threshold)
-- NOTE: system interface must be manually sected and then unselected
local old_iface = tostring(interface.getId())
interface.select(getSystemInterfaceId())
-- Always release the old alert because:
-- - If the granularity has changed, since the alert is bound to a specific granularity, it must be released
-- - If the threshold has changed, we want to change the threshold in the alert message (if engaged)
am_utils.releaseAlert(last_update.ip, key, value, threshold, old_granularity)
if am_utils.hasExceededThreshold(threshold, m_info.operator, value) then
am_utils.triggerAlert(last_update.ip, key, value, threshold, granularity)
end
interface.select(old_iface)
end
end
rv.message = i18n("active_monitoring_stats.host_edit_ok", {host=old_url})
elseif(action == "delete") then
local url = active_monitoring_utils.formatAmHost(host, measurement)
local existing = active_monitoring_utils.hasHost(host, measurement)
local url = am_utils.formatAmHost(host, measurement)
local existing = am_utils.hasHost(host, measurement)
if not existing then
reportError(i18n("active_monitoring_stats.host_not_exists", {host=url}))
end
active_monitoring_utils.deleteHost(host, measurement)
am_utils.deleteHost(host, measurement)
rv.message = i18n("active_monitoring_stats.host_delete_ok", {host=url})
else
reportError(i18n("active_monitoring_stats.bad_action_param"))

View file

@ -9,7 +9,7 @@ require "lua_utils"
local format_utils = require("format_utils")
local json = require("dkjson")
local plugins_utils = require("plugins_utils")
local active_monitoring_utils = plugins_utils.loadModule("active_monitoring", "am_utils")
local am_utils = plugins_utils.loadModule("active_monitoring", "am_utils")
sendHTTPContentTypeHeader('application/json')
@ -17,13 +17,13 @@ local charts_available = plugins_utils.timeseriesCreationEnabled()
-- ################################################
local active_monitoring_hosts = active_monitoring_utils.getHosts()
local active_monitoring_hosts = am_utils.getHosts()
local res = {}
for key, active_monitoring_host in pairs(active_monitoring_hosts) do
local chart = ""
local m_info = active_monitoring_utils.getMeasurementInfo(active_monitoring_host.measurement)
local m_info = am_utils.getMeasurementInfo(active_monitoring_host.measurement)
if not m_info then
goto continue
@ -36,7 +36,7 @@ for key, active_monitoring_host in pairs(active_monitoring_hosts) do
local column_last_ip = ""
local column_last_update = ""
local column_last_value = ""
local last_update = active_monitoring_utils.getLastAmUpdate(active_monitoring_host.host, active_monitoring_host.measurement)
local last_update = am_utils.getLastAmUpdate(active_monitoring_host.host, active_monitoring_host.measurement)
local alerted = 0
if(last_update ~= nil) then
@ -57,14 +57,10 @@ for key, active_monitoring_host in pairs(active_monitoring_hosts) do
if(column_last_value == nil) then
chart = ""
else
if(m_info.operator == "gt") then
if(column_last_value > active_monitoring_host.threshold) then
alerted = 1
end
if am_utils.hasExceededThreshold(active_monitoring_host.threshold, m_info.operator, column_last_value) then
alerted = 1
else
if(column_last_value < active_monitoring_host.threshold) then
alerted = 1
end
alerted = 0
end
end