2012-09-20 19:29:30 +02:00
|
|
|
-- call_flow_monitor.lua
|
|
|
|
|
-- Part of FusionPBX
|
|
|
|
|
-- Copyright (C) 2010 Mark J Crane <markjcrane@fusionpbx.com>
|
|
|
|
|
-- All rights reserved.
|
|
|
|
|
--
|
|
|
|
|
-- Redistribution and use in source and binary forms, with or without
|
|
|
|
|
-- modification, are permitted provided that the following conditions are met:
|
|
|
|
|
--
|
|
|
|
|
-- 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
-- this list of conditions and the following disclaimer.
|
|
|
|
|
--
|
|
|
|
|
-- 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
-- notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
-- documentation and/or other materials provided with the distribution.
|
|
|
|
|
--
|
|
|
|
|
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
|
-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
|
-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
|
-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
-- POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
--set the time between loops in seconds
|
2015-10-09 03:54:11 +02:00
|
|
|
sleep = 60;
|
2012-09-23 12:31:31 +02:00
|
|
|
|
|
|
|
|
--set the debug level
|
|
|
|
|
debug["log"] = false;
|
|
|
|
|
debug["sql"] = false;
|
2012-09-20 19:29:30 +02:00
|
|
|
|
2013-03-31 10:18:41 +02:00
|
|
|
--include config.lua
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.config";
|
2012-09-20 19:29:30 +02:00
|
|
|
|
2013-04-12 20:46:02 +02:00
|
|
|
--general functions
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.file_exists";
|
|
|
|
|
require "resources.functions.mkdir";
|
2013-04-12 20:46:02 +02:00
|
|
|
|
2015-08-11 04:06:33 +02:00
|
|
|
require "resources.functions.database_handle";
|
2015-10-02 12:02:13 +02:00
|
|
|
|
|
|
|
|
local log = require "resources.functions.log".call_flow_monitor
|
|
|
|
|
|
|
|
|
|
local presence_in = require "resources.functions.presence_in"
|
2012-09-20 19:29:30 +02:00
|
|
|
|
|
|
|
|
--make sure the scripts/run dir exists
|
2013-04-12 20:46:02 +02:00
|
|
|
mkdir(scripts_dir .. "/run");
|
2012-09-20 19:29:30 +02:00
|
|
|
|
|
|
|
|
--define the run file
|
|
|
|
|
run_file = scripts_dir .. "/run/call_flow_monitor.tmp";
|
|
|
|
|
|
|
|
|
|
--define the functions
|
|
|
|
|
--shell return results
|
|
|
|
|
function shell(c)
|
|
|
|
|
local o, h
|
|
|
|
|
h = assert(io.popen(c,"r"))
|
|
|
|
|
o = h:read("*all")
|
|
|
|
|
h:close()
|
|
|
|
|
return o
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--used to stop the lua service
|
|
|
|
|
local file = assert(io.open(run_file, "w"));
|
|
|
|
|
file:write("remove this file to stop the script");
|
2015-10-02 12:02:13 +02:00
|
|
|
file:close()
|
2012-09-20 19:29:30 +02:00
|
|
|
|
2015-10-02 12:02:13 +02:00
|
|
|
log.notice("Start")
|
2012-09-20 19:29:30 +02:00
|
|
|
--monitor the call flows status
|
2015-10-02 12:02:13 +02:00
|
|
|
local sql = "select d.domain_name, f.call_flow_uuid, f.call_flow_extension, f.call_flow_feature_code," ..
|
|
|
|
|
"f.call_flow_status, f.call_flow_label, f.call_flow_anti_label "..
|
|
|
|
|
"from v_call_flows as f, v_domains as d " ..
|
|
|
|
|
"where f.domain_uuid = d.domain_uuid " -- and call_flow_enabled = 'true'
|
2012-09-20 19:29:30 +02:00
|
|
|
while true do
|
2015-10-02 12:02:13 +02:00
|
|
|
-- debug print
|
2012-09-23 12:31:31 +02:00
|
|
|
if (debug["sql"]) then
|
2015-10-02 12:02:13 +02:00
|
|
|
log.notice("SQL:" .. sql);
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--connect to the database
|
|
|
|
|
local dbh = database_handle('system');
|
|
|
|
|
|
|
|
|
|
--get the extension list
|
|
|
|
|
if dbh:connected() then
|
|
|
|
|
dbh:query(sql, function(row)
|
|
|
|
|
local domain_name = row.domain_name;
|
|
|
|
|
local call_flow_uuid = row.call_flow_uuid;
|
|
|
|
|
--local call_flow_name = row.call_flow_name;
|
|
|
|
|
--local call_flow_extension = row.call_flow_extension;
|
|
|
|
|
local call_flow_feature_code = row.call_flow_feature_code;
|
|
|
|
|
--local call_flow_context = row.call_flow_context;
|
|
|
|
|
local call_flow_status = row.call_flow_status;
|
|
|
|
|
--local pin_number = row.call_flow_pin_number;
|
|
|
|
|
local call_flow_label = row.call_flow_label;
|
|
|
|
|
local call_flow_anti_label = row.call_flow_anti_label;
|
|
|
|
|
|
|
|
|
|
-- turn the lamp
|
|
|
|
|
presence_in.turn_lamp( call_flow_status == "false",
|
|
|
|
|
call_flow_feature_code.."@"..domain_name,
|
|
|
|
|
call_flow_uuid
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (debug["log"]) then
|
|
|
|
|
local label = (call_flow_status == "true") and call_flow_label or call_flow_anti_label
|
|
|
|
|
log.noticef("label=%s,status=%s,uuid=%s", label, call_flow_status, call_flow_uuid);
|
|
|
|
|
end
|
|
|
|
|
end);
|
2012-09-23 12:31:31 +02:00
|
|
|
end
|
2015-10-02 12:02:13 +02:00
|
|
|
|
|
|
|
|
-- release dbh
|
|
|
|
|
dbh:release()
|
2012-09-20 19:29:30 +02:00
|
|
|
|
|
|
|
|
--exit the loop when the file does not exist
|
|
|
|
|
if (not file_exists(run_file)) then
|
|
|
|
|
break;
|
|
|
|
|
end
|
2012-09-21 00:52:02 +02:00
|
|
|
|
|
|
|
|
--sleep a moment to prevent using unecessary resources
|
|
|
|
|
freeswitch.msleep(sleep*1000);
|
2015-10-02 12:02:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
log.notice("Stop")
|