-
Notifications
You must be signed in to change notification settings - Fork 28
/
wan-failover.rsc
69 lines (65 loc) · 2.91 KB
/
wan-failover.rsc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#-------------------------------------------------------------------------------
# MikroTik RouterOS WAN Failover
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Settings
#-------------------------------------------------------------------------------
:local PRIMARYROUTELABEL "PRIMARY_ROUTE"
:local BACKUPROUTELABEL "BACKUP_ROUTE"
# Hosts to ping via primary route (Yandex.DNS)
:local PRIMARYHOSTS {77.88.8.8; 77.88.8.7}
# Hosts to ping via backup route (Yandex.DNS)
:local BACKUPHOSTS {77.88.8.1; 77.88.8.3}
:local PINGCOUNT 3
:local PINGTRESHOLD 70
#-------------------------------------------------------------------------------
# Check primary route
#-------------------------------------------------------------------------------
:local primaryOk false
:local primarySuccessPingCount 0
:foreach host in=$PRIMARYHOSTS do={
:local n [/ping $host count=$PINGCOUNT]
:set primarySuccessPingCount ($primarySuccessPingCount + $n)
}
:set primaryOk (($primarySuccessPingCount * 100) >= ([:len $PRIMARYHOSTS] * $PINGCOUNT * $PINGTRESHOLD))
:if (!$primaryOk) do={
:log error "Primary internet connection down"
}
#-------------------------------------------------------------------------------
# Check backup route
#-------------------------------------------------------------------------------
:local backupOk false
:local backupSuccessPingCount 0
:foreach host in=$BACKUPHOSTS do={
:local n [/ping $host count=$PINGCOUNT]
:set backupSuccessPingCount ($backupSuccessPingCount + $n)
}
:set backupOk (($backupSuccessPingCount * 100) >= ([:len $BACKUPHOSTS] * $PINGCOUNT * $PINGTRESHOLD))
:if (!$backupOk) do={
:log error "Backup internet connection down"
}
#-------------------------------------------------------------------------------
# Failover
#-------------------------------------------------------------------------------
:local primaryDistance [/ip route get [find comment=$PRIMARYROUTELABEL] distance]
:local backupDistance [/ip route get [find comment=$BACKUPROUTELABEL] distance]
:if ($primaryOk && ($primaryDistance >= $backupDistance)) do={
# Switch to primary route
/ip route set [find comment=$PRIMARYROUTELABEL] distance=1
/ip route set [find comment=$BACKUPROUTELABEL] distance=2
:log warning "Switched to primary internet connection"
# Remove all connections
/ip firewall connection tracking set enabled=no
:delay 5s
/ip firewall connection tracking set enabled=yes
}
:if (!$primaryOk && $backupOk && ($primaryDistance <= $backupDistance)) do={
# Switch to backup route
/ip route set [find comment=$PRIMARYROUTELABEL] distance=2
/ip route set [find comment=$BACKUPROUTELABEL] distance=1
:log error "Switched to backup internet connection"
# Remove all connections
/ip firewall connection tracking set enabled=no
:delay 5s
/ip firewall connection tracking set enabled=yes
}