network-monitor/app/hub/db.py
neil be727949c1 Probes log locally only while the hub is unreachable; the hub confirms silences with that log
Probe: no access log, nothing written while reports get through. When the hub
can't be reached, failed reports go to data/outage.jsonl and unsent results to
data/backlog.jsonl (survives restarts). The first report that gets through
uploads both and they are deleted.

Hub: a probe that misses reports gets a "Not reporting" incident. When it
reports again, the runs from its outage log become confirmed "Hub unreachable"
incidents; the rest of the silence (probe off or restarting) is dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 20:50:30 -08:00

96 lines
2.8 KiB
Python

"""SQLite storage under $DATA_DIR. One short-lived connection per unit of work."""
import os
import sqlite3
from contextlib import contextmanager
from pathlib import Path
SCHEMA = """
CREATE TABLE IF NOT EXISTS probes (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
token TEXT NOT NULL UNIQUE,
address TEXT, -- set by the hub from the report, or ADVERTISE_ADDR, or by hand
address_locked INTEGER NOT NULL DEFAULT 0,
port INTEGER,
hostname TEXT,
version TEXT,
icmp INTEGER, -- 1/0: whether ICMP works on that probe
created REAL NOT NULL,
last_seen REAL,
last_error TEXT
);
CREATE TABLE IF NOT EXISTS measurements (
ts REAL NOT NULL,
probe_id INTEGER NOT NULL,
target TEXT NOT NULL, -- probe:<id>, gateway, dns:<ip>, vps, web:<url>
label TEXT,
kind TEXT NOT NULL, -- lan | wan
method TEXT NOT NULL, -- icmp | http | dns | throughput
ok INTEGER NOT NULL,
latency REAL, -- ms
loss REAL, -- 0..1 (icmp)
mbps REAL, -- throughput
error TEXT
);
CREATE INDEX IF NOT EXISTS m_ts ON measurements(ts);
CREATE INDEX IF NOT EXISTS m_series ON measurements(probe_id, target, method, ts);
CREATE TABLE IF NOT EXISTS devices (
key TEXT PRIMARY KEY, -- MAC when known, else ip:<address>
mac TEXT,
ip TEXT,
hostname TEXT,
sources TEXT NOT NULL DEFAULT '',
first_seen REAL NOT NULL,
last_seen REAL, -- last activity (DNS query or ARP); NULL = only listed somewhere
name TEXT,
category TEXT,
suspicious INTEGER NOT NULL DEFAULT 0,
notes TEXT
);
CREATE INDEX IF NOT EXISTS d_ip ON devices(ip);
CREATE TABLE IF NOT EXISTS probe_outages ( -- see outages.py
id INTEGER PRIMARY KEY,
probe_id INTEGER NOT NULL,
start_ts REAL NOT NULL,
end_ts REAL, -- NULL while the probe is still silent
confirmed INTEGER NOT NULL DEFAULT 0, -- 1 = backed by the probe's outage log
failures INTEGER, -- failed reports the probe logged
error TEXT
);
CREATE INDEX IF NOT EXISTS po_probe ON probe_outages(probe_id, end_ts);
CREATE TABLE IF NOT EXISTS ip_mac (
ip TEXT PRIMARY KEY,
mac TEXT NOT NULL,
last_seen REAL NOT NULL
);
"""
def path() -> Path:
return Path(os.environ.get("DATA_DIR", "data")) / "hub.sqlite3"
def connect() -> sqlite3.Connection:
con = sqlite3.connect(path(), timeout=15)
con.row_factory = sqlite3.Row
con.execute("PRAGMA journal_mode=WAL")
con.execute("PRAGMA synchronous=NORMAL")
return con
@contextmanager
def db():
"""A connection whose work commits on success and rolls back on error."""
con = connect()
try:
with con:
yield con
finally:
con.close()
def init() -> None:
path().parent.mkdir(parents=True, exist_ok=True)
with db() as con:
con.executescript(SCHEMA)