After an afternoon of fiddling around I had it done. And in the context of 'show your projects', I want to share the result.
I use an alert device for this, while a notification is also sent by email if one of the websites is offline. I have the script running every 20 minutes.
The device is only updated when content has changed. This already makes a difference in preventing unnecessary registration. For myself, I have also included the device in a script that clears the logs from time to time, because I am not interested in longer history and also not to take up unnecessary space.
Multiple websites can be entered with URL and Name.
A maximum number of (offline sites) to be shown in the alert device is adjustable (because of the limited space). Therefore, put the most important websites first.
Site after site is called and the response processed via callback. By using the asynchronous shell command execution, you are not affected by the 10 second limitation. A fantastic invention!
To maintain an overview of the whole, a number of things are kept in variables in global_data. For explanation see DzVents wiki
The example result: and The variables that are set in global_data under data:
Code: Select all
--Used in t-CheckWebsites
cw_offlineWebsites = { initial = '' },
cw_alertText = { initial = '' },
cw_rowCount = { initial = 0 },
cw_offlineCount = { initial = 0 },
Code: Select all
-- 18-06-2014 Script by Jan Peppink; hppts://ict.peppink.nl
-- Script runs every xx minutes to check if websites are online.
-- HTTP response status code 200 expected.
-- Gives notification when one or more websites is offline.
-- Set custom Alert device.
--- Your settings ----------------------------------------------------------------
-- First set the used device index numbers and variables you might want to change.
local cw_alertIdx = 99999 -- Set to the idx of the Virtual Alert sensor you have to create for this script
local cw_timerInterval = 20 --Set every xx minutes.
local cw_maxalertLines = 4 --Max offline websites to show in devicetext.
-- Set websites to check. When more than 'maxalertLines', place most important first.
local websites = {
{url = "https://website1.com", name = "Website 1"},
{url = "https://website2.com", name = "Website 2"},
{url = "https://website3.com", name = "Website 3"},
--Add more of these lines as needed.
}
-- For notification
local emailTo = "[email protected]"
local notificationSubject = "Website Monitoring"
local notificationMessage = "Website %s is offline!"
----------------------------------------------------------------------------------
return {
on = {
timer = {
'every ' .. cw_timerInterval .. ' minutes',
},
shellCommandResponses = {
-- matches callback strings below
'webcheck',
},
},
logging = {
-- Level can be domoticz.LOG_INFO, domoicz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG, domoticz.LOG_ERROR or domoticz.LOG_FORCE
level = domoticz.LOG_INFO,
--level = domoticz.LOG_DEBUG,
marker = " Websitechecks-"
},
execute = function( dz, triggeredItem )
-- Set Local environment=================
--local _u = dz.utils -- Holds subset of handy utilities.
--local _h = dz.helpers -- Holds the global functions.
local _d = dz.globalData -- Holds the global data.
-- Set your alert device index to store the result.
local cw_alertLevel = dz.ALERTLEVEL_GREY -- Holds the alertLevel (color) to set
local cw_alertText = ''
local cw_lastalertText = dz.devices(cw_alertIdx).text -- Holds string of the previous round.
local tableMaxRow = #websites --Number of (loops) sites to check.
-- Local Functions go here =============
local function isWebsiteOnline( url2Check )
dz.executeShellCommand({
command = 'curl -Is -ipv4 ' .. url2Check .. ' | head -n 1',
callback = 'webcheck'
})
end
-- Now start to do something ============
if triggeredItem.isTimer then
--Start cleaning up variables and set counter for brand new start.
_d.cw_offlineWebsites = ''
_d.cw_alertText = ''
_d.cw_rowCount = 1
_d.cw_offlineCount = 0
dz.log( 'Checking ' .. websites[_d.cw_rowCount].name, dz.LOG_DEBUG )
isWebsiteOnline( websites[_d.cw_rowCount].url )
end
if triggeredItem.isShellCommandResponse then
--We catched the reponse.
if triggeredItem.statusCode == 0 then
--Command ended with success.
if triggeredItem.trigger == 'webcheck' then
-- Trigger = webcheck.
if triggeredItem.data:match( "200" ) then
-- HTTP code = 200. Site is online.
dz.log( websites[_d.cw_rowCount].name .. ' is online.', dz.LOG_DEBUG )
else
-- Site is offline, or unexpected HTTP code. Needs check.
_d.cw_offlineCount = _d.cw_offlineCount + 1
-- Add site to message to send notification.
_d.cw_offlineWebsites = _d.cw_offlineWebsites .. string.format(notificationMessage, websites[_d.cw_rowCount].name ) .. '<br>'
if _d.cw_offlineCount <= cw_maxalertLines then
--Also add for the device.
_d.cw_alertText = _d.cw_alertText .. string.format( notificationMessage, websites[_d.cw_rowCount].name ) .. '<br>'
end
dz.log( websites[_d.cw_rowCount].name .. ' is offline!', dz.LOG_DEBUG )
end
if _d.cw_rowCount < tableMaxRow then
-- Go for the next website
_d.cw_rowCount = _d.cw_rowCount + 1
dz.log( 'Checking ' .. websites[_d.cw_rowCount].name, dz.LOG_DEBUG )
isWebsiteOnline( websites[_d.cw_rowCount].url )
else
--We are done looping the websites. Now check results.
if _d.cw_offlineWebsites ~= '' then
cw_alertLevel = dz.ALERTLEVEL_RED
cw_alertText = _d.cw_alertText
dz.email( notificationSubject, _d.cw_offlineWebsites, emailTo, 0 )
else
cw_alertText = 'Alle website zijn online.'
cw_alertLevel = dz.ALERTLEVEL_GREEN
end
if cw_alertText ~= cw_lastalertText then
-- We have new information, so update the device.
dz.devices(cw_alertIdx).updateAlertSensor( cw_alertLevel, cw_alertText )
end
end
end
end
end
end
}
-- That's All --------------------------------------------------