Created
August 1, 2016 00:08
-
-
Save PBXForums/ac0b9d44818159f89530aee1d282803a to your computer and use it in GitHub Desktop.
memcache.lua setup for replicated fusionpbx
This file contains 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
--description | |
--monitor custom memcache event and clear memcache on remote servers | |
--protect xmlrpc using a firewall on the server to limit access by ip address | |
--dependencies | |
--install mod_curl freeswitch module | |
--uncomment mod_curl from modules.conf when compiling freeswitch | |
--xmlrpc | |
--open port xmlrpc port for other master server IP addresses | |
--change the password for xmlrpc in system -> settings | |
--conf/autoload_configs/lua.conf.xml | |
-- <param name="startup-script" value="memcache.lua"/> | |
--iptables | |
-- /sbin/iptables -I INPUT -j ACCEPT -p tcp --dport 8080 -s x.x.x.x/32 | |
-- ubuntu: service iptables-persistent save | |
--define the servers running freeswitch do not include local | |
servers = {} | |
x = 0; | |
servers[x] = {} | |
servers[x]['username'] = "yourusername"; | |
servers[x]['password'] = "yourpass"; | |
servers[x]['hostname'] = "secondserverip"; | |
servers[x]['port'] = "8787"; | |
--subscribe to the events | |
--events = freeswitch.EventConsumer("all"); | |
events = freeswitch.EventConsumer("api"); | |
--define trim | |
function trim (s) | |
return (string.gsub(s, "^%s*(.-)%s*$", "%1")) | |
end | |
--prepare the api object | |
api = freeswitch.API(); | |
--get the events | |
for event in (function() return events:pop(1) end) do | |
--serialize the data for the console | |
--freeswitch.consoleLog("notice","event:" .. event:serialize("xml") .. "\n"); | |
--freeswitch.consoleLog("notice","event:" .. event:serialize("json") .. "\n"); | |
--get the uuid | |
local api_command = event:getHeader("API-Command"); | |
if (api_command ~= nil) then | |
api_command = trim(api_command); | |
--freeswitch.consoleLog("NOTICE","api_command: "..api_command .. "\n"); | |
end | |
if (api_command == "memcache") then | |
local api_command_http_host = event:getHeader("HTTP-HOST"); | |
if (api_command_http_host == nil) then | |
memcache_updated = false; | |
local api_command_argument = event:getHeader("API-Command-Argument"); | |
if (api_command_argument ~= nil) then | |
api_command_argument = trim(api_command_argument); | |
api_command_argument = api_command_argument:gsub(" ", "%%20"); | |
end | |
if (api_command_argument ~= nil) then | |
if (api_command_argument == "flush") then | |
memcache_updated = true | |
end | |
if (string.sub(api_command_argument, 0, 6) == "delete") then | |
memcache_updated = true | |
end | |
if (memcache_updated) then | |
for key,row in pairs(servers) do | |
--cmd = [[ssh ]]..row.username..[[@]]..row.hostname..[[ "fs_cli -x 'memcache ]]..api_command_argument..[['"]]; | |
--freeswitch.consoleLog("INFO", "[notice] command: ".. cmd .. "\n"); | |
--os.execute(cmd); | |
url = [[http://]]..row.username..[[:]]..row.password..[[@]]..row.hostname..[[:]]..row.port..[[/webapi/memcache?]]..api_command_argument; | |
response = api:execute("curl", url); | |
freeswitch.consoleLog("INFO", "[notice] ".. url .. " "..response.."\n"); | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment