Community discussions

MikroTik App
just joined
Topic Author
Posts: 2
Joined: Wed Jul 15, 2015 10:44 pm

local server failover

Tue Jan 12, 2021 11:31 pm


Im running two servers in my local network.This script below works fine - so when the SERVER1 running on is down, it will enable NAT rule to the SERVER2 and disable NAT rule to SERVER1. When the SERVER1 is up again, it will enable NAT rule to the SERVER1 and disable NAT rule to SERVER2.

:if ([/ping count=5]=0) \
do={:log info "SERVER1 is DOWN"; /ip firewall nat set [find comment="SERVER1"] disabled=yes; /ip firewall nat set [find comment="SERVER2"] disabled=no} \
else={:log info "SERVER1 is UP"; /ip firewall nat set [find comment="SERVER1"] disabled=no; /ip firewall nat set [find comment="SERVER2"] disabled=yes};

But the problem is that every 5sec my logs are getting flooded by:
script,info SERVER1 is DOWN
system,info nat rule changed by someuser
system,info nat rule changed by someuser

Is there any elegant solution that logs above will be generated only when failover take place?
I was trying to search it and tried many solutions but nothing worked for me.

Many thanks
Forum Guru
Forum Guru
Posts: 6444
Joined: Mon Apr 20, 2009 9:11 pm

Re: local server failover

Wed Jan 13, 2021 12:00 am

It's surely doable with few more ifs, in each branch (do/else) first check if the desired state is already active, and only do any changes when it isn't.
Excessive quoting is useless and annoying. If you use it, please consider if you could do without it.
User avatar
Forum Guru
Forum Guru
Posts: 1871
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at

Re: local server failover

Wed Jan 13, 2021 11:34 pm

This may do. (Not tested)
Avoid having multiple commands on same line and use tab for if and tests. Looks better and easier to read.

Script should only run change to up part if it was down before, and not all time.
:local status true
:if ([/ping count=5]=0) do={
	:log info "SERVER1 is DOWN"
	/ip firewall nat set [find comment="SERVER1"] disabled=yes
	/ip firewall nat set [find comment="SERVER2"] disabled=no
	:set status false
} else={
	:if ($status = false) do={
		:log info "SERVER1 is UP"
		/ip firewall nat set [find comment="SERVER1"] disabled=no
		/ip firewall nat set [find comment="SERVER2"] disabled=yes
		:set status true
Why do not use Splunk to monitor your MikroTik Router(s)? Look at this page in how to set it up.


Who is online

Users browsing this forum: perlasnialex and 15 guests