Community discussions

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

local server failover

Tue Jan 12, 2021 11:31 pm

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
 
Sob
Forum Guru
Forum Guru
Posts: 9119
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.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3292
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

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 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
	}
}
 
sec
just joined
Topic Author
Posts: 5
Joined: Wed Jul 15, 2015 10:44 pm

Re: local server failover

Sun Jan 17, 2021 8:25 pm

Thanks Jotne!

I have made a little adjustment and this works for me:
:local status true

/ip firewall nat set [find comment="SERVER1"] disabled=no
/ip firewall nat set [find comment="SERVER2"] disabled=yes

do {if (([/ping 192.168.0.249 count=5]=0) and ($status = true)) 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 (([/ping 192.168.0.249 count=5]=5) and ($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
	}
}
} while=( true )

Who is online

Users browsing this forum: No registered users and 20 guests