Mikrotik failover

{
# =====================================================================
#  PROFI DUAL-WAN FAILOVER (ROS 7.21+) - EVENT DRIVEN + DUAL IP
# =====================================================================

:put "Zacinam konfiguraci dual-WAN failoveru..."

# 1. Vytvoření statických rout
/ip route add dst-address=0.0.0.0/0 gateway=127.0.0.1 distance=1 comment="WAN1-Default"
/ip route add dst-address=0.0.0.0/0 gateway=127.0.0.1 distance=10 comment="WAN2-Default"
/ip route add dst-address=1.1.1.1/32 gateway=127.0.0.1 comment="WAN1-Monitor"
/ip route add dst-address=9.9.9.9/32 gateway=127.0.0.1 comment="WAN1-Monitor2"
/ip route add dst-address=8.8.8.8/32 gateway=127.0.0.1 comment="WAN2-Monitor"
/ip route add dst-address=1.0.0.1/32 gateway=127.0.0.1 comment="WAN2-Monitor2"

# 2. Úprava/Tvorba DHCP klientů (pro ether1 a ether2)
/ip dhcp-client add interface=ether1 add-default-route=no disabled=no script=":if (\$bound=1) do={\r\n  /ip route set [find comment=\"WAN1-Default\"] gateway=\$\"gateway-address\"\r\n  /ip route set [find comment=\"WAN1-Monitor\"] gateway=\$\"gateway-address\"\r\n  /ip route set [find comment=\"WAN1-Monitor2\"] gateway=\$\"gateway-address\"\r\n}\r\n:if (\$bound=0) do={\r\n  /ip route set [find comment=\"WAN1-Default\"] distance=20\r\n}"
/ip dhcp-client add interface=ether2 add-default-route=no disabled=no script=":if (\$bound=1) do={\r\n  /ip route set [find comment=\"WAN2-Default\"] gateway=\$\"gateway-address\"\r\n  /ip route set [find comment=\"WAN2-Monitor\"] gateway=\$\"gateway-address\"\r\n  /ip route set [find comment=\"WAN2-Monitor2\"] gateway=\$\"gateway-address\"\r\n}\r\n:if (\$bound=0) do={\r\n  /ip route set [find comment=\"WAN2-Default\"] distance=30\r\n}"

# 3. Netwatch pro WAN1 (VDSL - 1.1.1.1 a 9.9.9.9)
:local nwDown1 "# Zkontroluje, zda jsou OBE adresy ve stavu down\r\n:if ([/tool netwatch get [find host=\"1.1.1.1\"] status] = \"down\" && [/tool netwatch get [find host=\"9.9.9.9\"] status] = \"down\") do={\r\n  :if ([/ip route get [find comment=\"WAN1-Default\"] distance] = 1) do={\r\n    /ip route set [find comment=\"WAN1-Default\"] distance=20\r\n    /ip firewall connection remove [find]\r\n    /ip dns cache flush\r\n    :log warning \"LINK-ALERT: WAN1 je DOWN!\"\r\n    /tool e-mail send to=\"alert@email.com\" subject=\"[MIKROTIK] ALARM: WAN1 je DOWN\" body=\"Primarni linka spadla. Prepnuto na zalohu.\"\r\n  }\r\n}"
:local nwUp1 "# Staci, aby ozila aspon JEDNA adresa\r\n:if ([/tool netwatch get [find host=\"1.1.1.1\"] status] = \"up\" || [/tool netwatch get [find host=\"9.9.9.9\"] status] = \"up\") do={\r\n  :if ([/ip route get [find comment=\"WAN1-Default\"] distance] = 20) do={\r\n    /ip route set [find comment=\"WAN1-Default\"] distance=1\r\n    /ip dns cache flush\r\n    :log warning \"LINK-ALERT: WAN1 je zpet UP.\"\r\n    /tool e-mail send to=\"alert@email.com\" subject=\"[MIKROTIK] INFO: WAN1 naskocila\" body=\"Navrat na primarni spoj.\"\r\n  }\r\n}"

/tool netwatch add host=1.1.1.1 interval=3s packet-count=5 thr-avg=150ms thr-loss-percent=5 type=icmp comment="WAN1-Quality-Check" down-script=$nwDown1 up-script=$nwUp1
/tool netwatch add host=9.9.9.9 interval=3s packet-count=5 thr-avg=150ms thr-loss-percent=5 type=icmp comment="WAN1-Quality-Check" down-script=$nwDown1 up-script=$nwUp1

# 4. Netwatch pro WAN2 (LTE - 8.8.8.8 a 1.0.0.1)
:local nwDown2 "# Zkontroluje, zda jsou OBE adresy ve stavu down\r\n:if ([/tool netwatch get [find host=\"8.8.8.8\"] status] = \"down\" && [/tool netwatch get [find host=\"1.0.0.1\"] status] = \"down\") do={\r\n  :if ([/ip route get [find comment=\"WAN2-Default\"] distance] = 10) do={\r\n    /ip route set [find comment=\"WAN2-Default\"] distance=30\r\n    /ip firewall connection remove [find]\r\n    :log warning \"LINK-ALERT: WAN2 je DOWN!\"\r\n    /tool e-mail send to=\"alert@email.com\" subject=\"[MIKROTIK] ALARM: WAN2 je DOWN\" body=\"Zalozni linka spadla.\"\r\n  }\r\n}"
:local nwUp2 "# Staci, aby ozila aspon JEDNA adresa\r\n:if ([/tool netwatch get [find host=\"8.8.8.8\"] status] = \"up\" || [/tool netwatch get [find host=\"1.0.0.1\"] status] = \"up\") do={\r\n  :if ([/ip route get [find comment=\"WAN2-Default\"] distance] = 30) do={\r\n    /ip route set [find comment=\"WAN2-Default\"] distance=10\r\n    :log warning \"LINK-ALERT: WAN2 je zpet UP.\"\r\n    /tool e-mail send to=\"alert@email.com\" subject=\"[MIKROTIK] INFO: WAN2 naskocila\" body=\"Zalozni spoj je opet pripraven.\"\r\n  }\r\n}"

/tool netwatch add host=8.8.8.8 interval=5s packet-count=5 thr-avg=400ms thr-loss-percent=10 type=icmp comment="WAN2-Quality-Check" down-script=$nwDown2 up-script=$nwUp2
/tool netwatch add host=1.0.0.1 interval=5s packet-count=5 thr-avg=400ms thr-loss-percent=10 type=icmp comment="WAN2-Quality-Check" down-script=$nwDown2 up-script=$nwUp2

:put "=========================================================="
:put "HOTOVO! Proved RENEW na DHCP klientech pro nacteni bran."
:put "=========================================================="
}