carstein.seeberg

NAS script to generate a report of alarms that occured last night

Discussion created by carstein.seeberg on Sep 14, 2009
Latest reply on Jun 23, 2016 by RHENRIQUE
I've had this question pop up now and then, so I thought I should share it with the rest of you.
The attached script could be scheduled to run in the morning, giving you an email of the nightly alarm events.  Note that the check_alarm function can also be used to list alarms in the provided time-window related to the last night or whenever.

Enjoy,
Carstein

----------------------------------------------------------------------------------------------
-- Script to dump alarms that happened within a time-period e.g during last night/evening
--
recipient    = "user@company.com"
timezone_ofs = 0
-- Uncomment for manual timezone ofset override.
-- timezone_ofs = -7200      -- GMT+2

-- This function checks if the alarm has arrived within a predefined period
--    E.g check_alarm (a,18,8)           -- will return true if alarm arrived last night
--        check_alarm (1,21,06,false)    -- will return true if alarm arrived in timewindow regardless.
--
function check_alarm(a,t1,t2,last_night)
   local d,midnight,pstart,pend

   if t1==nil then t1=0 end
   if t2==nil then t2=6 end
   if last_night==nil then last_night=true end

   if last_night then
      d = math.floor (timestamp.now() / 86400)
   else
      d = math.floor (a.arrival / 86400)  
   end

   midnight = d*86400 + timezone_ofs     -- compute 'last' midnight for alarm (adjusted with timezone)

   if t1 > t2 then
      pstart   = midnight + (t1 * 3600)
      pend     = midnight + 3600*24 + (t2 * 3600)  -- adjust till "todays" midnight
   else
      pstart   = midnight + (t1 * 3600)
      pend     = midnight + (t2 * 3600)
   end

   if a.arrival >= pstart and a.arrival <= pend then
      return true
   end

   return false
end

-- Get active alarms
al  = alarm.list()
buf = ""

for i,a in pairs(al) do

   -- auto timezone: use the first alarm with tz_offset as the timezone_ofs
   if timezone_ofs == 0 and a.tz_offset ~= nil then
      timezone_ofs = a.tz_offset
   end

   if a.level > NIML_CLEAR then
      -- check alarms between 22 and 06, last night
      if check_alarm(a,22,06) then
         buf = buf .. sprintf ("\n%s - %s %s %s %s",timestamp.format(a.arrival),a.nimid, a.hostname, a.severity, a.message)
      end
   end

end
if buf ~= "" then
   action.email (recipient,"Nightly Alarm Report",buf)
end

Outcomes