Sample: the settings block
This is the only part a person edits: links, failover order, critical sites, alert mail and thresholds. Everything below it in the file is left alone. Addresses and names here are examples.
###################################################################
##### #####
##### MULTI-WAN AUTO CONFIGURATOR #####
##### #####
##### Fill in this block. Nothing below it needs editing. #####
##### #####
##### Safe to run on a LIVE router. It removes only its own #####
##### objects before rebuilding, so re-running never #####
##### duplicates anything and never needs a factory reset. #####
##### Change a gateway, add an ISP, add a mail recipient - #####
##### edit here, re-import, done. The router stays up. #####
##### #####
##### Every setting is checked before anything is touched. #####
##### A bad value aborts with a clear reason in the log #####
##### rather than half-configuring the router. #####
##### #####
###################################################################
:local IDENTITY "MIK-LAB"
:local TZ "Asia/Kolkata"
###################################################################
##### LAN #####
###################################################################
:local LANIFACE "LAN"
:local LANADDR "192.168.88.254/24"
:local LANNET "192.168.88.0/24"
:local DHCPSTART "192.168.88.10"
:local DHCPEND "192.168.88.100"
:local DNSSERVERS "8.8.8.8,8.8.4.4"
###################################################################
##### PHYSICAL PORTS #####
##### Check against the actual hardware. A wrong entry #####
##### renames nothing and the install aborts on purpose #####
##### rather than building on interfaces that do not exist. #####
###################################################################
:local PORTWAN1 "ether1"
:local PORTWAN2 "ether2"
:local PORTWAN3 "ether3"
:local PORTWAN4 "ether4"
:local PORTLAN "ether5"
###################################################################
##### MODE #####
###################################################################
# false = failover, one link at a time, in the ORDER below
# true = PCC load balance across the primary group
:local ISPCC false
# Load balancing always hashes on the client's own address (src-address),
# so one PC keeps one public IP and HTTPS sessions never see it change.
# That is fixed on purpose and is not a setting.
# Virtual router (CHR/Proxmox) rather than real hardware? A virtual
# NIC always reports link-ok, so port status is meaningless there
# and is reported as "CHR" instead of a bogus link state.
:local ISCHR false
# Failover mode only. A healthy link that is NOT the one carrying
# traffic: false = its routes are disabled
# true = left enabled but idle (distance still decides)
:local DONTTURNOFF false
# Let RouterOS ping each gateway itself and drop the route when it
# stops answering - about 20s, and works even if the script is
# broken. Sees gateway failure only, not "modem up, internet bad".
:local CHECKGW true
###################################################################
##### FAILOVER ORDER #####
##### #####
##### Which internet is tried 1st, 2nd, 3rd, 4th. #####
##### Values are WAN numbers. #####
##### #####
##### 1,2,3,4 normal - WAN_1 preferred #####
##### 2,3,4,1 WAN_1 demoted to LAST #####
##### #####
##### Use this when a link is technically up but behaving #####
##### badly and you want it only as a last resort. Route #####
##### distance follows this too, so a demoted link also #####
##### loses on distance, not just inside the script. #####
##### #####
##### Must list each of 1,2,3,4 exactly once. #####
###################################################################
:local ORDER1 1
:local ORDER2 2
:local ORDER3 3
:local ORDER4 4
###################################################################
##### THE FOUR INTERNET LINKS #####
##### #####
##### MODE "static" you type the address and gateway #####
##### "dhcp" the modem hands them out #####
##### #####
##### GW an IP address, e.g. "192.168.4.254" #####
##### or "auto" - take whatever gateway the modem #####
##### hands out, and follow it if it #####
##### ever changes. Needs MODE "dhcp". #####
##### #####
##### Every WAN has a DHCP client; it is switched ON only #####
##### for a link whose GW is "auto". When it is, the static #####
##### address on that port is removed once the modem has #####
##### answered, and the removal is written to the log. #####
##### #####
##### Use "auto" at sites where the modem gets replaced #####
##### from time to time and the replacement hands out a #####
##### different gateway. Nobody has to edit anything: the #####
##### routes follow the new modem within a second, and the #####
##### change is written to the log. #####
##### #####
##### ADDR only used when MODE is "static" #####
##### RANGE the modem's whole subnet, so LAN clients can #####
##### still reach the modem's own web page. #####
##### "auto" (use it with a dhcp link) takes the #####
##### subnet from the modem's lease and follows it #####
##### if a replacement modem uses a different one. #####
##### A static link needs the subnet typed in, such #####
##### as "192.168.4.0/24". #####
###################################################################
###################################################################
##### INTERNET 1 #####
###################################################################
:local W1ACTIVE true
:local W1MODE "dhcp"
:local W1ADDR "192.168.1.2/24"
:local W1GW "auto"
:local W1RANGE "auto"
:local W1ISP "ProviderOne"
:local W1GROUP "PrimaryPccGroup"
###################################################################
##### INTERNET 2 #####
###################################################################
:local W2ACTIVE true
:local W2MODE "dhcp"
:local W2ADDR "192.168.2.2/24"
:local W2GW "auto"
:local W2RANGE "auto"
:local W2ISP "ProviderTwo"
:local W2GROUP "PrimaryPccGroup"
###################################################################
##### INTERNET 3 #####
###################################################################
:local W3ACTIVE false
:local W3MODE "dhcp"
:local W3ADDR "192.168.5.100/24"
:local W3GW "auto"
:local W3RANGE "auto"
:local W3ISP "NOTCONFIGURED"
:local W3GROUP "PrimaryPccGroup"
###################################################################
##### INTERNET 4 #####
###################################################################
:local W4ACTIVE false
:local W4MODE "dhcp"
:local W4ADDR "192.168.15.100/24"
:local W4GW "auto"
:local W4RANGE "auto"
:local W4ISP "NOTCONFIGURED"
:local W4GROUP "PrimaryPccGroup"
###################################################################
##### TIER 1 - is the link alive #####
##### Generic targets, averaged over the ones that ANSWER. #####
##### Use three different operators so one of them blocking #####
##### ICMP is not mistaken for an outage. #####
###################################################################
:local T1A "8.8.8.8"
:local T1B "1.1.1.1"
:local T1C "9.9.9.9"
# PINGGAP is the wait between pings, in ms. It is never used below LATFAIL:
# a reply that comes after the next ping has gone out counts as LOST, so a
# 4G link answering in 200 ms read as broken with a 200 ms gap.
:local PINGCOUNT 3
:local PINGGAP 600
:local LATFAIL 600
:local LOSSFAIL 60
###################################################################
##### TIER 2 - can we do our job #####
##### #####
##### Business-critical destinations. Each is PASS/FAIL on #####
##### its own: ANY failure disqualifies that link, however #####
##### healthy Tier 1 looks. This is what catches "the #####
##### internet works but our supplier blocks this ISP". #####
##### #####
##### method: #####
##### http80 = GET on port 80, codes below. A 301 counts #####
##### as success - the server is talking to us. #####
##### A WAF or IP block returns 403, which is #####
##### not in the list, so it is caught. #####
##### tcp443 = TCP connect to 443. For hosts that do not #####
##### serve port 80, e.g. outlook.office365.com. #####
##### off = slot unused. #####
##### #####
##### Tier 2 can also RESCUE Tier 1: if ICMP is blocked by #####
##### an ISP but every critical target answers over TCP, #####
##### the link demonstrably works and is kept in service. #####
###################################################################
# How many consecutive failed checks before a Tier 2 target counts as
# really down. A single blip must not disqualify a working link: big
# services answer from dozens of front-ends and one of them hiccuping
# is normal. 3 checks at 30s is about 90 seconds of sustained failure.
:local T2FAILS 3
:local C1HOST "example.com"
:local C1METHOD "http80"
:local C1CODES "200,301,302"
:local C2HOST "outlook.office365.com"
:local C2METHOD "tcp443"
:local C2CODES "200"
:local C3HOST ""
:local C3METHOD "off"
:local C3CODES "200"
###################################################################
##### FLAP DAMPING - for a line that keeps bouncing #####
##### #####
##### A "flap" is a link switching between UP and DOWN. #####
##### One outage is simple: traffic moves to another link #####
##### and stays there. A link that FLAPS - a modem that #####
##### reboots in a loop, a loose cable - is worse: traffic #####
##### is moved back and forth and everybody's connections #####
##### drop every time. So a link that flaps too much is #####
##### PARKED: kept out of use, even while it looks healthy, #####
##### until it has settled down. #####
##### #####
##### FLAPCHECK true = protection on, false = off #####
##### MAXFLAPS how many UP/DOWN changes are put up with #####
##### FLAPWIN MINUTES. Parked when the link has changed #####
##### more than MAXFLAPS times and the last #####
##### change was less than this long ago #####
##### RECOVERWIN MINUTES a parked link must stay UP without#####
##### changing before it is trusted again #####
##### #####
##### Example: MAXFLAPS 5, FLAPWIN 20, RECOVERWIN 2. #####
##### The 6th change parks the link. After 2 steady #####
##### minutes up it is released. #####
###################################################################
:local FLAPCHECK true
:local MAXFLAPS 5
:local FLAPWIN 20
:local RECOVERWIN 2
# Seconds after boot before failover acts and mail is sent, so a
# router coming up does not alarm while links are still settling.
:local BOOTGRACE 120
###################################################################
##### LOGGING #####
##### #####
##### 0 off nothing. Alert emails still go out. #####
##### 1 notice the status block every cycle - one row #####
##### per link, its state, and which one is #####
##### carrying traffic - plus every change #####
##### and alert. Use this on a client site. #####
##### 2 verbose adds packet loss, latency and port speed #####
##### per link, and a start and end line for #####
##### every script with the seconds it took. #####
##### The 1000-line log then holds under an #####
##### hour, so use it when chasing a problem. #####
##### 3 debug adds internal tracing. Noisy. #####
###################################################################
:local LOGLEVEL 2
###################################################################
##### EMAIL #####
##### #####
##### One mail per genuine transition per link - a down #####
##### mail when it fails, an up mail when it recovers, and #####
##### nothing in between however long it stays that way. #####
##### #####
##### The SMTP password is deliberately NOT here. This file #####
##### travels between machines and into git. Set it once on #####
##### the router: #####
##### /tool e-mail set password="app-password" #####
##### and the installer never touches it again. #####
##### #####
##### Leave a recipient slot as "" to disable it. #####
###################################################################
:local MAILON true
:local MAILFROM "alerts@example.com"
:local MAILTO1 "admin@example.com"
:local MAILTO2 ""
:local MAILTO3 ""
:local MAILSERVER "smtp.gmail.com"
:local MAILPORT 587
###################################################################
##### LAB ONLY #####
##### true = ssh stays reachable so the bench can drive it #####
##### false = client site: everything except winbox is off #####
###################################################################
:local LABMODE truePhotos
Detail
- The problem: two or three internet lines, a router that could use them all, and nobody with time to set it up. When a line drops, the first sign is that the internet stops.
- The tool: a configurator for MikroTik RouterOS 7 routers with up to 26 internet links. Import one file once and the router builds everything itself.
- Two modes, one setting: non-PCC failover (one link at a time, in a set order) or PCC load balancing across a group of links. Switching is a true/false setting and a re-import of the same file. The router removes only its own objects and rebuilds them, so no script is rewritten and nothing is duplicated.
- Every 30 seconds: it checks every link, moves traffic to a healthy one when a link fails, and moves it back when the preferred link recovers.
- Alert emails: one mail when a link goes down and one when it comes back, showing each link's state, its port, who flapped and which link is carrying traffic. Nothing in between, however long a state lasts.
- The log: every cycle a status block names each link, whether it is up, and which one carries traffic, with the time each script took.
- Built to be safe: small numbered scripts, each with one job, so the one a person edits is the only one they can break.
- When something fails: the router names the failing step in its log and carries on doing what it was already doing.
- Version: 2.11, evolved from a configurator already used at client sites.
- Proven on the bench: failover, return, load sharing and live changes. Refined from its first field site, where a mobile link exposed a false packet-loss reading that is now fixed.
How it is built
Seven scripts run in order every 30 seconds, plus three tools for field engineers. Each has a header saying what it reads, what it writes and where to look when something is wrong.
0_WAN_MonitorRuns every 30 seconds and calls the others in order; waits if a previous run is still going.1_WAN_SettingsThe only script a person edits: links, failover order, critical sites and thresholds.2_WAN_DhcpFollows each modem's address and gateway, and switches a link between automatic and typed settings.3_WAN_CheckMeasures every link: reachability, delay, loss and the critical sites. It only looks.4_WAN_DecideJudges each link up or down, applies flap protection and picks the link to use.5_WAN_ApplyThe only script that changes the router: moves the routes and load-balancing rules to match the decision.6_WAN_ReportWrites the status block to the log, with the reason for every change.WAN_Add_LinkAdds one new internet link to a running router.WAN_Remove_LinkRemoves one link and everything that was built for it.WAN_Test_OutageSwitches one link off for a set time to test failover, then switches it back on by itself.What it does
Install and update
- One file imported once; the router builds everything itself
- Safe to run again: it removes only its own tagged objects and rebuilds them, so no duplicates
- Every setting is checked first; a bad value stops the install with the reason in the log
- Automatic self-check after install, reported as "N of N passed"
- A scripts-only update for sites where a full re-import is not an option
Internet links
- Up to 26 links per router, including phones and 4G modems
- Gateway taken from the modem, or typed by hand, and switched live between the two
- A typed gateway must answer before anything changes
- Two ISPs on the same gateway address cannot cross over
- Add or remove a link on a running router
Health checking
- Two levels: "is the link alive" and "can this site do its job"
- Each link is tested only through its own path, never silently over another
- Business-critical sites checked one by one; selective blocking by an ISP is caught
- If a critical site fails on every link, the site is down, not the links
- Plain-language reasons in the log
Failover and load balancing
- Switch between non-PCC failover and PCC load balancing with one setting and a re-import - no reprogramming
- Failover in a set order: one 30-second cycle to switch, one to come back
- PCC load balancing by client address, so a PC keeps one public IP
- Shares are recalculated when a link drops, so the survivors stay even
- Flap protection parks a link that keeps going up and down
Field engineer tools
- Test a failure: switch a link off for 1 to 60 minutes and watch failover happen
- The link comes back by itself, even if the router restarts during the test
- Refuses to switch off the only working internet connection
- Add or remove a link on a running router, with any number from 1 to 26
Reporting and security
- A status block in the log, one row per link in use
- One email when a link goes down and one when it recovers, with the state of every link
- Every change is logged with its reason, in plain language
- The router's plain web service is off and inbound traffic from the internet is dropped
- No passwords or keys ever appear in the settings