Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
--- Service for FusionPBX to monitor on gateway status
-- and send email notification when gateway change its status.
--
-- Require FusionPBX 4.3 or higher
--
-- start: `fs_cli -x 'luarun gw_monitor.lua'`
-- stop: `fs_cli -x 'lua service gw_monitor stop'`
-- pid file: `${script_dir}/run/gw_monitor.pid`
local email = 'mail@address'
local service_name = 'gw_monitor'
----------------------------------------------------
require "resources.functions.config"
local log = require "resources.functions.log"[service_name]
local send_mail = require "resources.functions.send_mail"
local Database = require "resources.functions.database"
local BasicEventService = require "resources.functions.basic_event_service"
local get_gw_info_sql = [[select d.domain_name, d.domain_uuid, g.gateway
from v_gateways as g left outer join v_domains as d on d.domain_uuid = g.domain_uuid
where g.gateway_uuid=:gateway_uuid]]
local function get_gw_info(name)
local dbh = Database.new('system')
if not dbh then return end
local row = dbh:first_row(get_gw_info_sql, {gateway_uuid = name})
dbh:release()
if not row then return end
return row.gateway, row.domain_name, row.domain_uuid
end
local service = BasicEventService.new(log, service_name)
local known = {}
service:bind("CUSTOM::sofia::gateway_state", function(self, name, event)
-- log.notice(event:serialize('plain'))
local gateway = event:getHeader('Gateway')
local state = event:getHeader('State')
local title
if state == 'FAILED' then
if known[gateway] == 'FAILED' then
return
end
title = 'ALERT - Gateway is DOWN: '
elseif state == 'REGED' then
if known[gateway] == 'REGED' then
return
end
title = 'ALERT - Gateway is UP: '
else
-- ignore other states
return
end
known[gateway] = state
local ping = event:getHeader('Ping-Status')
local status = event:getHeader('Status')
local phrase = event:getHeader('Phrase')
local gateway_name, domain_name, domain_uuid = get_gw_info(gateway)
if not gateway_name then return end
local full_name = gateway_name
if #domain_name > 0 then full_name = full_name .. '@' .. domain_name end
local response = status or ''
if status and phrase then -- this is SIP response
response = ' ' .. status .. ' ' .. phrase
end
local msg = string.format("%s state %s%s ping: %s", full_name, state, response, ping or '')
log.notice(msg)
local headers = {}
headers["X-FusionPBX-Email-Type"] = 'gateway-monitor'
if #domain_name > 0 then
headers["X-FusionPBX-Domain-UUID"] = domain_uuid
headers["X-FusionPBX-Domain-Name"] = domain_name
end
send_mail(headers, email, {
title .. ' ' .. full_name,
os.date('%Y-%m-%d %H:%M:%S') .. ' ' .. msg
})
end)
log.notice("start")
service:run()
log.notice("stop")
@kismetgerald

This comment has been minimized.

Copy link

commented Jul 11, 2016

I like this, it works great. I modified line# 78 and 84 to read: 'ALERT - Gateway is DOWN: ' .. full_name; and 'ALERT - Gateway is UP: ' .. full_name; respectively.
Makes the grammar sound better I think.

@kismetgerald

This comment has been minimized.

Copy link

commented Jul 13, 2016

hey bud, so I tried the updated code and it fired an email once. It's now 24 hours and it hasn't generated another one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.