Skip to content

Instantly share code, notes, and snippets.

@ilium007
Created January 11, 2026 03:40
Show Gist options
  • Select an option

  • Save ilium007/efdbd7b5204c907eaf030f22abd9090e to your computer and use it in GitHub Desktop.

Select an option

Save ilium007/efdbd7b5204c907eaf030f22abd9090e to your computer and use it in GitHub Desktop.
# Script: wan-watchdog
# Reboot logic:
# - Reboot only if TTL4 is down AND both E2E checks are down
# - Condition must persist for downSecondsRequired
# - Cooldown prevents reboot loops
# - UP debounce to avoid flapping (state changes only after stable up)
:local ttlComment "WAN-TTL4"
:local e2e1Comment "WAN-E2E-1"
:local e2e2Comment "WAN-E2E-2"
:local runIntervalSec 10
:local downSecondsRequired 60
:local upSecondsRequired 30
:local cooldownSeconds 300
:local downThreshold ($downSecondsRequired / $runIntervalSec)
:local upThreshold ($upSecondsRequired / $runIntervalSec)
:local cooldownCycles ($cooldownSeconds / $runIntervalSec)
:global "wan_wd_down_count"
:global "wan_wd_up_count"
:global "wan_wd_cooldown"
:global "wan_wd_state"
:if ([:typeof $"wan_wd_down_count"] = "nothing") do={ :set "wan_wd_down_count" 0 }
:if ([:typeof $"wan_wd_up_count"] = "nothing") do={ :set "wan_wd_up_count" 0 }
:if ([:typeof $"wan_wd_cooldown"] = "nothing") do={ :set "wan_wd_cooldown" 0 }
:if ([:typeof $"wan_wd_state"] = "nothing") do={ :set "wan_wd_state" "up" }
:if ($"wan_wd_cooldown" > 0) do={ :set "wan_wd_cooldown" ($"wan_wd_cooldown" - 1) }
:local idT [/tool netwatch find where comment=$ttlComment]
:local id1 [/tool netwatch find where comment=$e2e1Comment]
:local id2 [/tool netwatch find where comment=$e2e2Comment]
:if ([:len $idT] = 0) do={ :log error "wan-watchdog: missing netwatch entry WAN-TTL4"; :return }
:if ([:len $id1] = 0) do={ :log error "wan-watchdog: missing netwatch entry WAN-E2E-1"; :return }
:if ([:len $id2] = 0) do={ :log error "wan-watchdog: missing netwatch entry WAN-E2E-2"; :return }
:local sT [:tostr [/tool netwatch get $idT status]]
:local s1 [:tostr [/tool netwatch get $id1 status]]
:local s2 [:tostr [/tool netwatch get $id2 status]]
:local allDown false
:if (($sT = "down") && ($s1 = "down") && ($s2 = "down")) do={ :set allDown true }
:local allUp false
:if (($sT = "up") && ($s1 = "up") && ($s2 = "up")) do={ :set allUp true }
:if ($allDown) do={
:set "wan_wd_down_count" ($"wan_wd_down_count" + 1)
:set "wan_wd_up_count" 0
:if (($"wan_wd_down_count" >= $downThreshold) && ($"wan_wd_cooldown" = 0)) do={
:if ($"wan_wd_state" != "down") do={
:set "wan_wd_state" "down"
:log warning ("wan-watchdog: WAN DOWN stable. Status: TTL4=" . $sT . " E2E-1=" . $s1 . " E2E-2=" . $s2)
}
:set "wan_wd_cooldown" $cooldownCycles
:set "wan_wd_down_count" 0
/system reboot
#:log warning "wan-watchdog: WOULD reboot now (test mode)"
}
} else={
:set "wan_wd_down_count" 0
:if ($allUp) do={
:set "wan_wd_up_count" ($"wan_wd_up_count" + 1)
:if ($"wan_wd_up_count" >= $upThreshold) do={
:if ($"wan_wd_state" != "up") do={
:set "wan_wd_state" "up"
:log info ("wan-watchdog: WAN UP stable. Status: TTL4=" . $sT . " E2E-1=" . $s1 . " E2E-2=" . $s2)
}
}
} else={
:set "wan_wd_up_count" 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment