Modem access code: NAT sessions, and a ping from the modem when slow
Lint / JS (eslint) (push) Successful in 1m11s
Lint / Python (flake8) (push) Successful in 1m55s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 54s
Lint / Python (flake8) (pull_request) Successful in 53s
Test / Python Tests (pytest) (push) Successful in 1m5s
Lint / JS (eslint) (pull_request) Successful in 11s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 55s
Test / Python Tests (pytest) (pull_request) Successful in 1m3s

With modem.access_code in config.json, the monitor logs in to the
BGW320 (md5 of code + nonce, as its own page does) for two read-only
checks, stored in monitor_state.modem_checks:

- NAT sessions in use every 30 minutes; Needs attention warns at 80%.
- While UniFi says the internet is slow, a ping from the modem itself
  to 1.1.1.1 (at most every 10 minutes). Compared with UniFi's latency
  through the modem, it says whether the delay is past the modem (the
  provider), in the modem, or gone. The Internet line in Needs
  attention and the WAN / Fiber panel show the result.

A refused code is reported and not retried for 30 minutes; other
failures pause the checks for 10. The unauthenticated status polling
is unchanged. Tested against the real gateway and a fake one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-19 18:32:31 -05:00
1 parent 2f4cb05f67
commit de75f71bf8
10 files changed
+645 -8

No files matched your search

+43 -4
View File
@@ -115,7 +115,42 @@ def _ago(seconds: float) -> str:
return f'{minutes} min ago' if minutes < 60 else f'{round(minutes / 60)} h ago'
def _wan(quality: Optional[dict], now: float) -> List[dict]:
_NAT_FULL = 0.8 # share of the gateway's NAT sessions in use before it's worth a line
def _modem_ping_note(checks: Optional[dict], now: float) -> str:
"""One sentence on the latest ping from the modem itself, if it ran in the last hour."""
ping = (checks or {}).get('ping') or {}
if not ping or now - (ping.get('at') or 0) > _WAN_WINDOW_S:
return ''
when, avg, ours = _ago(now - ping['at']), ping.get('avg_ms'), ping.get('ours_ms')
theirs = (f'{round(avg)} ms' if avg is not None else 'no reply') + (
f', {ping["loss_pct"]}% lost' if ping.get('loss_pct') else '')
ours_txt = f'; ours through it {round(ours)} ms' if ours is not None else ''
verdict = {
'upstream': "so the delay is past the modem, on the provider's side",
'gateway': 'so the delay is in the modem, or between it and the router',
'clear': 'both fine by then',
}.get(ping.get('verdict'), '')
return f"Modem's own ping {when}: {theirs}{ours_txt}, {verdict}. "
def _modem_checks(checks: Optional[dict]) -> List[dict]:
out = []
nat = (checks or {}).get('nat') or {}
if nat.get('available') and nat.get('in_use', 0) / nat['available'] >= _NAT_FULL:
pct = round(100 * nat['in_use'] / nat['available'])
out.append(_entry('broken', 'warn', f'Modem NAT table {pct}% full',
'#modem-section', f'{nat["in_use"]:,} of {nat["available"]:,} sessions in use. '
'When it fills, new connections stall or fail.'))
error = (checks or {}).get('error') or {}
if error.get('login'):
out.append(_entry('tidy', 'warn', 'Modem login failed: check modem.access_code in config.json',
'#modem-section', 'Gandalf waits 30 minutes before trying again.'))
return out
def _wan(quality: Optional[dict], now: float, checks: Optional[dict] = None) -> List[dict]:
if not quality or now - (quality.get('updated') or 0) > _WAN_STALE_S:
return []
out = []
@@ -133,6 +168,7 @@ def _wan(quality: Optional[dict], now: float) -> List[dict]:
if day_bad else '')
if latencies:
detail += 'Now: ' + ', '.join(f'{k} {v} ms' for k, v in sorted(latencies.items())) + '. '
detail += _modem_ping_note(checks, now)
detail += 'See the 24 hour strip on the WAN / Fiber panel.'
lossy = recent['loss'] + recent['down']
if lossy >= _WAN_LOSS_PERIODS:
@@ -154,16 +190,19 @@ def _wan(quality: Optional[dict], now: float) -> List[dict]:
def build(events: List[dict], power: List[dict], cutsheet_view: Optional[dict],
rack_issues: Optional[Dict[str, dict]] = None, wan_quality: Optional[dict] = None,
now: Optional[float] = None) -> dict:
now: Optional[float] = None, modem_checks: Optional[dict] = None) -> dict:
"""{'broken': [entry], 'tidy': [entry]}, worst first within each group.
events: active alerts, annotated with 'is_suppressed'. power: topology.build_power output.
cutsheet_view: the cutsheet API view (cables with 'problem', suggestions, port_names,
labels), or None when the cutsheet couldn't be loaded. rack_issues: {rack: rack_layout()}.
wan_quality: modem.wan_quality() output as stored by the monitor. now: for tests.
wan_quality: modem.wan_quality() output as stored by the monitor. modem_checks: the
monitor's logged-in modem checks (NAT sessions, ping from the modem). now: for tests.
Entry: {group, severity: bad|warn|info, text, href, detail}.
"""
entries = _wan(wan_quality, time.time() if now is None else now) + _alerts(events) + _power(power)
now = time.time() if now is None else now
entries = (_wan(wan_quality, now, modem_checks) + _modem_checks(modem_checks)
+ _alerts(events) + _power(power))
if cutsheet_view is not None:
entries += _cutsheet(cutsheet_view)
entries += _rack(rack_issues or {})