User Tools

Site Tools


Action disabled: resendpwd
current_alerts

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
alertIdAlert 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
groupIdThe id key of the group the alert belongs toNumber

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"                -- your telegram chat bot id 
active_alerts = 0                              -- global counter for current alerts
 
function main (userId)
 
  local alerts = GetCurrentAlerts()
 
  if (#alerts > active_alerts) then            -- number has increased, # - means count table elements
      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 
      SendTelegramMessage(TELEGRAM_BOT_ID, 
                          "Current alerts are: " .. table.concat(a_names, ', '))
  end 
 
  active_alerts = #alerts
 
end
current_alerts.txt ยท Last modified: 2022/07/04 12:35 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki