Page 1 of 1

Check if a site is still working

Posted: Monday 16 April 2018 19:32
by dannybloe
Howdee folks,

I have a couple of webservers running and at some point one of them was not working anymore so I thought it was about time to do some regular monitoring. Of course there are plenty solutions out there but I though: let's do it with dzVents. And... turns out it is very simple to do.

So, I started by creating a dummy switch 'mySiteA' that will represent the state of my website A. If the switch is Off then... well.. the site is down. I also create a switch 'testSite' so I can manually trigger a test. Then I created this script:

Code: Select all

return {
	on = {
		timer = { 'at 09:00' },
		devices = { 'testSite' },
		httpResponses = { 'mySite'}
	}
	execute = function(domoticz, item)

		if (item.isTimer or item.isDevice) then 
			-- start the test
			domoticz.openURL({ url = 'http://mysite', callback = 'mySite', method = 'GET' })
		end
		
		if (item.isHTTPResponse) then 		    
			if (item.ok) then
				domoticz.email('Web server status', 'mySite is active', '[email protected]')
				domoticz.devices('mySite').switchOn()
			else
				domoticz.email('Web server status', 'mySite is down', '[email protected]')
				domoticz.devices('mySite').switchOff()
			end
		end
	end
}
Of course this is a simple test and it tests only one site. But you can test many sites like this and you can adjust the timer to check every hour or whatever. Also, in this case I do a simple GET request but you can also do POST requests for instance if you want to test if a contact form actually works. In the example above I test for for the ok response but you can inspect all response headers or the returned data.

Oh, and the switch mySite is just there so you can see in Domoticz the state of your site as well. In case you missed the email (you might create a script to test that as well if this is the case ;-) )

Re: Check if a site is still working

Posted: Tuesday 03 August 2021 8:17
by sciurius
Can you elaborate on this?

I'm still a novice as it comes to Domoticz scripting...

E.g. I would like to perform such a check periodically. How cna I do that?

Re: Check if a site is still working

Posted: Tuesday 03 August 2021 12:49
by erem
@sciurius

The

Code: Select all

timer = { 'at 09:00' },
is responsible for the recurrence.
for all timer options, look here: https://www.domoticz.com/wiki/DzVents:_ ... gger_rules

Re: Check if a site is still working

Posted: Tuesday 03 August 2021 16:31
by sciurius
Great, thanks!

Re: Check if a site is still working

Posted: Monday 16 August 2021 16:07
by Wilco
Hello,

1. i created a dummy switch
2. i created a new lua-script, with only your sample

and got this error

Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_http.lua: /home/pi/domoticz/scripts/lua/script_device_http.lua:7: '}' expected (to close '{' at line 1) near 'execute'

what must i do?

Kind Regards
Wilco

Re: Check if a site is still working

Posted: Monday 16 August 2021 16:22
by erem
you most likely have an unmatched bracket in your code.

Re: Check if a site is still working

Posted: Monday 16 August 2021 16:27
by Wilco
Thanx for helping

i copied the sample code, where is the unmatched bracked? I don't see it.

Re: Check if a site is still working

Posted: Monday 16 August 2021 16:47
by Maikel76
Wilco wrote: Monday 16 August 2021 16:07 Hello,

1. i created a dummy switch
2. i created a new lua-script, with only your sample

and got this error

Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_http.lua: /home/pi/domoticz/scripts/lua/script_device_http.lua:7: '}' expected (to close '{' at line 1) near 'execute'

what must i do?

Kind Regards
Wilco
Wilco, Make a new dzVents script, not a lua script (you can choose that option too in the online editor)
Otherwise, make sure all { opening brackets are also closed with an closing bracket }

Re: Check if a site is still working

Posted: Monday 16 August 2021 16:56
by Wilco
also an error

2021-08-16 16:54:50.071 Error: dzVents: Error: (3.1.7) error loading module 'HTTP' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/HTTP.lua':
2021-08-16 16:54:50.071 ...e/pi/domoticz/scripts/dzVents/generated_scripts/HTTP.lua:7: '}' expected (to close '{' at line 1) near 'execute'

Re: Check if a site is still working

Posted: Monday 16 August 2021 17:13
by waltervl
I think the example script misses a comma

Code: Select all

}
	execute = function(domoticz, item)
should be

Code: Select all

},
	execute = function(domoticz, item)

Re: Check if a site is still working

Posted: Monday 16 August 2021 17:20
by Wilco
Thank you all,

what i want is very simpel:
- 1 dummy-device (type on/off)
- check (every hour) the http(s)-response
yes = dummy-device ON
no = dummy-device OFF

does someone have a lua-script?

Re: Check if a site is still working

Posted: Tuesday 17 August 2021 7:17
by kiddigital
Check the ‘HTTP(S) Poller’ hardware module.

For more info, read the Wiki page about it.

Re: Check if a site is still working

Posted: Tuesday 17 August 2021 13:24
by Wilco
Hello Kiddigital,

i saw that, but how to? can you give me a sample?