Goal : monitor an external website and alert when DOWN.
In the following example, the alert is triggered when the web server doesn’t respond properly 3 times in a row (w/ a 1 second delay between each try) and if Google responds.
The alert consists of flashing lights 5 times (3 seconds between each state change, ie, for 30 secs) and have dimmers set to 25% during the alert (bedroom, living room).
After 30 seconds, back to the initial state.
A Virtual Switch is used to enable/disable monitoring.
The following LUUP scene is scheduled every minute :
[tt]
local virtual_switch_device = 10
local light_devices = { 1, 2, 3 }
local dimmer_devices = { 4, 5 }
local server_url = “http://” – remote server URL
local string_to_match = “OK” – a string to search within the reply
local reference_url = “http://www.google.com/” – a reference server
local counter = 5
local delay = 3
local max_tries = 3
local original_lights_status = {}
local original_dimmers_status = {}
function setLightState(device, value)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, { newTargetValue = value }, device)
end
function setDimmerState(device, value)
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, { newLoadlevelTarget = value }, device)
end
function switchOn()
for i, device in ipairs(light_devices) do
setLightState(device, 1)
end
luup.call_delay(‘switchOff’, delay)
end
function switchOff()
counter = counter-1
if counter > 0 then
for i, device in ipairs(light_devices) do
setLightState(device, "0")
end
luup.call_delay('switchOn', delay)
else
resetAlert()
end
end
function resetAlert()
for i, device in ipairs(light_devices) do
setLightState(device, original_lights_status[i])
end
for i, device in ipairs(dimmer_devices) do
setDimmerState(device, original_dimmers_status[i])
end
end
function runAlert()
for i, device in ipairs(light_devices) do
original_lights_status[i] = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”, “Status”, device)
end
for i, device in ipairs(dimmer_devices) do
original_dimmers_status[i] = luup.variable_get(“urn:upnp-org:serviceId:Dimming1”, “LoadLevelStatus”, device)
setDimmerState(device, “25”)
end
switchOff()
end
function checkWebServer(try)
try = tonumber(try)
local status, result = luup.inet.wget(server_url, 5)
if (result == nil) then
result = ‘’
end
if (status ~= 0) or (string_to_match and (result:find(string_to_match) == nil)) then
luup.log(“Web server FAILED at try #” … try … " with status " … status … " and content " … result)
try = try + 1
if try <= max_tries then
luup.call_delay(“checkWebServer”, 1, “” … try)
return false
else
luup.log(‘Web server FAILED, looking at reference’)
status, result = luup.inet.wget(reference_url, 5)
if status == 0 then
luup.log(‘Web server FAILED, reference is OK: alert!’)
runAlert()
else
luup.log(‘Web server FAILED, reference is FAILED too: Internet access down?’)
end
return false
end
else
luup.log(‘Web server OK at try #’ … try)
return true
end
end
if (luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”, “Status”, virtual_switch_device) == “1”) then
luup.log(‘Checking Web server’)
checkWebServer(1)
else
luup.log(‘Web server monitoring disabled’)
end
return true
[/tt]
Comments welcome!