Initial rework for in-memory alerts

This commit is contained in:
emanuele-f 2019-07-16 19:24:44 +02:00
parent 7eae30e914
commit c117f20e19
12 changed files with 492 additions and 133 deletions

View file

@ -424,9 +424,18 @@ function getNumAlerts(what, options)
end
local num = 0
local opts = getUnpagedAlertOptions(options or {})
local res = performAlertsQuery("SELECT COUNT(*) AS count", what, opts)
if((res ~= nil) and (#res == 1) and (res[1].count ~= nil)) then num = tonumber(res[1].count) end
if(what == "engaged") then
local entity_type_filter = tonumber(options.entity)
local entity_value_filter = options.entity_val
local res = interface.getEngagedAlertsCount(entity_type_filter, entity_value_filter)
if(res ~= nil) then num = res.num_alerts end
else
local opts = getUnpagedAlertOptions(options or {})
local res = performAlertsQuery("SELECT COUNT(*) AS count", what, opts)
if((res ~= nil) and (#res == 1) and (res[1].count ~= nil)) then num = tonumber(res[1].count) end
end
return num
end
@ -446,18 +455,19 @@ local function engagedAlertsQuery(params)
--~ tprint(string.format("type=%s sev=%s entity=%s val=%s", type_filter, severity_filter, entity_type_filter, entity_value_filter))
--~ local alerts = interface.getEngagedAlerts(type_filter, severity_filter, entity_type_filter, entity_value_filter)
local alerts = interface.getEngagedAlerts(entity_type_filter, entity_value_filter, type_filter, severity_filter)
return(alerts)
end
-- #################################
function getAlerts(what, options)
--~ if what == "engaged" then
--~ return engagedAlertsQuery(options)
--~ else
if what == "engaged" then
return engagedAlertsQuery(options)
else
return performAlertsQuery("SELECT rowid, *", what, options)
--~ end
end
end
-- #################################
@ -1425,6 +1435,21 @@ end
-- #################################
local function menuEntriesToDbFormat(entries)
local res = {}
for entry_id, entry_val in pairs(entries) do
res[#res + 1] = {
id = tostring(entry_id),
count = tostring(entry_val),
}
end
return(res)
end
-- #################################
function drawAlertTables(num_past_alerts, num_engaged_alerts, num_flow_alerts, get_params, hide_extended_title, alt_nav_tabs, options)
local alert_items = {}
local url_params = {}
@ -1620,10 +1645,12 @@ function getCurrentStatus() {
end
if t["status"] == "engaged" then
-- TODO read from memory
--~ type_menu_entries = {
--~ {id = "2", count = "2"}
--~ }
local res = interface.getEngagedAlertsCount(tonumber(_GET["entity"]), _GET["entity_val"])
if(res ~= nil) then
type_menu_entries = menuEntriesToDbFormat(res.type)
sev_menu_entries = menuEntriesToDbFormat(res.severities)
end
end
print(drawDropdown(t["status"], "type", a_type, alert_types, i18n("alerts_dashboard.alert_type"), get_params, type_menu_entries))

View file

@ -294,17 +294,23 @@ function alerts.new_trigger(entity_info, type_info, when)
when = when or os.time()
local granularity_sec = type_info.alert_granularity and type_info.alert_granularity.granularity_seconds or 0
local granularity_id = type_info.alert_granularity and type_info.alert_granularity.granularity_id or nil
local subtype = type_info.alert_subtype or ""
local alert_json = json.encode(type_info.alert_type_params)
if(granularity_id ~= nil) then
local triggered = true
local alert_key_name = get_alert_triggered_key(type_info)
local params = {alert_key_name, granularity_id,
type_info.alert_type.severity.severity_id, type_info.alert_type.alert_id,
subtype, alert_json
}
if((host.storeTriggeredAlert) and (entity_info.alert_entity.entity_id == alertEntity("host"))) then
triggered = host.storeTriggeredAlert(alert_key_name, granularity_id)
triggered = host.storeTriggeredAlert(table.unpack(params))
elseif((interface.storeTriggeredAlert) and (entity_info.alert_entity.entity_id == alertEntity("interface"))) then
triggered = interface.storeTriggeredAlert(alert_key_name, granularity_id)
triggered = interface.storeTriggeredAlert(table.unpack(params))
elseif((network.storeTriggeredAlert) and (entity_info.alert_entity.entity_id == alertEntity("network"))) then
triggered = network.storeTriggeredAlert(alert_key_name, granularity_id)
triggered = network.storeTriggeredAlert(table.unpack(params))
end
if(not triggered) then
@ -314,7 +320,6 @@ function alerts.new_trigger(entity_info, type_info, when)
end
end
local alert_json = json.encode(type_info.alert_type_params)
local action = ternary((granularity_id ~= nil), "engaged", "stored")
local alert_event = {
@ -325,7 +330,7 @@ function alerts.new_trigger(entity_info, type_info, when)
type = type_info.alert_type.alert_id,
severity = type_info.alert_type.severity.severity_id,
message = alert_json,
subtype = type_info.alert_subtype or "",
subtype = subtype,
tstamp = when,
action = action,
}