Disclaimer: I am aware of possibility to set multiple DNS servers, but for Pi-Hole to work you need to set only Pi-Hole IP address.
Use case: Set-up Mikrotik and RPI with Pi-Hole. When RPI goes down, internet will "stop" working for everyone on the LAN, and you don't want it to happen. Add script to Mikrotik that detects when Pi-Hole is no longer resolving queries and switch all DNS settings on Mikrotik to public DNS servers, such as 1.1.1.2,1.0.0.2. Also notify yourself that RPI is down. Second RPI costs money, and brings no benefits rather than high-availability, so this is not an option in this case.
Variant 1 - changes only DNS server for router. Assumes that all devices use router as the only DNS server:
:local currentDNS [/ip dns get server]
:local piholeDNS "192.168.0.50"
:local backupDNS "1.1.1.2,1.0.0.2"
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dns set servers=$backupDNS
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dns set servers=$piholeDNS
} on-error={}
}
:local telegramBotKey "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
:local chatID "XXXXXXXXX"
:local currentDNS [/ip dns get server]
:local piholeDNS "192.168.0.50"
:local backupDNS "1.1.1.2,1.0.0.2"
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dns set servers=$backupDNS
/tool fetch "https://api.telegram.org/bot$telegramBotKey/sendmessage?chat_id=$chatID&text=Pi-Hole not working! Changed DNS from $currentDNS to $backupDNS." keep-result=no
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dns set servers=$piholeDNS
/tool fetch "https://api.telegram.org/bot$telegramBotKey/sendmessage?chat_id=$chatID&text=Pi-Hole is working again. Changed DNS from $currentDNS to $piholeDNS." keep-result=no
} on-error={}
}
:local currentDNS [/ip dns get server]
:local piholeDNS "192.168.0.50"
:local backupDNS "1.1.1.2,1.0.0.2"
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dns set servers=$backupDNS
/ip dhcp-server network set [find] dns-server=$backupDNS;
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dns set servers=$piholeDNS
/ip dhcp-server network set [find] dns-server=$piholeDNS;
} on-error={}
}
:local telegramBotKey "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
:local chatID "XXXXXXXXX"
:local currentDNS [/ip dns get server]
:local piholeDNS "192.168.0.50"
:local backupDNS "1.1.1.2,1.0.0.2"
:local testDomain "www.google.com"
:if ($currentDNS = $piholeDNS) do={
:do {
:resolve $testDomain server $piholeDNS
} on-error={
/ip dns set servers=$backupDNS
/ip dhcp-server network set [find] dns-server=$backupDNS;
/tool fetch "https://api.telegram.org/bot$telegramBotKey/sendmessage?chat_id=$chatID&text=Pi-Hole not working! Changed DNS from $currentDNS to $backupDNS." keep-result=no
}
} else={
:do {
:resolve $testDomain server $piholeDNS
/ip dns set servers=$piholeDNS
/ip dhcp-server network set [find] dns-server=$piholeDNS;
/tool fetch "https://api.telegram.org/bot$telegramBotKey/sendmessage?chat_id=$chatID&text=Pi-Hole is working again. Changed DNS from $currentDNS to $piholeDNS." keep-result=no
} on-error={}
}
EDIT: Updated scripts according to suggestions in comments & offered more variants.