Architecture Overview
System Overview
MiBee Steward is a device management and monitoring system deployed as a single binary. The Go backend (Chi web framework + SQLite) embeds a SvelteKit 5 SPA via go:embed. This zero-dependency shape means one mibee-steward binary on a Linux box is the entire stack, no runtime, no container, no sidecar.
In a typical deployment a reverse proxy (Nginx) fronts the binary and forwards requests to the Chi router; requests then pass through the middleware chain, handlers, and service layer before landing in SQLite.

Layered Architecture
The backend is layered; data repos (DeviceRepository / DeviceSystemRepository / AuditRepository) live beside their consumers inside the service layer, there is no separate repository package:
Domain Layer (internal/domain): business models, DTOs, constants, context keys for request tracing, typed error definitions.
Service Layer (internal/service): business logic, error handling, the scannerv2 engine, and the repos of the heartbeat/notification/audit subsystems. Constructor-based dependency injection; typed errors. Charter: mutating handlers must go through a service; read-only passthrough handlers may call sqlc directly.
Handler Layer (internal/api): HTTP request/response processing, input validation, audit logging, error-to-status-code translation. routes.go registers ~40 handlers plus the middleware chain.
Request Lifecycle
-
HTTP Request → Chi Router → middleware chain
-
Middleware:
RequestID → RealIP → Logging → Metrics → Recoverer → CORS → SecurityHeaders → CSRF → RateLimit → Auth/RBAC(plus a scope middleware for network-scoped endpoints;agent_authbearer auth for agent endpoints) -
Handler: validation → audit log → service call
-
Service → sqlc → SQLite
-
Response back up the chain
Frontend
SvelteKit 5 SPA embedded via web/embed.go (//go:embed all:dist). Tailwind 4 styling, ECharts for charts, @inlang/paraglide-js for i18n (English + Chinese). File-based routing in web/src/routes/; shared components in web/src/lib/components/.
Database
SQLite via the pure-Go modernc.org/sqlite driver (CGO-free), WAL mode, main pool MaxOpenConns=16 (busy_timeout=5000). Heartbeat results go to a separate SQLite file data/heartbeat.db (single connection, batched writes) so high-frequency probe records never contend with the main DB. sqlc generates type-safe Go from db/queries/*.sql. Default path: ./data/mibee.db. Schema bootstrap is fresh-only: the embedded schema.sql is the single DDL source, applied once when the database is first created and stamped via PRAGMA user_version; a database stamped with a different version is rejected at startup with guidance instead of being upgraded in place. Never edit internal/db/*.go directly, modify SQL and regenerate.
Scanner Engine v2
The scanner is a plugin-based, 5-layer architecture that decouples detection from persistence. Adding a new protocol means registering one classifier + one handler, no orchestrator or persistence changes required.
-
① Probe, active (TCP/SNMP/RTSP/ONVIF/HTTP-metrics) + passive (eBPF TC observer behind the
WITH_EBPFbuild tag) → emits Evidence (port_open / banner / snmp / …). -
② Classifier, per-protocol pure functions over Evidence → fuses into ServiceIdentity (ssh/http/rtsp/onvif/prometheus/node_exporter/snmp/camera) with confidence.
-
③ ServiceHandler, per-service customization:
GenerateHeartbeat()/Collect()/EnrichDevice(). 29 service handlers registered, 8 of them TLS-wrapped cert collectors. -
④ Persistence, Repository interface → SQLite; records evidence / services / device updates / heartbeats.
-
⑤ Orchestrator, declarative gather → classify → dispatch, with cycle-guarded cascade triggers (max depth 5).
Typical cascade: http → probe /metrics → prometheus → node_exporter → parse CPU/mem/kernel → enrich device fields.
Probe Source Taxonomy
| Category | Source | What it needs | What it yields |
|---|---|---|---|
| Active | ICMP ping | network reachability | echo/reply, RTT |
| Active | TCP port scan | target IP + port | port_open, banner text |
| Active | SNMP Get (8 scalar OIDs) | UDP/161, community/credentials | sysDescr, sysObjectID and other system fields (v1/v2c + v3 USM) |
| Active | RTSP OPTIONS | RTSP port (554/8554) | rtsp_banner, Server header |
| Active | ONVIF SOAP probe | TCP unicast (observed ports / 80/8080) | onvif_response, device metadata |
| Active | HTTP probe | HTTP/HTTPS port | Server header, /metrics availability |
| Active | ARP cache | local /proc/net/arp | IP→MAC mapping (same subnet) |
| Active | CDP-MIB | SNMP on Cisco switches | neighbor MAC/IP/port |
| Active | LLDP-MIB | SNMP on LLDP-capable switches | neighbor MAC/port/vlan |
| Active | Q-BRIDGE-MIB | SNMP on managed switches | MAC→port + 802.1Q VLAN tags |
| Active | STP-MIB | SNMP on switches | dot1dTpFdbTable (MAC→port) |
| Passive (host-local) | arp_cache | local host | kernel ARP table |
| Passive (host-local) | multicast | local host | multicast group membership |
| Passive (host-local) | router_arp | local host | router ARP cache |
| Passive (eBPF) | eBPF TCX observer | kernel ≥6.6, WITH_EBPF build tag | ONVIF multicast + TCP magic bytes (confidence 0.6) |
| Router-resident Tier-1 | dhcp_leases | on-gateway only (default off) | DHCP lease file / DHCPACK logs |
| Router-resident Tier-1 | conntrack | on-gateway only (default off) | /proc/net/nf_conntrack active flows |
| Router-resident Tier-1 | hostapd | on-gateway only (default off) | Wi-Fi client associations |
| Router-resident Tier-1 | dns_log | on-gateway only (default off) | DNS query/response log |
Passive sources (eBPF + host-local) are off by default; router-resident Tier-1 sources only run when the binary is deployed directly on the gateway, opt-in and off by default. See Discovery & Identification for details.
Detected Services (out of the box)
SSH, HTTP/HTTPS, RTSP, ONVIF, SNMP, Prometheus, node_exporter, mail (SMTP/POP3/IMAP), remote-access (VNC/RDP/Telnet), directory & file-share (LDAP/SMB), DNS, databases (MySQL/PostgreSQL/Redis/MongoDB/MSSQL/Memcached), TLS-wrapped family (LDAPS, SMTPS, IMAPS, POP3S, FTPS, IRCS, TelnetS), and a host-level camera meta-identity (fused from RTSP + ONVIF evidence, with brand inference from Server headers / SNMP sysDescr / enterprise OID / TLS cert CN).
TLS Certificate Inventory
Any port classified as TLS-speaking (default ports 443/8443/9443/4443 + well-known TLS-wrapped ports 465/636/989/990/992/993/994/995 + classifier-flagged ports) has its full certificate chain collected via probe.CollectCertChain and persisted to host_tls_certs, Subject/Issuer/SAN/validity/signature/key/fingerprint + PEM, one row per cert (leaf + issuers). Surfaced via GET /api/v1/devices/{id}/certificates.
Persistence Tables (v2)
| Table | Contents |
|---|---|
| service_evidence | Raw probe observations (sampling-controlled) |
| host_services | Classified service identities per host |
| host_tls_certs | TLS cert chains per (ip, port), one row per cert; not_after indexed |
| device_neighbors | Raw neighbor facts from the LLDP/CDP/Bridge/Q-BRIDGE probes, the raw-frame LLDP/CDP listeners (WITH_LLDP/WITH_CDP builds, #501), and the WiFi AP's own association table (hostapd/iw, #505) |
| topology_edges | Materialized device↔device topology edges |
| device_sightings | Address-observation history per (device, ip, mac, vantage, source): where a device was seen and by whom (#527/#529) |
| device_network_presence | The multi-homed identity layer: every network an asset is actively present in, at which address (#526) |
| device_interfaces | The device's own IF-MIB + ipAddrTable port census, per-port addresses included (#529) |
Spanning-tree facts (#525): the STP-MIB probe also records each bridge's dot1dStp view into scan_attributes.stp — its own bridge MAC, the network's root bridge, an is_root boolean, and each port's role (root/designated/alternate/backup/disabled). The topology node set exposes stp_root, and the graph's root detection takes that collected fact over every heuristic (see web-ui.md).
| subnets / vlans | Per-network CIDR/gateway, 802.1Q VLANs |
Device upserts go to the existing devices table; heartbeat configs to heartbeat_configs; SNMPv3/SSH credentials (AES-256-GCM encrypted) to snmp_credentials / ssh_credentials; device config-backup versions to device_configs.
Device Identity & Persistence
MAC-Primary Identity
Devices are identified by MAC address first (a roaming device stays one asset across networks), falling back to (ip_address, network_id) when no MAC is known:
- Same private IP on two different LANs → two distinct devices (partitioned by
network_id). - Same MAC, different IP (subnet move) → one asset.
(ip_address, network_id)composite unique index backs this partitioning.
Multi-homed identity & network presence (#526): an asset reported from a network other than its row's home no longer roams the row — while it holds a FRESH presence (24h window) in some other network, the report only upserts that network's presence row (device_network_presence: one per (device, network), the address it answers at there); the device row stays a single graph node anchored to its home. This kills the dual-homed-router ping-pong (both agents' reports used to relocate the row at scan cadence). A genuine mover — every other presence stale — relocates the row, and the roam now carries network_id with it (it used to lag forever, feeding the reconcile drift counter). The topology node set exposes each node's presence networks, so the graph answers "which device interconnects these segments" from collected fact.
Interface census (#529): the IF-MIB + ipAddrTable walk records every port of the device itself into device_interfaces - ifIndex, name, description, MAC, link speed, and the IPv4 each port answers at (the multi-homed detail a single devices.ip_address row can never express). GET /devices/{id}/interfaces feeds the detail-page panel; vanished ports keep their frozen last-seen row (history, not deletion). agent-rs collects the same census.
Route-table edges (#528): the ipRouteTable walk collects every non-direct route (dest CIDR + next hop + egress ifIndex + protocol); routes whose next hop resolves to a known device materialize topology_edges rows (router -> next hop, via route-table, destination subnet in metadata - the more specific subnet wins when routes share a hop). The topology graph unions these edges with the neighbor-derived set, so multi-hop interconnects render as collected fact. Direct routes are skipped: the connected-subnet fact already lives in the interface census and per-network presence.
Sighting history & vantage provenance (#527): every device-bridge apply records a device_sightings row — where (ip/mac/network), via which vantage (empty = center-local, else the reporting agent id), from which source (scan / passive). Same tuple upserts (last_seen + see_count), a changed tuple appends; GET /devices/{id}/sightings?vantage= is the roaming/move archaeology feed. Neighbor rows carry the reporting agent_id too.
MAC-keyed sightings (#522): sources that witness L2 presence without an IP (LLDP/CDP chassis frames, NDP packets) anchor device rows purely by MAC. Such rows carry the #anchor:<mac> placeholder IP — unique by construction (the composite unique index tolerates several per network, same doctrine as the #park: sentinels) and matched by no IP-based lookup — until a scan or ARP sighting fills in the real address; MAC-primary identity then merges the anchored row into the identified one. Identity resolution never treats a no-location report (empty or placeholder IP) as a roam target or a slot take-over, so a chassis re-sighting can never blank a known address.
Local anchor (#522): the scan center's own device rows gain their interface MACs from the OS at wiring time and after every scan (AnchorLocalInterfaces). Self-scan can never learn them — ARP to a local address takes the kernel's loopback shortcut, no wire frame exists — and without the anchor every MAC-keyed neighbor edge (LLDP/CDP frame listeners, hostapd AP→STA edges) was silently dropped for lack of a local device row.
MAC bit flags (observability only): mac_is_locally_administered (U/L bit) and mac_is_multicast (I/G bit) are recorded in scan_attributes as neutral facts. Neither changes device identity.
OUI vendor inference: the OUI loader (internal/service/scannerv2/vendor/) resolves MAC to IEEE-registered vendor via longest-prefix-match across three registries: MA-S (/36) → MA-M (/28) → MA-L (/24). Result stored as scan_attributes.oui_prefix + oui_vendor (NIC silicon vendor), kept separate from vendor (device self-declared brand via SNMP/HTTP/TLS). Out-of-box: embedded curated CC-BY-SA table; full IEEE set optional via scripts/fetch-oui.sh (scanner.oui_path).
Single-Writer Funnel (v0.2.0)
All device writes funnel through runner.applyDeviceBridge, a single-writer concurrency model that prevents race conditions between concurrent probe handlers (the bridge runs sequentially after the parallel scan). It landed together with MAC-primary identity in v0.2.0.
Device Replacement Detection
When a scan discovers a device whose MAC matches an existing record but key attributes differ significantly (e.g., different IP range, different services), the system detects a replacement event: the IP holder wins, the old MAC row is marked offline, and the diff is recorded in change_log.
Network Reconciliation Drift Job
internal/service/scannerv2/reconcile provides the network-reconciliation job (scanner.reconcile_interval, default 1h): it periodically reconciles devices against their network's CIDR membership but only detects and surfaces drift (the mibee_network_mismatches gauge), it never auto-modifies device records.
Change Detection Engine
The center diffs each scan against known device state and emits events to change_log:
| Event | Trigger |
|---|---|
| device_added | New device appears in scan |
| device_changed | Tracked field differs (type, brand, MAC, ports, services, scan_attributes). Field-by-field comparison with normalized scan_attributes (volatile keys stripped, key order canonicalized). after_data = full post-change snapshot. |
| device_lost | Absent from lostThreshold (2) consecutive scans. Grace period prevents single-scan jitter. |
| device_recovered | A previously-lost device reappears. Shares the liveness cooldown bucket with device_lost (anti-flap). |
| device_config_changed | The config-backup sweep fetched a running-config whose hash differs from the last version (see below). |
Events: GET /api/v1/changes, SSE GET /api/v1/changes/watch, Prometheus counter mibee_changes_total{type}.
Liveness noise control: online/offline verdicts are sampled into the device_liveness time series (in the heartbeat store) instead of firing device_changed per flip, the registry stays a living ledger without burying real changes.
Device Config Backup
internal/service/scannerv2/configbackup runs an opt-in sweep (scanner.config_backup, default 6h): it selects router/switch/firewall devices with a bound SSH credential, fetches the running-config over SSH (vendor command matrix; host-key TOFU), computes a unified diff against the last version (internal/configdiff), and records a new device_configs version only on change, emitting device_config_changed into the change-detection pipeline above. SSH credentials live in ssh_credentials, encrypted with the same AES-256-GCM master-key cipher as SNMPv3 passphrases.
Synthetic Probing (probe targets)
internal/service/probetarget probes explicitly configured EXTERNAL endpoints (public HTTPS sites, hosted TLS ports) on per-target intervals (10s tick re-reads targets, CRUD applies without restart; next-due resumes from last_run_at; concurrency bounded by probe.max_concurrency, default 8 — previously a hard-coded 8). Modules http/tcp/icmp reuse the heartbeat probers; tls (and https) call CollectCertChain, so the internal cert-chain inventory extends to internet hosts. Tables: probe_targets / probe_results / probe_tls_certs; metrics mibee_probe_*.
Three subsystems grew around that engine (#603–#610):
- Per-module options executor (#605): a target may carry an options JSON block. Option-less targets stay on the shared legacy probers (bit-for-bit unchanged semantics); option-carrying http/tcp targets run the option-aware executors (
executor_options.go: method, exact expected status codes, 1 MiB-bounded body keyword match contains/not_contains/regex,ignore_tls_errorsopt-out; tcp send payload + banner expectation), and the newdnsmodule (executor_dns.go: A/AAAA/CNAME/TXT, pinned resolverhost[:port]via a PreferGo UDP resolver,expected_ipsintersection on A/AAAA). Options are validated per module at CRUD time (fields foreign to the module are rejected, not silently ignored) and deliberately hold no credentials — they ride the secret-free agent command channel verbatim. - Capability-filtered dispatcher (#604): the vantage dispatcher filters each agent's plan by its declared
supported_modulescapability (reported in report meta →agent_status.supported_modules). Legacy agents (≤0.1.x, no declaration) are assumed{icmp, http, tls}and held back from tcp/dns plans with a one-time upgrade hint — pre-0.2.0 agents shipped tcp plans but failed them with "unknown module" (a live bug), so the dispatcher shields them until they upgrade. - Egress quality (#608):
QualityServiceseeds a system matrix of public probe points fromprobe.qualityconfig (rows withsource=system, vantageall; disabled = parked, config edits refresh rows, a config entry removed deletes the row with its history, a UI-deleted row reseeds on restart) and analyzes them read-time over a sliding window: per-vantage ok-rate and p50 aggregation, cross-vantage target isolation (failing everywhere blames the target, excluded from network verdicts; needs >1 reporting vantage), then the deterministic verdict chainegress_down > dns_fault > upstream_resolver > degraded > ok. The DNS differential (two public resolvers), the DNS-free direct-IP TCP leg, 204 endpoints, and the international reference group (excluded from the primary verdict — cross-border variance is its own signal, not an egress fault) are all config, nothing hardcoded. Served atGET /api/v1/probe-quality; a 1-minute loop refreshes themibee_probe_egress_verdict/mibee_probe_quality_scoregauges (metrics only, no notification wiring by design). - Autogen sweep (#606):
AutogenService(startup + everyprobe.autogen.sweep_interval_minutes, default 15) reconciles each device's detected_services identities into device-bound targets (source=auto): http/https services get http targets, other TCP services plain port probes, vantage preferring the device's own network agent. A service that left the scan parks its target (kept for history); a reappearance revives it; manual targets are never touched; the population is capped bymax_targets(default 100). Default OFF.
Schema generation 8 (#609) carries the storage side: probe_targets gains options (JSON), device_id (optional device binding; deletion unbinds — auto targets are disabled, manual/system keep running), source (manual|auto|system) and the dns module CHECK; agent_status gains supported_modules. As always the schema is fresh-only — crossing from an older generation is backup → new DB → rescan.
Heartbeat & State
HeartbeatService runs in a background goroutine with a 30-second ticker:
| Config Key | Default | Description |
|---|---|---|
| heartbeat.default_interval | 30 | Device check interval (seconds) |
| heartbeat.timeout | 5 | Probe timeout (seconds) |
| heartbeat.tick_interval_seconds | 30 | Probe loop tick (seconds) |
| heartbeat.offline_threshold | 5 | Consecutive failures before offline |
| heartbeat.offline_backoff_ticks | 10 | Probe offline devices once every N ticks (~5 min on 30s ticker) instead of every tick. Stops steady timeout-row writes for known-dead hosts. A reviving scan clears failure count immediately. 0 disables backoff. |
Service lifecycle: NewRouter() → HeartbeatService.Start() → goroutine → signal wait → graceful shutdown.
Distributed Model
MiBee Steward supports a center + agent deployment: the center (cmd/server) aggregates device data from multiple agents (the Rust agent-rs/ workspace), each on a different LAN segment. Deep dive → Distributed Deployment, implementation report → agent-rs.md.
| Role | Binary | Responsibilities |
|---|---|---|
| Center | cmd/server | Aggregation hub: API, SPA, device registry, change detection, heartbeat, ingestion, agent management |
| Agent | agent-rs/ (Rust) | Lightweight scanner: runs the same scannerv2 discovery engine logic locally (router-form reports passive discoveries), reports upstream, polls for commands. The Go agent (cmd/agent) was retired 2026-10-09 — comparison in agent-rs.md |
Pull model, agent initiates all connections (NAT-friendly):
-
Report (
POST /api/v1/agents/report): batch HostReports, MAC-primary merge. -
Command poll (
GET /api/v1/agents/commands, the token is the identity): every 60s; ack/complete are separate calls. -
Disconnect recovery: failed batches held in memory (bounded at 100), flushed oldest-first.
Anti-entropy: agent reports carry X-Network-State-Hash (SHA-256 of the alive-set identity/classification fields); the center uses it for drift detection. A matching hash skips the device bridge (lease refresh only) — with one heal rider (#612): a report host carrying services while its center row's service columns are empty is re-bridged right there, because a center-side wipe (historically a passive empty-field report) could otherwise never re-land while the hash says "unchanged". Passive batches (origin: "passive", the agent's discovery sweeps between scans) assert liveness/identity only: the bridge preserves open_ports/detected_services/prometheus URLs instead of overwriting them with the empty observation — an active scan that genuinely observed no services still clears stale ones. A slow background sweep also backfills host_services.device_uuid for rows written before their device row existed. Lease TTL: agent tokens are bound to network_id + agent_id; lease-TTL lost detection (default 5m), revocation is soft-delete (revoked_at).
Observability
Metrics: /metrics, standard Prometheus endpoint. Counter, Gauge, Histogram for system metrics.
Service Discovery: /sd, HTTP SD endpoint for Prometheus scrape config and device-systems auto-discovery (metrics_enabled=true).
Dashboard Proxy: /api/v1/dashboard/query, read-only proxy to Prometheus.
Retention Sweepers (internal/service/scannerv2/cleanup/): periodic pruning of high-volume detail tables. Batch deletes (default 5000 rows) to avoid holding the SQLite write lock. Runs on startup + every sweep_interval_hours (default 6).
| Table | Default Retention |
|---|---|
| heartbeat_results | 7 days |
| scan_results | 30 days |
| service_evidence | 14 days |
| host_services | 30 days |
| host_tls_certs | 30 days |
| change_log | 30 days |
| device_neighbors | 90 days |
| device_liveness | 7 days |
| notification_log | 30 days |
| scan_task_runs | 30 days |
| audit_logs | 90 days |
An asset-archive engine (internal/service/archive/, #663) replaces the old silent-device physical prune: a scanner-discovered device offline past the threshold (per-device override > global archive.offline_days, default 7d; no-MAC devices take a 24h fast lane) moves to the archive — archived_at stamped, full history preserved, every transition written to asset_archive_events. An archived device continuously online past archive.restore_online_minutes (default 60) auto-restores; brief blips only record online_blip events. Pins (force_active/force_archive) override everything, and the periodic-pattern learner (#664) exempts devices that keep returning on a stable cycle. The only remaining device-row delete is the roamed-orphan duplicate reap — a duplicate is not an asset, archiving it would create a ghost.
Background Tasks
Resident goroutines started by NewRouter() (stopped in dependency order on graceful shutdown):
| Task | Cadence | Duty |
|---|---|---|
| Heartbeat probing loop | 30s tick | Poll due devices, write verdicts |
| Token blacklist cleanup | periodic | Evict expired JWT blacklist entries |
| v2 scan scheduler | cron | Fire scan_tasks + stale-run sweeper |
| Lease sweeper | 60s | Agent-network lease expiry → device_lost |
| Retention sweeper | 6h | Batched pruning of the table above |
| Config-backup sweep | 6h (config) | SSH running-config pulls → device_configs versions |
| Probe-target engine | 10s tick | Due synthetic probes → probe_results |
| Probe dispatch tick | 10s tick | Capability-filtered vantage plans → agent command channel (fingerprint-deduplicated) |
| Egress-quality metrics loop | 1 min | Window analysis → mibee_probe_egress_verdict / mibee_probe_quality_score gauges |
| Probe autogen sweep | 15 min (config) | Discovered services → device-bound source=auto targets (park/revive lifecycle) |
| Notification rule engine | event-driven | Change events → matched rules → dispatch queue |
| Notification dispatcher | 3 workers | Rule engine → channel (email/webhook/…) dispatch |
| Device metrics refresher | periodic | Device gauges for /metrics |
| Change Watcher | event-driven | change_log → SSE push |
Security Model
-
JWT auth: cookie + Bearer dual mode.
auth.jwt_secretmust be changed in production (≥32 chars, enforced at startup).auth.token_expirydefault 24h. -
TOTP 2FA (optional):
/api/v1/auth/2fa/{verify,setup,enable,disable,status}. -
RBAC: capability-based model (admin / operator / viewer tiers,
userbeing a legacy alias for viewer), enforced in the middleware chain. -
Network scope authorization (network grants): per-user network scope isolation via
internal/authz(scopeql + scoperesolver), restricting users to authorized networks. -
Secret storage: SNMPv3 USM passphrases and SSH credentials are AES-256-GCM encrypted in SQLite under
security.master_key(32 bytes, held in memory only; optional until the first credential exists). -
CSRF: cross-site request forgery protection middleware.
-
Agent tokens: SHA-256-hashed opaque bearer tokens in
agent_tokens. Plaintext returned once at creation; only hash persisted. Bound tonetwork_id+agent_id. -
Audit log: request-level audit trail via middleware.
-
Security headers: set by
SecurityHeadersmiddleware.