WAN: ticket sustained slowdowns; reboot drops no longer mean "unstable"
Lint / JS (eslint) (push) Successful in 11s
Lint / Python (flake8) (push) Successful in 1m28s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m38s
Lint / Python (flake8) (pull_request) Successful in 57s
Test / Python Tests (pytest) (push) Successful in 1m20s
Lint / JS (eslint) (pull_request) Successful in 17s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 2m54s
Test / Python Tests (pytest) (pull_request) Successful in 3m9s

On 2026-11-23/24 the WAN was degraded for about 20 hours with no ticket,
and the only ticket came from the owner's reboot fixing it.

- wan_degraded (P2): half or more of the last 2 hours of UniFi WAN health
  periods slow, lossy or down. Closes with a recovery note once the last
  2 hours are at most a quarter bad. Mentions a slow ping from the modem.
- "WAN connection unstable" waits 10 minutes, then counts only drops that
  no modem restart explains (Smart Power, uptime reset, or the Restart
  modem button, folded into one restart when within 15 minutes).
- Both list the last 7 days of restarts and how long each lasted, and
  suggest asking AT&T for a replacement after 2+ restarts in a week.

Closes #58

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-23 23:51:26 -05:00
1 parent bba483bb05
commit a31437f2e5
5 files changed
+346 -17

No files matched your search

+89 -15
View File
@@ -2020,11 +2020,13 @@ class NetworkMonitor:
actionable = modem_api.apply_wan_events(
history, [e for e in events if not modem_api.is_update_key(e['key'])])
# UniFi repeats "failed multiple times" per failure, so ticket it once a day at most.
# UniFi repeats "failed multiple times" per failure, so ticket it once a day at most,
# and decide a few minutes later: a modem reboot's own drops trigger it too, and the
# uptime reset that proves the reboot only shows once the modem answers again.
unstable = [e for e in actionable if e['key'] == modem_api.LOG_WAN_UNSTABLE]
ticket_unstable = bool(unstable) and now - history.get('unstable_ticketed_at', 0) >= 86400
if ticket_unstable:
history['unstable_ticketed_at'] = now
if (unstable and not history.get('unstable_pending_at')
and now - history.get('unstable_ticketed_at', 0) >= 86400):
history['unstable_pending_at'] = now
# Save first: a failed ticket call shouldn't replay these events next cycle.
db.set_state('wan_history', history)
@@ -2041,21 +2043,88 @@ class NetworkMonitor:
'The internet connection failed badly enough to trigger auto power-cycle. '
'Check the WAN / Fiber panel for outages around this time and Rx power for a drop.',
)
if ticket_unstable:
self._one_shot_ticket(
suppressions, 'wan_unstable',
'UniFi reports the WAN connection went down multiple times in the last 24 hours.',
'WAN connection unstable', '2',
'See the WAN history on the modem page. Repeated drops with healthy Rx power '
'point toward the provider or the modem rather than the fiber.',
)
UNSTABLE_DECIDE_AFTER_S = 600
def _collect_wan_quality(self) -> None:
def _restart_context(self, now: float) -> tuple:
"""(restart episodes in the last 7 days, markdown lines, replacement hint)."""
raw = db.get_state('modem_restart')
try:
button = json.loads(raw) if raw else None
except ValueError:
button = None
episodes = modem_api.restart_episodes(self._load_wan_history(), button, now - 7 * 86400)
return episodes, modem_api.restart_lines(episodes, now), modem_api.replacement_hint(episodes)
def _decide_unstable(self, suppressions: list) -> None:
"""Ticket UniFi's "WAN failed multiple times" only if drops remain that no modem
restart explains. A reboot (from the AT&T app, Smart Power or Gandalf) drops the WAN
once or twice by itself, and that alone used to open this ticket."""
history = self._load_wan_history()
pending = history.get('unstable_pending_at')
now = time.time()
if not pending or now - pending < self.UNSTABLE_DECIDE_AFTER_S:
return
history.pop('unstable_pending_at', None)
episodes, lines, hint = self._restart_context(now)
drops = modem_api.unexplained_drops(history.get('outages'), episodes, now - 86400)
if len(drops) < 2:
db.set_state('wan_history', history)
logger.info(f'WAN "failed multiple times" explained by modem restarts '
f'({len(drops)} other drop(s) in 24 h): no ticket')
return
history['unstable_ticketed_at'] = now
db.set_state('wan_history', history)
description = (f'UniFi reports the WAN connection went down multiple times in the last 24 hours: '
f'{len(drops)} drops not explained by a modem restart.')
if lines:
description += '\n\nModem restarts in the last 7 days:\n' + '\n'.join(lines)
self._one_shot_ticket(
suppressions, 'wan_unstable', description, 'WAN connection unstable', '2',
'See the WAN history on the modem page. Repeated drops with healthy Rx power '
'point toward the provider or the modem rather than the fiber.' + (f' {hint}' if hint else ''),
)
def _collect_wan_quality(self) -> Optional[dict]:
"""Store UniFi's WAN health timeline. If UniFi can't be read, the last copy stays
and goes stale, and the dashboard stops drawing conclusions from it."""
quality = modem_api.wan_quality(self.unifi.get_wan_dashboard(), time.time())
if quality is not None:
db.set_state('wan_quality', quality)
return quality
def _process_wan_degraded(self, quality: Optional[dict], suppressions: list) -> None:
"""Ticket a sustained slowdown: most of the last two hours slow, lossy or down by
UniFi's own measure. It closes, with a recovery note, once two hours are mostly fine."""
periods = (quality or {}).get('periods') or []
if not periods:
return # nothing fresh to judge by: leave any open alert as it is
newest = periods[-1]['ts']
fraction = float((self.cfg.get('modem') or {}).get('degraded_fraction', modem_api.DEGRADED_FRACTION))
bad = modem_api.degradation(periods, newest, fraction)
if bad is None:
if modem_api.recovered(periods, newest):
db.resolve_event('wan_degraded', self.modem_name, '')
return
episodes, lines, hint = self._restart_context(time.time())
parts = [f'{bad[s]} {s}' for s in ('latency', 'loss', 'down') if bad[s]]
description = (f'The internet has been degraded for most of the last 2 hours: {bad["bad"]} of '
f'{bad["total"]} five-minute periods had {", ".join(parts)} (UniFi WAN health).')
try:
raw = db.get_state('modem_checks')
ping = ((json.loads(raw) if isinstance(raw, str) else raw) or {}).get('ping') or {}
except (ValueError, AttributeError):
ping = {}
if ping.get('verdict') == 'gateway':
description += (f' A ping from the modem itself was slow too (avg {ping.get("avg_ms")} ms), '
f'so the modem or the line past it is the slow hop, not the LAN.')
if lines:
description += '\n\nModem restarts in the last 7 days:\n' + '\n'.join(lines)
advice = ('Restart the modem from the WAN / Fiber panel (Restart modem): it silences Gandalf '
'and Uptime Kuma while it reboots.')
if hint:
advice += f' {hint}'
self._modem_event(suppressions, 'wan_degraded', 'warning', '', description,
'Internet degraded (high latency or packet loss)', '2', advice)
_RESTART_SETTLE_S = 180 # the modem answering this soon may not have restarted yet
_RESTART_GIVE_UP_S = 1800
@@ -2366,12 +2435,17 @@ class NetworkMonitor:
with self._wan_batch():
self._process_wan_log(suppressions)
self._collect_modem(suppressions)
self._decide_unstable(suppressions)
except Exception as e:
logger.error(f'Modem collection failed: {e}', exc_info=True)
# Internet quality as UniFi measures it (latency, loss, drops): for the dashboard
# Internet quality as UniFi measures it (latency, loss, drops): for the
# dashboard, and a ticket when it stays bad
try:
self._collect_wan_quality()
quality = self._collect_wan_quality()
if self.modem:
with self._wan_batch():
self._process_wan_degraded(quality, suppressions)
except Exception as e:
logger.error(f'WAN quality collection failed: {e}', exc_info=True)