Check if a site is still working

Moderator: leecollings

Post Reply
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Check if a site is still working

Post 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 ;-) )
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
sciurius
Posts: 55
Joined: Monday 26 July 2021 16:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: NL
Contact:

Re: Check if a site is still working

Post 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?
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: Check if a site is still working

Post 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
Regards,

Rob
sciurius
Posts: 55
Joined: Monday 26 July 2021 16:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: NL
Contact:

Re: Check if a site is still working

Post by sciurius »

Great, thanks!
Wilco
Posts: 18
Joined: Monday 20 March 2017 15:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Zwolle, Netherlands
Contact:

Re: Check if a site is still working

Post 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
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: Check if a site is still working

Post by erem »

you most likely have an unmatched bracket in your code.
Regards,

Rob
Wilco
Posts: 18
Joined: Monday 20 March 2017 15:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Zwolle, Netherlands
Contact:

Re: Check if a site is still working

Post by Wilco »

Thanx for helping

i copied the sample code, where is the unmatched bracked? I don't see it.
Maikel76
Posts: 71
Joined: Friday 14 August 2020 6:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Bangkok
Contact:

Re: Check if a site is still working

Post 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 }
Scripting is my passion, dzVents, LUA, Bash
Dashticz v3.10.7 Beta --Raspbian Buster
Wilco
Posts: 18
Joined: Monday 20 March 2017 15:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Zwolle, Netherlands
Contact:

Re: Check if a site is still working

Post 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'
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Check if a site is still working

Post 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)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Wilco
Posts: 18
Joined: Monday 20 March 2017 15:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Zwolle, Netherlands
Contact:

Re: Check if a site is still working

Post 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?
User avatar
kiddigital
Posts: 435
Joined: Thursday 10 August 2017 6:52
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Check if a site is still working

Post by kiddigital »

Check the ‘HTTP(S) Poller’ hardware module.

For more info, read the Wiki page about it.
One RPi with Domoticz, RFX433e, aeon labs z-wave plus stick GEN5, ha-bridge 5.4.0 for Alexa, Philips Hue Bridge, Pimoroni Automation Hat
One RPi with Pi foundation standard touch screen to display Dashticz
Wilco
Posts: 18
Joined: Monday 20 March 2017 15:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Zwolle, Netherlands
Contact:

Re: Check if a site is still working

Post by Wilco »

Hello Kiddigital,

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

Who is online

Users browsing this forum: No registered users and 1 guest