I recently had a bit of trouble with really slow page loads in my browser which I couldn’t correlate with network packet loss or slow pings. I think it was related slow DNS lookup times (through NextDNS) although I’m not 100% sure because by the time I figured out how to diagnose DNS, the problem had gone away.
Anyway I came up with this command line monitor for DNS lookup times and figured it might be useful for others so sharing here and also looking for feedback on the approach I’ve used.
The following command monitors the DNS query time. If the DNS query fails it results in a value of -10. This is partly to drive the uptime monitoring (see next template configuration), but also makes it easy to spot outages when charting query times. The default scan interval for a command line sensor is 60 seconds, so the domain name used needs to use a TTL less than 60 seconds, otherwise the DNS lookup will be cached by the DNS resolver. I’ve used force.com which has a 30s TTL.
- platform: command_line
name: DNS resolution time
command: "dig +retries=1 force.com | grep -A 100 'ANSWER SECTION:' | grep -Po '(?<=Query time: )[0-9a]*' || echo '-10'"
unit_of_measurement: "mS"
And then this uses the sensor result to track DNS uptime (i.e. is DNS resolving)
template:
- binary_sensor:
- name: DNS Resolution
state: >
{{ states('sensor.dns_resolution_time') != "-10" }}