Don't know but seems anther missing link in the docker install. Please make sure you have Email and at least 1 other notification subsystem working.
Below script rely on these options.
Can you try this one
Code: Select all
local scriptVersion = '0.20210506_01'
local scriptVar = 'SSH_' .. scriptVersion
--[[
This dzVents script is used to monitor open VPN state of a ASUS RT-AC86U router loaded with asuswrt-merlin firmware
The script use async executeShellCommand to trigger a nvram and when required do a service restart command on a remote system via ssh
(the router must be accessible by the user that is running the domoticz service, via password-less SSH (with public / private key setup)
Before activating the script:
Read the GETTING STARTED section of the dzVents wiki.
Change the values in the script to reflect your setup
requires:
access to ssh and ping (extra actions needed to install these when in docker)
passwordless access using ssh setup for the user executing domoticz
domoticz >= V2020.2 12771 (dzVents >= 3.1.0)
configured and working notification subsystem(s) and Email in domoticz
]]--
return
{
on =
{
timer =
{
'every minute', -- change to required frequency
},
shellCommandResponses =
{
scriptVar,
},
},
logging =
{
level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
marker = scriptVar,
},
execute = function(dz, item)
local remoteHost = '192.168.1.1' -- change to router IP
local notConnectedMessage = 'OpenVPN not connected. Check it out'
local reconnectedMessage = 'OpenVPN (re)connected'
local wanDownMessage = 'Wan Connection down? Check it out'
local remoteUser = 'Admin' -- domoticz uses user 'root' for executing commands, so the user 'root' should be password-less SSH access
local remotePort = 2211
local VPNStatus = dz.devices(1155)
local notifyFrequency = 60 -- frequency in minutes
-- =======================================================================
-- NO changes required below this line
-- =======================================================================
--commands to execute remote
local commands =
{
status = 'nvram get vpn_client1_state ',
stop = 'service stop_vpnclient1 ',
start = 'service start_vpnclient1 ',
ping = 'ping -c1 -w1 8.8.8.8 ', -- ping command to check WAN connection available/down
}
commands.restart = commands.stop .. '; ' .. commands.start
local function managedPopen(cmd)
dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)
local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
if commandOutput:find '::ERROR::' then -- something went wrong
dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_DEBUG)
else -- all is fine!!
dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:' .. commandOutput, dz.LOG_DEBUG)
end
return commandOutput,returnTable[3] -- rc[3] contains returnCode
end
local function osCommand(cmd, callback, timeout)
dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)
local timeout = timeout or 50
dz.executeShellCommand(
{
command = cmd,
callback = callback,
timeout = timeout,
})
end
local function buildSSHCommand(cmd)
local sshBOL = "ssh " .. remoteUser .. "@" .. remoteHost .. " -p " .. remotePort .. " \'"
local sshEOL = ";\'"
local cmd = sshBOL .. cmd .. sshEOL
return cmd
end
-- Main
if item.isTimer then
osCommand(buildSSHCommand(commands.status), scriptVar)
else -- callback of the executeShellCommand
--- dz.utils.dumpTable(item) -- Debug only
if ( statusCode ~= 0 ) or ( item.data ~= 2 ) then
if VPNStatus.state == 'On' or VPNStatus.lastUpdate.minutesAgo > notifyFrequency then
dz.log('Result: ' .. item.data .. ': ' .. notConnectedMessage, dz.LOG_ERROR)
dz.notify(scriptVar, notConnectedMessage, dz.PRIORITY_HIGH)
VPNStatus.switchOff().checkFirst()
end
osCommand(buildSSHCommand(commands.restart))
else -- seems OK
local result, rc = managedPopen(buildSSHCommand(commands.ping))
if rc ~= 0 then
dz.log('VPN status is active but ping failed, internet WAN connection down?', dz.LOG_ERROR)
dz.notify(scriptVar, wanDownMessage, dz.PRIORITY_HIGH)
elseif VPNStatus.state == 'Off' then
dz.log(reconnectedMessage, dz.LOG_DEBUG)
dz.notify(scriptVar, reconnectedMessage, dz.PRIORITY_LOW)
VPNStatus.switchOn().checkFirst()
end
end
end
end
}