Gatherer: aggregate several sessions of one user under the account
Session key uses the OpenVPN Client ID (survives float), one usage_history row per user per tick, summed speeds in the API, last_bytes/real_address per account, migration 2 (active_sessions.session_id). Also throttles the "status file missing" log and reports recovery. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ad6995bb63
commit
7d6f44c965
11 files changed
+287
-57
No files matched your search
@@ -136,6 +136,11 @@ Returns a snapshot of all known clients.
|
||||
|
||||
### `GET /stats`
|
||||
|
||||
Field notes (a user with several simultaneous sessions is one record):
|
||||
- `current_recv_rate_mbps` / `current_sent_rate_mbps`: peak over the last 30 seconds of the **sum of the rates of all sessions** of the user at one tick (not the rate of a single session).
|
||||
- `real_address`: the address of the session with the latest start. `last_bytes_received/sent` (table `clients`): the sum of the current counters of all active sessions. Per-session values are in `GET /sessions`.
|
||||
- In `GET /stats/<common_name>` the history of every interval has the volume and the rate summed over the user's sessions (rate: `MAX` over the interval of the per-tick sum).
|
||||
|
||||
#### Example Response
|
||||
```json
|
||||
{
|
||||
|
||||
@@ -8,11 +8,11 @@ The gatherer runs a continuous loop (default interval: **10 seconds**).
|
||||
|
||||
### A. Log Parsing
|
||||
- **Source**: `/var/log/openvpn/openvpn-status.log` (Status File v2, CSV format).
|
||||
- **Target Fields**: `Common Name`, `Real Address`, `Bytes Sent`, `Bytes Received`, `Connected Since (time_t)`.
|
||||
- **Target Fields**: `Common Name`, `Real Address`, `Bytes Sent`, `Bytes Received`, `Connected Since (time_t)`, `Client ID` (the column index is taken from the `HEADER,CLIENT_LIST,...` line; a missing column or a non-numeric value means no session id).
|
||||
- **Filtering**: Only lines starting with `CLIENT_LIST`.
|
||||
|
||||
### B. Delta Calculation (`update_client_status_and_bytes`)
|
||||
The log provides *lifetime* counters for a session. The gatherer calculates the traffic *delta* (increment) since the last check. A session is identified by the key `(common_name, real_address, connected_since_ts)`, where `connected_since_ts` is the `Connected Since (time_t)` column (index 8) of `CLIENT_LIST`. A reconnect always gets a new `connected_since_ts` and therefore a new session.
|
||||
The log provides *lifetime* counters for a session. The gatherer calculates the traffic *delta* (increment) since the last check. A session is identified by the key `(common_name, session_id or real_address, connected_since_ts)`, where `session_id` is the `Client ID` of the status file (it does not change when the client address changes without a reconnect, `float`) and `connected_since_ts` is the `Connected Since (time_t)` column (index 8) of `CLIENT_LIST`. If the status has no `Client ID` (old OpenVPN), `real_address` is used instead. A reconnect always gets a new `connected_since_ts` and therefore a new session. `active_sessions.session_id` (TEXT, nullable, migration 2) stores the id; old snapshot rows without it match by the fallback key.
|
||||
|
||||
| Situation | Increment |
|
||||
|-----------|-----------|
|
||||
@@ -22,10 +22,12 @@ The log provides *lifetime* counters for a session. The gatherer calculates the
|
||||
| Not in cache, session older than the daemon and no snapshot (first start after an update, lost DB) | Baseline: increment `0`, a WARNING with the client name is logged (the only case of lost traffic, once per session). |
|
||||
| `connected_since_ts` is missing or not a number (non-standard status file) | Fallback heuristic: counters below 10 MB are counted as a new session, otherwise increment `0`; a WARNING is logged. |
|
||||
|
||||
**Several sessions of one user** (`duplicate-cn`): the increments of all sessions of a common name are added up and belong to one `clients` row. In `clients`, `real_address` is the address of the session with the latest start and `last_bytes_received/sent` is the **sum** of the current counters of all active sessions; per-session details stay in `active_sessions`. Before writing `usage_history` and the aggregates the sessions are collapsed into **one record per common name per tick** (sum of increments and of rates).
|
||||
|
||||
Cache changes are collected in a temporary structure and applied only after the database transaction is committed. If the transaction fails, the cache is unchanged and the next tick counts the difference for the whole interval.
|
||||
|
||||
### B1. Read Validity, Grace Period, Restart and Shutdown
|
||||
- **Validity** (`parse_status`): the status file must contain a `TITLE` line, a `TIME,<text>,<epoch>` line and end with `END`. A missing file, a read error or an incomplete file (read while OpenVPN rewrites it) makes the gatherer **skip the tick entirely**: the database and the cache are not touched, and the next valid tick counts the difference for the whole interval. After `status_invalid_max` (default 6) skipped ticks in a row the message is logged at ERROR level.
|
||||
- **Validity** (`parse_status`): the status file must contain a `TITLE` line, a `TIME,<text>,<epoch>` line and end with `END`. A missing file, a read error or an incomplete file (read while OpenVPN rewrites it) makes the gatherer **skip the tick entirely**: the database and the cache are not touched, and the next valid tick counts the difference for the whole interval. The skip is logged on the first tick, at `status_invalid_max` (default 6, level ERROR) and then every 60th tick (about every 10 minutes at the default interval), not on every tick; when the file is readable again one INFO line says after how many skipped ticks.
|
||||
- **Freshness**: if `TIME` is older than `status_stale_seconds` (default 60 s), OpenVPN is considered stopped: there are no clients, all clients become `Disconnected`, sessions leave the cache via the grace period. A valid, fresh file without `CLIENT_LIST` lines simply means no clients.
|
||||
- **Grace period**: a session missing from the status is not dropped at once. Its `missing_ticks` counter grows each tick and the session is removed from the cache when it reaches `session_grace_ticks` (default 3, about 30 s). A session that comes back with the same key continues without double counting. A removed session is remembered (last counters, 1 hour) so that a late return with the same key is still counted as a continuation.
|
||||
- **Restart**: at start the gatherer loads `active_sessions` as the baseline (see the table above). `active_sessions.connected_since` stores the real session start in UTC (`YYYY-MM-DD HH:MM:SS`), which is also what `/sessions` returns.
|
||||
@@ -34,7 +36,7 @@ Cache changes are collected in a temporary structure and applied only after the
|
||||
|
||||
### C. Rate Calculation
|
||||
- **Speed**: Calculated as `Increment * 8 / (Interval * 1_000_000)` to get **Mbps**.
|
||||
- **Storage**: Raw samples (10s resolution) including speed and volume are stored in the `usage_history` table.
|
||||
- **Storage**: Raw samples (10s resolution) including speed and volume are stored in the `usage_history` table: one row per user per tick (the rate is the sum over the user's sessions). Rows written by earlier versions have one row per session; the API sums rows with the same `timestamp` before taking `MAX` over an interval.
|
||||
|
||||
## 2. Data Aggregation (TSDB)
|
||||
|
||||
|
||||
Reference in new issue
Block a user