Community discussions

MikroTik App
 
User avatar
vinigas
just joined
Topic Author
Posts: 18
Joined: Thu Jun 18, 2020 8:48 pm
Location: Lithuania

(PiHole healthcheck) HTTP Fetch as-value crashes in script when query fails.

Fri Dec 11, 2020 11:05 am

RouterOS 6.48beta58; RB4011iGS+5HacQ2HnD-IN

Then I use fetch as-value I get _exception_ which terminates my whole script. But then I use normal fetch I get normal result with status=failure.

Then I try to print as-value result to log, nothing appears too.
I expected to get failure status as variable which then I could use to handle in script. Instead script dies on fetch command, also this exception is not logged by my default settings.

Example in terminal (which I expect to behave the same, except saving results to variable then using as-value):
scrn.png
I use self-signed certificate in PiHole webserver.

PS. The script I want to make is to http healthcheck if PiHole service is alive, then dynamically set DNS upstream server to it, then it is alive. And if it is not alive, then set DNS upsteam server to 1.1.1.1. Typical expected failures is: link timeout, raspberrypi did not finished starting, and PiHole have not finished starting. Netwatch is not enough to healthcheck pihole.
Yes, I will change API key I posted here, don't get frisky.

Appending script I want to use in scheduler (status=failed part doesn't work, because script dies in first line):
# License: MIT
{
    :local result [/tool fetch url="https://pi.hole/admin/api.php\?status&auth=a138b30f41929c6ec46a6886772cae9f415e60cb6bba58dff079ad459adeefba" as-value output=user];
    :if ($result->"status" = "finished") do={
        :if ($result->"data" = "{\"status\":\"enabled\"}") do={
            /ip dns set servers=192.168.88.20
        } else={
            /ip dns set servers=1.1.1.1,8.8.8.8,1.0.0.1,8.8.4.4
        }
    }
    :if ($result->"status" = "failed") do={
            /ip dns set servers=1.1.1.1,8.8.8.8,1.0.0.1,8.8.4.4
    }
}
You do not have the required permissions to view the files attached to this post.
Last edited by vinigas on Thu Jul 22, 2021 9:32 pm, edited 4 times in total.
 
User avatar
vinigas
just joined
Topic Author
Posts: 18
Joined: Thu Jun 18, 2020 8:48 pm
Location: Lithuania

Re: [Bug] HTTP Fetch as-value crashes in script when query fails.

Sat Dec 12, 2020 1:52 pm

I am also using script I posted above and for now I added additional hacky workaround:
Added netwatch 192.168.88.20 IP with 1s interval with no UP event and DOWN event script is
/ip dns set servers=1.1.1.1,8.8.8.8,1.0.0.1,8.8.4.4
:delay 5
/ip dns set servers=1.1.1.1,8.8.8.8,1.0.0.1,8.8.4.4
I added repeat after delay just in case of some weird concurrency, to be safe. Since HTTP fetch is executed every 1-5s, it is guarranted that in long-term the upstream DNS setting will converge to correct one anyway.

Now it works as expected. But it would be great if I could handle HTTP fetch timeout errors in scripts.
 
sin3vil
newbie
Posts: 34
Joined: Sat May 26, 2018 10:05 pm

Re: [Bug] (PiHole healthcheck) HTTP Fetch as-value crashes in script when query fails.  [SOLVED]

Sun Dec 13, 2020 8:25 pm

You can wrap the tool fetch command in a do { } on-error={ } construct to handle it if it fails :
#initialize result as an array
local result ({})
do {
set result [/tool fetch url="http://something.at/somwhere" output=user as-value]
} on-error={
#this part triggers if previous mikrotik command errors out
#set status key to failed
set ($result->"status") "failed"
}
#continue handling as normal

Who is online

Users browsing this forum: HugoCar and 28 guests