current_alerts
This is an old revision of the document!
Functions to work with alerts
GetCurrentAlerts()
The GetCurrentAlerts function returns a list of current alerts. Type of returned value is table. Its key is the alarm number, the value is the another table with the alert properties.
Alert properties are:
Key | Description | Data type |
---|---|---|
alertId | Alert id key in the project | Number |
title | Alert name | String |
startTime | Alert trigger time | Number, UnixTime |
alertLevel | The flag indicates the alert can be confirmed. | [1,2,3] Info, Warn, Alert |
acknowledgedBy | User name, who confirmed the alert. If the alert is not confirmed - empty string. | String |
acknowledgedTime | Time, when the alert was confiremed. If the alert is not confirmed - 0. | Number, UnixTime |
canBeAcknowledged | The flag indicates the alert can be confirmed. | Boolean |
groupId | The id key of the group the alert belongs to | Number |
Using this structure, it is possible to implement, for example, various notification scenarios about unconfirmed accidents.
An example of such program:
function main (userId) local ACK_DELAY_TOLERANCE = 30 local now = os.time() local alerts = GetCurrentAlerts() for a_num, a in ipairs(alerts) do INFO("Processing alert #" .. a_num) -- tprint(a) INFO(tostring(a.canBeAcknowledged) .. ' ' .. a.startTime) local time_diff = (now - a.startTime) if (a.canBeAcknowledged and (time_diff > ACK_DELAY_TOLERANCE)) then ERROR("The alert " .. a.title .. " exceeded ack time!") end end end function tprint(t, indent) if not indent then indent = 0 end for k, v in pairs(t) do local formatting = string.rep(' ', indent) .. k .. ': ' if type(v) == "table" then ERROR(formatting) tprint(v, indent + 1) -- recursive call else if type(v) == "boolean" then v = v and "TRUE" or "FALSE" end ERROR(formatting .. v) end end -- for end -- tprint
Example of sending Telegram messages upon new alerts
Below is the example of how to send Telegram messages when the alert count has increased:
--[[ Checks if number of alerts has increased and then sends message via Telegram --]] TELEGRAM_BOT_ID = "229345xxxxx" active_alerts = 0 function main (userId) local alerts = GetCurrentAlerts() if (#alerts > active_alerts) then -- number has increased local a_names = {} DEBUG("Alert count has increased!") for a_num, a in ipairs(alerts) do INFO("Processing alert #" .. a_num) table.insert(a_names, a.title) end a_names = table.concat(a_names, ', ') SendTelegramMessage(TELEGRAM_BOT_ID, "Current alerts are: " .. a_names) end active_alerts = #alerts end
current_alerts.1641291920.txt.gz ยท Last modified: 2022/01/04 10:25 by emozolyak