From 00676cf152665e91122cad21c864bfc912d93550 Mon Sep 17 00:00:00 2001 From: Mattia Tadini Date: Wed, 8 Jan 2025 16:58:50 +0000 Subject: [PATCH] Add dns-healtcheck --- dns-healtcheck | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 dns-healtcheck diff --git a/dns-healtcheck b/dns-healtcheck new file mode 100644 index 0000000..4d03313 --- /dev/null +++ b/dns-healtcheck @@ -0,0 +1,71 @@ +## +## Automatically DNS Healthcheck +## +## +## script by Mattia Tadini +## created: 2025-01-05 +## updated: 2025-01-05 +## tested on: RouterOS 7.16.2 / multiple HW devices, won't work on 6.49 and older (different update process & value naming) +## + +###### define variables here + +:local sysname [/system identity get name]; +:local Email "[YOUR EMAIL ADDRESS FOR NOTIFICATIONS HERE]"; ###### please configure SMTP in /tools/email to use mail notifications + +:local PrimaryDNS "[YOUR PRIMARY DNS IP HERE]"; +:local BackupDNS "1.1.1.1"; #### CloudFare public DNS as backup (better than Google's) +:local TestDomain "google.com" + +:local ConfiguredDNS [/ip dns get servers]; + +###### when router is in its primary configuration +:if ($PrimaryDNS = $ConfiguredDNS) do={ + :do { + ###### test resolution + :put [:resolve $TestDomain server $ConfiguredDNS]; + + ###### generate syslog messages + /log info "Primary DNS $PrimaryDNS healthcheck completed, no issues"; + + } on-error={ + :put "resolver failed"; + + ###### generate syslog messages + /log info "name resolution using primary DNS $PrimaryDNS failed"; + /log info "temporary setting backup DNS $BackupDNS as primary"; + + ###### update DNS with backup DNS + /ip dns set servers=$BackupDNS; + + ###### send notification email + /tool e-mail send to="$Email" subject="$sysname script notification: Primary DNS $PrimaryDNS down" body="Primary DNS $PrimaryDNS is down.\r\nDNS configuration changed to backup DNS $BackupDNS." + /log info "notification email to $Email sent"; + } +} + +###### when router is in its backup configuration +:if ($BackupDNS = $ConfiguredDNS) do={ + :do { + ###### test resolution + :put [:resolve $TestDomain server $PrimaryDNS]; + + ###### generate syslog messages + /log info "name resolution using primary DNS $PrimaryDNS working now"; + /log info "restoring original DNS configuration"; + + ###### revert back DNS configuration to original + /ip dns set servers=$PrimaryDNS; + + ###### send notification email + /tool e-mail send to="$Email" subject="$sysname script notification: Primary DNS $PrimaryDNS up" body="Primary DNS $PrimaryDNS is up.\r\nOriginal DNS configuration restored." + /log info "notification email to $Email sent"; + + } on-error={ + :put "resolver failed"; + + ###### generate syslog messages + /log info "system is configured with backup DNS $BackupDNS"; + /log info "Primary DNS $PrimaryDNS is still down, next check in 10 seconds"; + } +} \ No newline at end of file