-
-
Save kismetgerald/df4990b6b327568f815d46f079d4f41d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "resources.functions.config" | |
require "resources.functions.split" | |
local log = require "resources.functions.log".gw_monitor | |
local EventConsumer = require "resources.functions.event_consumer" | |
local send_mail = require "resources.functions.send_mail" | |
local Database = require "resources.functions.database" | |
local known = {} | |
local email = 'mail@address' | |
local sleep = 30000 | |
local pid_file = scripts_dir .. "/run/gw_monitor.tmp" | |
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='%s']] | |
local function get_gw_info(name) | |
local sql = string.format(get_gw_info_sql,name) | |
local dbh = Database.new('system') | |
if not dbh then return end | |
local row = dbh:first_row(sql) | |
dbh:release() | |
if not row then return end | |
return row.gateway, row.domain_name, row.domain_uuid | |
end | |
local events = EventConsumer.new(sleep, pid_file) | |
-- FS shutdown | |
events:bind("SHUTDOWN", function(self, name, event) | |
log.notice("shutdown") | |
return self:stop() | |
end) | |
-- shutdown command | |
events:bind("CUSTOM::fusion::gw::shutdown", function(self, name, event) | |
log.notice("shutdown") | |
return self:stop() | |
end) | |
-- shutdown command | |
events: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 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 = '' | |
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 | |
if state == 'FAILED' then | |
if (not known[gateway]) or (known[gateway] == 'REGED') then | |
send_mail(headers, email, { | |
'ALERT - Gateway is DOWN: ' .. full_name; | |
os.date() .. ' ' .. msg; | |
}) | |
end | |
known[gateway] = state | |
elseif state == 'REGED' then | |
if (not known[gateway]) or (known[gateway] == 'FAILED') then | |
send_mail(headers, email,{ | |
'ALERT - Gateway is UP: ' .. full_name; | |
os.date() .. ' ' .. msg; | |
}) | |
end | |
known[gateway] = state | |
end | |
end) | |
log.notice("start") | |
events:run() | |
log.notice("stop") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment