Restart AdGuard / pi-hole (a DNS container) when DNS stops responding with home assistant
At times for an unknown reason, my AdGuard docker instance running on home assistant operating system stops responding, it ignores all queries sent to it.
I have to manually restart the container, and with home assistant being an automation platform, it made me want to automate this task too.
Whilst I should really be fixing the issue itself (something to do with QUICK) and this being “not really the way to do things” sometimes necessity goes above anything else.
On this page
Setting up a DNS sensor
The first thing we want to do is to setup a DNS sensor, this can be done with the dnsip sensor integration
sensor: - platform: dnsip scan_interval: 60 hostname: one.one.one.one name: one_one_one_one resolver: 192.168.1.4
By default it refreshes the DNS resolve results every 120 seconds, that would be a bit long if DNS goes down so I reduced my sensor to 60 seconds.
You can take a public domain, but I advise to use a custom domain setup in your DNS resolver to always reply the same IP.
Eg:
dns-resolving-is-alive.com -> 0.0.0.0
The reason you want a local resolved address is because:
- If you take a public domain and your internet “dies” it will reboot the instance (unneeded)
- If you take a public domain and your upstream goes down it will reboot the instance (a reboot won’t fix this)
This is only in my case, if you resolve locally (unbound, etc…), you might want to use a public domain like:
- platform: dnsip scan_interval: 60 hostname: dns-resolving-is-alive.com name: dns_resolving_is_alive resolver: 192.168.1.4
Else, set up a custom domain, as such:
The sensor values
We don’t care what the dnsip is having as value, since for example with the one.one.one.one it will loadbalance over its two A records, what we care about is the “unknown” value which would indicate our dns is not resolving.
Example:
dig one.one.one.one ;; ANSWER SECTION: one.one.one.one. 149 IN A 1.1.1.1 one.one.one.one. 149 IN A 1.0.0.1
Meaning every time we ask the round-robin load balancing will give us a different result as seen in the sensor history.
When (in my case) DNS stops responding it becomes an unknown:
Or the more “safer” route and avoid rebooting the containers if internet is down or the upstream is to use the DNS rewritten result:
If you stop the container (to simulate DNS not responding) it will look like this:
Setting up the automation to reboot the DNS if resolving goes down.
We don’t need a cooldown period on the automation, since we go from x.x.x.x to unknown.
Since it will stay “unknown” until the resolver starts working again, thus only triggering at max 1 reboot
YAML export
- id: '1635167344810' alias: If adguard stops responding, reboot it description: '' trigger: - platform: state entity_id: sensor.dns_resolving_is_alive to: unknown condition: [] action: - service: hassio.addon_restart data: addon: a0d7b954_adguard mode: single
Thats it! your DNS container will be rebooted when it stops responding.