Guest User

Untitled

a guest
Sep 25th, 2023
4,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #Main interface name
  2.  
  3. :global MainIf ether2
  4.  
  5. #Failover interface name
  6.  
  7. :global RsrvIf ether1
  8.  
  9. #Minimall RTT for ping
  10. :local MinRtt "00:00:00.200"
  11.  
  12. #Minimal ping cout for routeros 7 - 3
  13. :local PingCount 3
  14.  
  15. #Ping targets
  16. :local PingTargets {1.1.1.1; 8.8.4.4; 9.9.9.9; 77.88.8.8}
  17.  
  18. :global ClientName "Example LLC";
  19. :global MainEmail [email protected]
  20. :global MainCN 12345
  21.  
  22. :global ReservEmail [email protected]
  23. :global ReservCN 12345
  24.  
  25. :local host
  26.  
  27. :local MainIfInetOk false
  28.  
  29. :local RsrvIfInetOk false
  30.  
  31. :local MainPings 0
  32.  
  33. :local RsrvPings 0
  34.  
  35. :local MainPingRtt 0
  36.  
  37. :local RsrvPingRtt 0
  38.  
  39. :local MainPingRtt 0
  40.  
  41. :local MainPingRttSumm 0
  42.  
  43. :local RsrvPingRtt 0
  44.  
  45. :local RsrvPingRttSumm 0
  46.  
  47. :local MainPingCont 0
  48. :local RsrvPingCont 0
  49.  
  50. :local MainPingProblemNum 0
  51. :local RsrvPingProblemNum 0
  52.  
  53. foreach host in=$PingTargets do={
  54.  
  55. :local res [/ping $host count=$PingCount interface=$MainIf]
  56.  
  57. :set MainPings ($MainPings + $res)
  58.  
  59. #:put $MainPings
  60.  
  61.  
  62. :local MainPingRttTest [/ping $host count=$PingCount interface=$MainIf as-value]
  63.  
  64. :foreach try in=$MainPingRttTest do={:set MainPingRtt ($MainPingRtt + ($try->"time"))}
  65. :set MainPingRtt ($MainPingRtt / [:len $MainPingRttTest])
  66. :if (($MainPingRtt < $MinRtt)) do={:set $MainPingRtt 1} else={:set MainPingRtt 0}
  67. #:put $MainPingRtt
  68. :set $MainPingRttSumm ($MainPingRttSumm + $MainPingRtt)
  69. :set $MainPingRtt 0
  70. :set $MainPingCont ($MainPingCont + 1)
  71.  
  72. :local res [/ping $host count=$PingCount interface=$RsrvIf]
  73.  
  74. :set RsrvPings ($RsrvPings + $res)
  75. #:put $RsrvPings
  76.  
  77. :local RsrvPingRttTest [/ping $host count=$PingCount interface=$RsrvIf as-value]
  78.  
  79. :foreach try in=$RsrvPingRttTest do={:set RsrvPingRtt ($RsrvPingRtt + ($try->"time"))}
  80. :set RsrvPingRtt ($RsrvPingRtt / [:len $RsrvPingRttTest])
  81. :if (($RsrvPingRtt < $MinRtt)) do={:set $RsrvPingRtt 1} else={:set $RsrvPingRtt 0}
  82. #:put $RsrvPingRtt
  83. :set $RsrvPingRttSumm ($RsrvPingRttSumm + $RsrvPingRtt)
  84. :set $RsrvPingRtt 0
  85. :set $RsrvPingCont ($RsrvPingCont + 1)
  86.  
  87. }
  88.  
  89.  
  90. #:put ($MainPings + $MainPingRttSumm)
  91. #:put $MainPingCont
  92. #:put $PingCount
  93. #:put $MainPingRttSumm
  94. :set $MainPingProblemNum (($MainPingCont * $PingCount + $MainPingCont))
  95. #:put $MainPingProblemNum
  96. :set MainIfInetOk (($MainPings + $MainPingRttSumm) >= ($MainPingProblemNum - $MainPingCont))
  97.  
  98. #:put ($RsrvPings + $RsrvPingRttSumm)
  99. :set $RsrvPingProblemNum (($RsrvPingCont * $PingCount + $RsrvPingCont))
  100. #:put $RsrvPingProblemNum
  101. :set RsrvIfInetOk (($RsrvPings + $RsrvPingRttSumm) >= ($RsrvPingProblemNum - $RsrvPingCont))
  102.  
  103.  
  104.  
  105. :put "MainIfInetOk=$MainIfInetOk"
  106.  
  107. :put "RsrvIfInetOk=$RsrvIfInetOk"
  108.  
  109.  
  110.  
  111. if (!$MainIfInetOk) do={
  112.  
  113. /log error "Main gateway down"
  114.  
  115. }
  116.  
  117.  
  118.  
  119. if (!$RsrvIfInetOk) do={
  120.  
  121. /log error "Reserve gateway error"
  122.  
  123. }
  124.  
  125.  
  126.  
  127. :local MainGWDistance [/ip route get [find comment="MainGW"] distance]
  128.  
  129. :local RsrvGWDistance [/ip route get [find comment="RsrvGW"] distance]
  130.  
  131. :put "MainGWDistance=$MainGWDistance"
  132.  
  133. :put "RsrvGWDistance=$RsrvGWDistance"
  134.  
  135.  
  136.  
  137. if ($MainIfInetOk && ($MainGWDistance >= $RsrvGWDistance)) do={
  138.  
  139. /ip route set [find comment="MainGW"] distance=1
  140.  
  141. /ip route set [find comment="RsrvGW"] distance=2
  142.  
  143. # Clear VoIP connections
  144.  
  145. /ip firewall connection remove [ find dst-address ~ ".*:5060" ]
  146.  
  147. /ip firewall connection remove [ find src-address ~ ".*:5060" ]
  148.  
  149. /log info "switched to main internet connection"
  150.  
  151. }
  152.  
  153.  
  154.  
  155. if (!$MainIfInetOk && $RsrvIfInetOk && ($MainGWDistance <= $RsrvGWDistance)) do={
  156.  
  157. /ip route set [find comment="MainGW"] distance=2
  158.  
  159. /ip route set [find comment="RsrvGW"] distance=1
  160.  
  161. # Clear VoIP connections
  162.  
  163. /ip firewall connection remove [ find dst-address ~ ".*:5060" ]
  164.  
  165. /ip firewall connection remove [ find src-address ~ ".*:5060" ]
  166.  
  167. /log info "switched to reserve internet connection"
  168.  
  169. /tool e-mail send from="[email protected]" [email protected] server=mail.example.ru port=587 [email protected] password=PassWord123 tls=starttls body="$ClientName on reserve ISP" subject="$ClientName MainGW Down ReservGW OK"
  170.  
  171. #/tool e-mail send from="[email protected]" to=$MainEmail server=mail.example.ru port=587 [email protected] password=PassWord123 tls=starttls body="We're have problem with internet connection. Contract number $MainCN. Best regards $ClientName." subject="$ClientName $MainCN problem with internet connection."
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment