Community discussions

MikroTik App
 
beuker
just joined
Topic Author
Posts: 6
Joined: Tue May 27, 2014 2:23 pm

script to check if dns is running

Tue Aug 18, 2020 3:28 pm

Hi all,

I have created a script to check of the DNS is running of a pi-hole service. I run pi-hole in a container on the internet but if that one is offline I would like to switch back to the ISP dns. What I came up with it this:
:local PIHOLEHOST "pihole.example.com"
:local PIHOLEHOSTIP [:resolve $PIHOLEHOST]

:log info "PI-Hole script started... ($PIHOLEHOSTIP)"

:if ([/ping $PIHOLEHOSTIP interval=1 count=1] = 1) do={
  :log info "PI-Hole host is UP! (ping)"
  :if ([:resolve $PIHOLEHOST server $PIHOLEHOSTIP]) do={
    :log info "PI-Hole DNS is UP! (DNS)"
    #/ip dns set servers="$PIHOLEHOSTIP"
    #/ip dhcp-client set use-peer-dns=no numbers=0
  } else {
    :log error "PI-Hole DNS is DOWN! (DNS)"
    #/ip dns set servers=""
    #/ip dhcp-client set use-peer-dns=yes numbers=0
  }

} else {
  :log error "PI-Hole host DOWN! (ping)"
  #/ip dns set servers=""
  #/ip dhcp-client set use-peer-dns=yes numbers=0
}
The script works perfectly if everything is up and running but if the DNS service is not working it hangs or timeouts... I don't know.
If I run the resolve command from a ssh session I get this message: failure: dns server failure. So it looks like the if statement does not know how to handle it? Or the scripts get terminated?

So what is the best solution to check if the DNS service on the pi-hole container is running?
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1071
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: script to check if dns is running

Tue Aug 18, 2020 5:04 pm

I guess the script is terminated on error... Try to catch it:
:do {
  :resolve ...
} on-error={
  ...
}
 
beuker
just joined
Topic Author
Posts: 6
Joined: Tue May 27, 2014 2:23 pm

Re: script to check if dns is running

Tue Aug 18, 2020 5:35 pm

Fixed it in this way:
:local PIHOLEHOST "pihole.example.com"
:local PIHOLEHOSTIP [:resolve $PIHOLEHOST]
:local PIHOLESTATUS "up"

:log info "PI-Hole script started... ($PIHOLEHOSTIP)"

:if ([/ping $PIHOLEHOSTIP interval=1 count=1] = 1) do={
  :log info "PI-Hole host is UP! (ping)"
  :do { [:resolve $PIHOLEHOST server $PIHOLEHOSTIP]; } on-error={ :set PIHOLESTATUS "down" }
  :if ( $PIHOLESTATUS = "up" ) do={
    :log info "PI-Hole DNS is UP! (DNS)"
    #/ip dns set servers="$PIHOLEHOSTIP"
    #/ip dhcp-client set use-peer-dns=no numbers=0
  } else {  
    :log error "PI-Hole DNS is DOWN! (DNS)"
    #/ip dns set servers=""
    #/ip dhcp-client set use-peer-dns=yes numbers=0
  }

} else {
  :log error "PI-Hole host DOWN! (ping)"
  #/ip dns set servers=""
  #/ip dhcp-client set use-peer-dns=yes numbers=0
}
Is this the most elegant solution?
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1071
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: script to check if dns is running

Tue Aug 18, 2020 7:04 pm

No... You can do something like this:
:local PIHOLEHOST "pihole.example.com"
:local PIHOLEHOSTIP [:resolve $PIHOLEHOST]

:log info "PI-Hole script started... ($PIHOLEHOSTIP)"

:if ([/ping $PIHOLEHOSTIP interval=1 count=1] = 1) do={
  :log info "PI-Hole host is UP! (ping)"
  :do {
    :resolve $PIHOLEHOST server $PIHOLEHOSTIP;
    :log info "PI-Hole DNS is UP! (DNS)"
    #/ip dns set servers="$PIHOLEHOSTIP"
    #/ip dhcp-client set use-peer-dns=no numbers=0
  } on-error={  
    :log error "PI-Hole DNS is DOWN! (DNS)"
    #/ip dns set servers=""
    #/ip dhcp-client set use-peer-dns=yes numbers=0
  }
} else={
  :log error "PI-Hole host DOWN! (ping)"
  #/ip dns set servers=""
  #/ip dhcp-client set use-peer-dns=yes numbers=0
}
But you still have a
:resolve $PIHOLEHOST
in your second line... If that fails the script terminates as well!

Oh, and another one... You should never ever use
numbers=0
! Replace that with
[ find where ... ]
.
 
beuker
just joined
Topic Author
Posts: 6
Joined: Tue May 27, 2014 2:23 pm

Re: script to check if dns is running

Tue Aug 18, 2020 9:53 pm

Yeah! That works like a charm!

That the first resolve fails is not a problem because that means the internet connection is dead also. Ow wait I should point that also to the ISP DNS or the google DNS.
I need it to switch back if that one fails...

Thank you so much for helping.
 
msatter
Forum Guru
Forum Guru
Posts: 2912
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: script to check if dns is running

Tue Aug 18, 2020 10:17 pm

You want to switch back your local resolver is active again.

I check if the domain "pi.hole" resolves and a external DNS server does know that domain. To avoid the stopping of the script I used also the :do but at the beginning.

When the internal DNS does not react or throws a not found then the script switch to external.

I simplified the script to only the essentials:

In the static DNS (/ip dns static) I have added two entries:

primary.dns --> 192.168.88.15
secondary.dns --> 8.8.8.8
{
:local activeDNS [:toip ([/ip dns print as-value ] ->"servers")]
:log info "PI-Hole script started...(active server $activeDNS)"

:do {
:if ([:resolve pi.hole server=primary.dns] do={ 
       :if  ([:resolve primary.dns] != $activeDNS ) do={
       :log error "Primary DNS is UP! again, switching to Pi-hole"
       /ip dns set servers=[:resolve primary.dns]
       #/ip dhcp-client set use-peer-dns=no numbers=0
}} on-error={  
    :if  ([:resolve secondary.dns] != $activeDNS ) do={
    :log error "Internal DNS is DOWN! switching to external DNS"
    /ip dns set servers=[:resolve secondary.dns]
    #/ip dhcp-client set use-peer-dns=yes numbers=0
}}
}
Not tested it but it should work.
 
Kosmatik
just joined
Posts: 1
Joined: Fri Jan 26, 2024 8:53 pm

Re: script to check if dns is running

Fri Jan 26, 2024 9:15 pm

This script checks the functionality of the server Pi.Hole with the address 192.168.188.245 and if it stops giving a response, it changes the main DNS server, and reverse. Clearing the cache is necessary to get the effect from Pi.Hole.
:local MONNAME "pi.hole";
:local URI "https://api.telegram.org/bot<BotAPICode>/sendMessage?chat_id=<CHID>&text=";

:local intDNS "192.168.188.245";
:local extDNS "8.8.8.8";
:local activeDNS ([/ip dns print as-value ] ->"servers");
:do {
    :if ( [:resolve $MONNAME server=$intDNS] != $activeDNS ) do={ 
      :log error "Primary DNS is UP! again, switching to Pi-hole";
      /ip dns set servers=$intDNS;
      /ip dns cache flush
#      /ip dhcp-client set use-peer-dns=no numbers=0
      :local time [/sys clock get time]; 
      /tool fetch url="$URI$MONNAME%20UP%20at%20$time" keep-result=no;
    }
} on-error={
    :if ( $extDNS != $activeDNS ) do={
      :log error "Internal DNS is DOWN! switching to external DNS";
      /ip dns set servers=$extDNS;
      /ip dns cache flush
#      /ip dhcp-client set use-peer-dns=yes numbers=0
      :local time [/sys clock get time]; 
      /tool fetch url="$URI$MONNAME%20DOWN%20at%20$time" keep-result=no;
    }
}

Who is online

Users browsing this forum: No registered users and 4 guests