Page 1 of 1

local server failover

Posted: Tue Jan 12, 2021 11:31 pm
by sec
Hi,

Im running two servers in my local network.This script below works fine - so when the SERVER1 running on 192.168.0.249 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 192.168.0.249 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

Re: local server failover

Posted: Wed Jan 13, 2021 12:00 am
by Sob
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.

Re: local server failover

Posted: Wed Jan 13, 2021 11:34 pm
by Jotne
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 192.168.0.249 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
	}
}