|
|
||
|---|---|---|
| .. | ||
| src/wfagent | ||
| tests | ||
| README.md | ||
| pyproject.toml | ||
| wfagent.example.yaml | ||
| wpa_cli_commands.md | ||
README.md
wfagent
Wi-Fi event/command bridge for wpa_supplicant with a separate delivery backend.
This repository contains two installable Python applications:
wfagent, the agent that reads events fromwpa_supplicant, processes them, writes selected events tostdoutas NDJSON, and can trigger commands.wfagent_backend, the HTTP backend that receives webhook deliveries from agents, normalizes them, queues them, and maps payload fields to downstream metrics.
The two applications are intentionally decoupled and can be deployed on different hosts.
Code Navigation
Main entry points
src/wfagent/cli.py- CLI entry point and config bootstrapsrc/wfagent/__main__.py-python -m wfagententry pointsrc/wfagent/config.py- YAML config parsing and validation
Event pipeline
src/wfagent/sources.py- D-Bus,ctrl_iface, and runtime sourcessrc/wfagent/pipeline.py- filters, normalizer, classifier, serializer, triggers, runtime orchestrationsrc/wfagent/models.py- source/event/command modelssrc/wfagent/services.py- clock, correlation IDs, observability
Runtime behavior
ctrl_ifacesource loss is handled as a recoverable condition.source_downandsource_upare internal runtime events and pass through the same pipeline asstart,reload,stop, andtimer_tick.- The agent serializes selected events to stdout as NDJSON only after classification.
- Command execution and command result data remain separate.
Payload decoding and delivery
src/wfagent/payloads/- blob and field-value decodingsrc/wfagent/delivery/- webhook delivery, queue/spool, and retrysrc/wfagent/commands.py- D-Bus andctrl_ifacecommand clientssrc/wfagent/enrichment.py- optional navigation snapshot enrichment servicesrc/wfagent/lifecycle.py- runtime lifecycle handling
What to look at first
sources.pyfor transport-specific source behavior and reconnect handling.pipeline.pyfor filtering, classification, and trigger decisions.payloads/decoders/for SSID/BSSID, TLV, WPS, and WFD decoding.delivery/for backend delivery batching and retry logic.enrichment.pyfor the optional navigation snapshot service and cache.commands.pyfor explicit D-Bus andctrl_ifacecommand execution.
Tests
tests/test_payloads.py- blob decoder behaviortests/test_pipeline.py- pipeline, normalization, and runtime orchestrationtests/test_delivery.py- delivery batching and backend handofftests/test_commands.py- command client behaviortests/test_config.py- config loading and validation
System Overview
The agent is the event producer and command orchestrator. It consumes:
- D-Bus events from the
wpa_supplicanttree ctrl_ifaceunsolicited lines from the Unix socket- internal runtime events such as
start,reload,stop,timer_tick,source_down, andsource_up
The ctrl_iface source is treated as recoverable: if the Unix socket disappears, the agent emits source_down, retries, and emits source_up after recovery.
The agent then:
- applies source-specific filters
- normalizes the event into the internal model and, when configured, attaches navigation snapshot data under
payload.navigation - classifies it as
drop,serialize_only, orserialize_and_trigger - serializes selected events to
stdoutas NDJSON - creates command requests for trigger rules
- dispatches commands to an explicitly configured destination
- optionally delivers normalized event batches to the backend over HTTP
The serialized event model keeps event_kind and data_kind separate:
event_kindis the event classification used by filters, classifier rules, and triggers.data_kindis the payload/content classification used by downstream delivery and mapping.
Enrichment layer
src/wfagent/enrichment.py implements the optional navigation enrichment layer.
- It is a background snapshot service, not a Wi-Fi event source.
- It does not emit pipeline events and does not participate in classifier or trigger matching.
- It periodically refreshes navigation status and coordinates through a configured API client.
- It caches the last known snapshot with TTL and exposes
NavigationSnapshotdata to the normalizer. - When a fresh snapshot is available, the normalizer adds it under
payload.navigation.
The current implementation includes:
EnrichmentService- lifecycle and refresh loopNavigationCache- cached snapshot state and TTL handlingNavigationApiClientStub- default stub client used when no live API client is injectedNavigationSnapshot- serialized status/coordinate view exposed to the event payload
NavigationSnapshot.to_payload() produces one of these compact payloads:
{"status":"available","age_ms":...,"coordinates":{"x":...,"y":...}}{"status":"unavailable","reason":...}
Enrichment is payload-only. It does not change event_kind, data_kind, or routing decisions.
The backend is the delivery and persistence side. It:
- accepts webhook requests on
POST /api/v1/ingest - expects canonical
items[]payloads only - normalizes requests into internal delivery objects
- enqueues items for retry and batching
- maps selected payload fields to downstream metric definitions
- sends data through
zabbix_sender
Architecture
Agent data flow
DBus Source -----------\
\
Runtime Source ---------> Source Filters -> Normalizer <--- Enrichment Snapshot Service
/ | |
CtrlIface Source ------/ | +-> payload.navigation
v
Classifier -> NDJSON Serializer -> stdout
|
+--> Trigger Engine -> Command Dispatcher
|
+--> Delivery Service -> Backend
Backend data flow
HTTP ingest -> Request normalization -> Queue -> Mapper -> Sender
| |
| +-> zabbix_sender
+-> NDJSON response/log output
Backend stdout logging is batch-oriented:
- one log record per accepted request batch
- top-level
clientmetadata is added by the backend - accepted items live under
batch.items[]
Repository Structure
Agent package
Located in src/wfagent:
cli.py- agent CLI entry pointconfig.py- YAML config loader and validatorsmodels.py- event, command, and delivery modelssources.py- D-Bus,ctrl_iface, and runtime sourcespipeline.py- filtering, normalization, classification, serialization, triggeringenrichment.py- optional navigation snapshot enrichment servicecommands.py- command dispatchingdelivery/- webhook delivery, queue/spool, and backend integrationlifecycle.py- runtime lifecycle handlingservices.py- clock, correlation IDs, observabilitypayloads/- payload decoding helpers
Backend package
Located in ../backend/src/wfagent_backend:
cli.py- backend CLI entry pointconfig.py- backend config loader and validatorsapp.py- request normalization and ingest handlingserver.py- HTTP server and routingqueue.py- in-memory queueing and retry coordinationmapping.py- metric mapping logicsender.py-zabbix_senderadaptercontracts.py- ingest and delivery data models
Agent Principles
The agent is designed around a single unified event pipeline:
- sources are transport-specific and lightweight
- source-specific filters run before normalization
- runtime events are first-class inputs
- optional enrichment services can add additive payload context without changing classifier or trigger logic
- classification decides whether an event is dropped, serialized, or also used for triggering
- stdout is the only serialization target for events in the agent
- command execution is separated from command result data
- every command request targets an explicit destination without fallback routing
Supported event sources
- D-Bus:
fi.w1.wpa_supplicant1fi.w1.wpa_supplicant1.Interface- signals such as
InterfaceAdded,InterfaceRemoved, andPropertiesChanged
ctrl_iface:- commands such as
STATUS,SCAN,SCAN_RESULTS,BSS,REASSOCIATE,RECONNECT, andTERMINATE - unsolicited events after
ATTACH, for exampleCTRL-EVENT-SCAN-RESULTS
- commands such as
- runtime:
startreloadstoptimer_ticksource_downsource_up
Backend Principles
The backend is a separate HTTP service with a narrower job:
- accept deliveries from agents over HTTP
- validate and normalize canonical payload shapes
- keep deliveries in memory with retry and TTL policies
- map normalized payload fields to metric definitions
- send metrics via
zabbix_sender - emit structured NDJSON logging if enabled
The backend does not talk to wpa_supplicant directly.
Configuration
Both applications use versioned YAML configuration.
Agent configuration
The agent config is documented in wfagent.example.yaml. The main sections are:
runtime- runtime-generated events and timer intervalnormalization- payload parsing behaviorenrichment- optional value-added navigation context refresh and cachingdelivery- webhook delivery settings for the backendsources- D-Bus,ctrl_iface, and runtime source settingsclassifier- event classification rulestriggers- command trigger rulescommands- explicit destination command definitionsoutput- NDJSON serialization targetobservability- logging and metrics behaviorguards- trigger loop and deduplication protection
Key deployment fields:
delivery.agent_id- stable logical deployment namedelivery.agent_instance_id- unique host/interface identitydelivery.webhook.url- backend ingest URLsources.ctrl_iface.socket_path-wpa_supplicantcontrol socketsources.dbus.service- D-Bus service name
Backend configuration
The backend config and ingest contract are documented in ../backend/README.md. Use ../backend/wfagent_backend.example.yaml as the concrete backend example.
Example agent config
The provided wfagent.example.yaml is a practical starting point for a single-host deployment. It enables:
- D-Bus source
ctrl_ifacesource- runtime source
- webhook delivery to the backend
- optional navigation-context enrichment is disabled by default and can be enabled under
enrichment.navigation
Install
From the repository root:
cd agent
python -m pip install -e .
Install the backend from the backend project directory:
cd backend
python -m pip install -e .
Run
Agent
cd agent
wfagent --config wfagent.example.yaml
or:
cd agent
python -m wfagent --config wfagent.example.yaml
Backend
cd backend
wfagent_backend --config wfagent_backend.example.yaml
Optional overrides:
wfagent_backend --config wfagent_backend.example.yaml --host 127.0.0.1 --port 8080
Deployment
Typical split deployment
- Run
wfagentfromagent/on the host that has access towpa_supplicant. - Run
wfagent_backendfrombackend/on the destination host or on the same host. - Point
delivery.webhook.urlin the agent config at the backend ingest endpoint. - Keep
delivery.agent_idstable across a logical fleet. - Set
delivery.agent_instance_idper host or per wireless interface.
Agent deployment notes
- Install the agent into a virtual environment.
- Make sure the user running the agent can access the D-Bus system bus and
wpa_supplicantcontrol socket. - Prefer a dedicated
wpa_supplicantinterface socket path per interface, for example/var/run/wpa_supplicant/wlp3s0. - Use
timer_tick_interval_msconservatively if triggers can cause command loops.
Backend deployment notes
- Bind the backend to localhost if the agent runs on the same host.
- Put the backend behind a reverse proxy or private network if it is exposed remotely.
- Configure
zabbix_senderaccess and credentials at the host level, not in the agent. - Increase
queue.max_itemsandqueue.item_ttl_msonly if you expect backpressure.
Data Format
NDJSON output from the agent
Each serialized event is written as one JSON object per line.
Example:
{"event_id":"e-1001","source_type":"runtime","source_id":"runtime","event_kind":"start","observed_at_unix_ms":1760000000000,"severity":"info","payload":{"payload":{"state":"start"}}}
Backend ingest contract
The backend accepts canonical batches with items[] and a canonical target object:
target.kindtarget.value
Metric mapping rules can match nested fields with dotted paths such as target.kind and target.value.
The backend adds client itself. It is not expected from the agent payload.
Backend ingest example
Canonical payload:
{
"batch_id": "del-1",
"agent_id": "wfagent",
"agent_instance_id": "wfagent@wlp3s0",
"sent_at_unix_ms": 1760000000000,
"items": [
{
"item_id": "evt-1",
"item_kind": "event",
"source_type": "dbus",
"source_id": "fi.w1.wpa_supplicant1",
"event_kind": "properties_changed",
"observed_at_unix_ms": 1760000000000,
"target": {
"kind": "bss",
"value": "/fi/w1/wpa_supplicant1/Interfaces/0/BSSs/0"
},
"data_kind": "properties_changed",
"payload": {
"changed": {
"Signal": -41,
"Age": 0
}
}
}
]
}
Examples
Agent example
The agent directory includes wfagent.example.yaml, which demonstrates:
- D-Bus source configuration
ctrl_ifacesocket configuration- runtime event generation
- classifier rules
- trigger rules
- webhook delivery to the backend
Backend example
The backend directory includes ../backend/wfagent_backend.example.yaml, which demonstrates:
- HTTP server binding
- ingest path and health path
- queue and retry configuration
- Zabbix sender settings
- metric mapping from BSS event payloads
Testing
Run the full test suite:
pytest
The default suite is self-contained and does not require a live wpa_supplicant.
Integration tests
The repository also includes live smoke tests for wpa_supplicant integrations.
- D-Bus tests use the
WFAGENT_DBUS_IFNAMEenvironment variable. ctrl_ifacetests use theWFAGENT_CTRL_IFACE_SOCKETorWPA_CTRL_IFACE_PATH.
Run them with:
pytest -m integration