Netwatch, how to run script after XX attempts

Hi, I’va a remote AP that today needed a reset, it may be a power problem, I?m nonitoring it. Anyway to don’t loose it I would like to do a reset if it can’t ping the main router, and I don’t want it does it at the first failure, I would for example reboot after 6 consecutive fails, which it becomes once an hour if I test every 10 minutes.
On netwatch parameters I can set interval and timeout, for example 2 seconds, which is useful to increment a failing counter, how can I create this failing counter and store it?

I’ve found a script, but I don’t know how to use it and where to save it.

:if ([/ping 8.8.8.8 count=6 size=64 interval=600s]=0) do={
:log error “reboot”;
/system reboot;
} else={
:log info “ping main router is FINE”;
}

thank you!

Use watchdog instead:

https://help.mikrotik.com/docs/display/ROS/Watchdog

Ping watchdog can monitor connectivity to a specific IP address and trigger the reboot function.

Thanks for reply, watchdog has only timeout, which can be max 600 seconds, I need 3600 to reduce ne number of reboots in cas of long failing
Anyway, if I set it for example to 10 min, will it send one packet only and wait it for 10 minutes? Or will it send multiple pacets during this time? There is a significative differenze, if target appears between this time it will be ignored.

You can set no-ping-delay (ping-start-after-boot) to 10m for exactly that purpose (to limit the number of reboots).


Anyway, if I set it for example to 10 min, will it send one packet only and wait it for 10 minutes? Or will it send multiple pacets during this time?

https://help.mikrotik.com/docs/display/ROS/Watchdog


ping-timeout (time; Default: 60s)
Specifies the time interval in which the device will be pinged 6 times (after “no-ping-delay”).

The amount of pings is fixed. The timeout can be widened or shortened. You don’t need to alter the timeout to limit the reboots, use no-ping-delay (ping-start-after-boot) for that.

Not sure if related but, this is what I have in Netwatch:

:if ([/ping 1.1.1.1 count=1 interval=1 as-value]->"ttl") do={
    :log ...
} else={
    :log ...
    ....
}

or

:if ([/ping [/ip route get [find comment~"..."] gateway] count=1 interval=1 as-value]->"status"="timeout") do={...

Thanks nescafe2002:
The method has its logic, I would know how many attempts it does during the timeout, do you know about it?
I would now focus to netwatch, it’s more powerful because it has script and I can better do what I need.

Thanks Simonej:
I suppose I need to write it on “Down” tab, but I don’t understand the logic, it execute a script, so it fails once and it execute a script that in my case will run 60 minutes, while it tests the connection every 10 minutes, exits if succeed and run a reboot if exceed the 6th attempt, is it right?

By the time an interval will come, and til will be runned another scriptm which will work in parallel with delay..
Is all right?

may somebody please clear me tha latest questions, thank you

I didn’t understood what you’re trying to achieve… Tuning Watchdog seems to be enough for delay the reboot, or if you want fine-tuning with Netwatch you could check an host, when down with a script, checking another IP, if not reachable your AP will reboot, you can also add the :delay

Yes I would like to fine-tuning the process, don’t reboot at first fail, I would count 6 times, then reboot
If I put in the script this:
:if ([/ping 8.8.8.8 count=6 size=64 interval=600s]=0) do={
:log error “reboot”;
/system reboot;
} else={
:log info “ping main router is FINE”;
}

and it will keep 1 hour to conclude the for cycle, by the time, the netwatch have already failed again and started new the same scripts? Is ok to use such way for counting?
THe ideal for me is the netwatch will trigger and add +1 somewhere ad the numer of attept, if it’s more than.. it stores back the number of attempts to zero and execute what I need, for example power off/on the USB.
There is a way to store a number, for example on disk?
Thank you!

working with this

:local attempts [:pick [/file get attempts.txt contents] 0]
:if ([:len $attempts] = 0) do={
    /file set attempts.txt contents=0
    :set attempts 0
}

:local pingResult [/ping 8.8.8.8 count=6 size=64 ]
:if ($pingResult = 0) do={
    :set attempts ($attempts + 1)
    /file set attempts.txt contents=$attempts
    :if ($attempts >= 3) do={
        :log error "Rebooting after $attempts failed attempts"
        /system reboot
    } else={
        :log info "Attempt $attempts: Ping to main router failed"
    }
} else={
    /file set attempts.txt contents=0
    :log info "Ping to main router is FINE"
}

i add more scheduler to reset attempts.txt 15mins to 0