I made the script below, that is in production.
Basically what it does is check the routing table for routes in a given subnet, if no routes are available, make the vrrp-instance go to backup-state.
Could you guys look it over and see if there is any optimilization possible, or maybe something more effecient?
Code: Select all
# Set route counter to zero before checking
:global routeCounter 0
# Specify the BGP advertised subnet to check for
:global subnet "10.2.0.0/20"
# Specify the name of the VRRP interface to work with
:global vrrpInterface1 "vrrp-v4"
:global vrrpInterface2 "vrrp-v6"
# Specify the priority the $vrrpInterface should get once the BGP routes are gone
:global newPrio 50
# Loop through all routes and check for BGP routes with given subnet
:foreach route in=[/ip route find where dst-address in $subnet and bgp] do={
# Increment the value of $routeCounter with every found route
:set $routeCounter ($routeCounter+1)
}
# When $routeCounter is zero, that means that there are no BGP routes to given subnet
if ($routeCounter = 0) do={
# Get the current VRRP priority of given VRRP interface
:global curPrio [/interface vrrp get [find name=$vrrpInterface1] priority]
# If the current VRRP priority is different then the $newPrio
if ($curPrio != $newPrio) do={
# Produce logging
:log error "================ WARNING ================"
:log error "No BGP routes to $subnet found."
:log error "Setting $vrrpInterface1 priority from $curPrio to $newPrio"
:log error "Setting $vrrpInterface2 priority from $curPrio to $newPrio"
:log error "========================================="
# Set the new priority of given VRRP interface to force a Master/Backup switch
:put [/interface vrrp set [find name=$vrrpInterface1] priority=$newPrio]
:put [/interface vrrp set [find name=$vrrpInterface2] priority=$newPrio]
}
}