Page 1 of 1

DNS server, servers update

Posted: Wed Oct 06, 2010 5:18 pm
by mindlesstux
I am not to good on intros to the subject so just bare with me. The setup we have 3 AD DCs that run DNS servers. I have the router referencing the AD DNS servers to help keep the world somewhat sane and all devices using the router for DNS. On occasion one or two of the AD DCs need to reboot (server hard lock, bad need of a rebuild but thats another forum), thus causing the router to have a slower response time on DNS and users were complaining. So I finally sat down and figured out how to resolve this. Figured I could use netwatch on the 3 servers to see if they are up or down then have that in turn run a script on status change. If all 3 were down to have some public dns servers set that the AD DNS servers reference anyway.

I figure someone would be in need of something similar so might as well post it for someone. Change the IPs and netwatch numbers to match your environments obviously.

Potential pitfalls is the use of numbers getting netwatch info.
# Set our netwatches on our DNS servers
/tool netwatch
add disabled=no down-script="/system script run reset-dns" host=10.0.0.1 interval=2s timeout=1s up-script="/system script run reset-dns"
add disabled=no down-script="/system script run reset-dns" host=10.0.0.2 interval=2s timeout=1s up-script="/system script run reset-dns"
add disabled=no down-script="/system script run reset-dns" host=10.0.0.3 interval=2s timeout=1s up-script="/system script run reset-dns"

# Set our script to check the netwatch status's and set the dns servers to what is up
/system script
add name=reset-dns policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive source=":local dnsservers;\r\
    \n\r\
    \nif ([/tool netwatch get value-name=status number=0] = \"up\") do={\r\
    \n  :if ([:len \$dnsservers] = 0) do={\r\
    \n    :set dnsservers \"10.0.0.1\";\r\
    \n  } else={\r\
    \n    :set dnsservers \"\$dnsservers,10.0.0.1\";\r\
    \n  }\r\
    \n}\r\
    \n\r\
    \nif ([/tool netwatch get value-name=status number=1] = \"up\") do={\r\
    \n  :if ([:len \$dnsservers] = 0) do={\r\
    \n    :set dnsservers \"10.0.0.2\";\r\
    \n  } else={\r\
    \n    :set dnsservers \"\$dnsservers,10.0.0.2\";\r\
    \n  }\r\
    \n}\r\
    \nif ([/tool netwatch get value-name=status number=2] = \"up\") do={\r\
    \n  :if ([:len \$dnsservers] = 0) do={\r\
    \n    :set dnsservers \"10.0.0.3\";\r\
    \n  } else={\r\
    \n    :set dnsservers \"\$dnsservers,10.0.0.3\";\r\
    \n  }\r\
    \n}\r\
    \n\r\
    \n:if ([:len \$dnsservers] = 0) do={\r\
    \n  :set dnsservers \"8.8.8.8,8.8.4.4\";\r\
    \n}\r\
    \n/ip dns set servers=\"\$dnsservers\";\r\
    \n:log info (\"changed dns servers to: \$dnsservers\");"
A cleaner review of the script
       :local dnsservers;
       
       if ([/tool netwatch get value-name=status number=0] = "up") do={
         :if ([:len $dnsservers] = 0) do={
           :set dnsservers "10.0.0.1";
         } else={
           :set dnsservers "$dnsservers,10.0.0.1";
         }
       }
       
       if ([/tool netwatch get value-name=status number=1] = "up") do={
         :if ([:len $dnsservers] = 0) do={
           :set dnsservers "10.0.0.2";
         } else={
           :set dnsservers "$dnsservers,10.0.0.2";
         }
       }
       if ([/tool netwatch get value-name=status number=2] = "up") do={
         :if ([:len $dnsservers] = 0) do={
           :set dnsservers "10.0.0.3";
         } else={
           :set dnsservers "$dnsservers,10.0.0.3";
         }
       }
       
       :if ([:len $dnsservers] = 0) do={
         :set dnsservers "8.8.8.8,8.8.4.4";
       }
       /ip dns set servers="$dnsservers";
       :log info ("changed dns servers to: $dnsservers");

Re: DNS server, servers update

Posted: Thu Oct 07, 2010 1:38 pm
by janisk
looks useful.