User Tools

Site Tools


current_alerts

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
current_alerts [2019/01/09 10:38] – created akuzmukcurrent_alerts [2022/07/04 12:35] (current) – [GetCurrentAlerts()] emozolyak
Line 9: Line 9:
  
 ^Key ^Description                                                       ^Data type^                                           ^Key ^Description                                                       ^Data type^                                          
-|startTime |Alert trigger time | Number, UnixTime| +|alertId|Alert id key in the project |Number | 
-|regId |The number of the register in which the flag of the alert led to this alert   |Number   | +|title |Alert name |String | 
-|regAlias |Variable name for programs |String| +|startTime |Alert trigger time |Number, UnixTime 
-|bit |Number of the bit with the alarm flag |Number| +|alertLevel |The flag indicates the alert can be confirmed. |[1,2,3] Info, Warn, Alert 
-|title |Alert name |String| +|acknowledgedBy| User name, who confirmed the alert. If the alert is not confirmed - empty string.|String | 
-|connectionTitle |Connection name |String| +|acknowledgedTime |Time, when the alert was confiremed. If the alert is not confirmed - 0.|Number, UnixTime | 
-|connectionId |Connection ID |Number| +|canBeAcknowledged |The flag indicates the alert can be confirmed. |Boolean 
-|connectionAlias |The name of the connection variable for programs|String+|groupId|The id key of the group the alert belongs to|Number |
-|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|+
  
 Using this structure, it is possible to implement, for example, various notification scenarios about unconfirmed accidents. Using this structure, it is possible to implement, for example, various notification scenarios about unconfirmed accidents.
Line 25: Line 22:
 An example of such program: An example of such program:
 <code lua> <code lua>
-local notificationsSent = {}; 
 function main (userId) function main (userId)
-    local alerts = GetCurrentAlerts(); +   
-    if (#alerts 0) then +  local ACK_DELAY_TOLERANCE = 30  
-        for num,alert in pairs(alerts) do  +  local now = os.time() 
-            local now os.time()+   
-            local canBeAcknowledged = alert['canBeAcknowledged']; +  local alerts = GetCurrentAlerts() 
-            local acknowledgedTime alert['acknowledgedTime']; +   
-            local startTime = alert['startTime']; +  for a_num, a in ipairs(alerts) do  
-            local waitSecondsBeforeEscalate = 300; -- notify about alerts that are not confirmed for more than 5 minutes +      INFO("Processing alert #" .. a_num) 
-            local regId alert['regId']; +    --   tprint(a) 
-            local bit = alert['bit']; +      INFO(tostring(a.canBeAcknowledged) .. ' ' .. a.startTime) 
-            if (canBeAcknowledged and acknowledgedTime == 0) then +      local time_diff = (now - a.startTime) 
-                if (now - startTime > waitSecondsBeforeEscalatethen +        if (a.canBeAcknowledged and (time_diff ACK_DELAY_TOLERANCE)) then  
-                    if (notificationsSent[regId] == nil or notificationsSent[regId][bit] ~= startTime) then +            ERROR("The alert " .. a.title .. " exceeded ack time!") 
-                        --ERROR("Alert" .. alert['title'.. " was not acknowledged!"); +        end  
-                        SendSMS("380501234567", "The alert is not confirmed: " .. alert['title'])+   end  
-                        if (notificationsSent[regId] == nilthen + 
-                            notificationsSent[regId] = {}; +end 
-                        end + 
-                        notificationsSent[regId][bit] startTime; +function tprint(t, indent) 
-                    end +    if not indent then indent = 0 end  
-                end +    for kin pairs(t) do  
-            end +        local formatting string.rep(' ', indent.. k .. '
-        end +        if type(v) == "table" then  
-    end+            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  
 +</code> 
 + 
 +=== Example of sending Telegram messages upon new alerts === 
 + 
 +Below is the example of how to send Telegram messages when the alert count has increased: 
 +<code lua> 
 +--[
 +    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 end
 </code> </code>
  
  
current_alerts.1547030292.txt.gz · Last modified: 2019/01/09 10:38 by akuzmuk

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki