106 lines
4.2 KiB
Python
106 lines
4.2 KiB
Python
"""Counting-only metrics from Suricata stats records.
|
|
|
|
Stats records update counters without changing policy, SQLite state or
|
|
router configuration. The caller controls when reports are logged.
|
|
|
|
Live eve.json (Suricata 7, threads: no + totals: yes) carries
|
|
capture/decoder/detect sub-objects on each stats record; there is no
|
|
'ifaces' object at totals level, so capture packets come from
|
|
capture.kernel_packets (the real field on the wire).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
|
|
class Metrics:
|
|
"""Cumulative Suricata counters; one delta report per full window.
|
|
|
|
The report gate exists because stats records arrive every
|
|
metrics.reconcile_interval seconds; a backlog drain can deliver two
|
|
stats records back-to-back, and the journal must still see at most one
|
|
line per window.
|
|
"""
|
|
|
|
def __init__(self, reconcile_interval: float = 30.0):
|
|
self.interval = max(1.0, float(reconcile_interval))
|
|
self.alerts = 0
|
|
self.non_alert_events = 0
|
|
self.stats_seen = 0
|
|
self._totals: dict[str, int] = {}
|
|
self._baseline: dict[str, int] | None = None
|
|
self._last = 0.0
|
|
|
|
def note_alert(self) -> None:
|
|
self.alerts += 1
|
|
|
|
def note_other(self) -> None:
|
|
self.non_alert_events += 1
|
|
|
|
def note_stats(self, event: dict) -> None:
|
|
"""Extract the cumulative counters; nothing else is logged or stored.
|
|
|
|
Live suricata emits both thread records (flush=False: per-thread
|
|
counters, 'engine' key) and a totals record (uptime/memcap_pressure
|
|
keys, 'detect' key). Totals are authoritative for rates; thread
|
|
records are only accepted if no totals have ever been seen.
|
|
"""
|
|
stats = event.get("stats") or {}
|
|
cap = stats.get("capture") or {}
|
|
dec = stats.get("decoder") or {}
|
|
det = stats.get("detect") or stats.get("engine") or {}
|
|
totals = {
|
|
"uptime": int(stats.get("uptime", 0)),
|
|
"memcap_pressure": int(stats.get("memcap_pressure", 0)),
|
|
"packets": int(cap.get("kernel_packets", 0)),
|
|
"dropped": int(cap.get("kernel_drops", 0)),
|
|
"capture_errors": int(cap.get("errors", 0)),
|
|
"decoded": int(dec.get("pkts", 0)),
|
|
"bytes": int(dec.get("bytes", 0)),
|
|
"invalid": int(dec.get("invalid", 0)),
|
|
"engine_alerts": int(det.get("alert", 0)),
|
|
"suppressed": int(det.get("alerts_suppressed", 0)),
|
|
"queue_overflow": int(det.get("alert_queue_overflow", 0)),
|
|
}
|
|
if "uptime" in stats or not self._totals:
|
|
self._totals = totals
|
|
self.stats_seen += 1
|
|
|
|
def report(self, now: float) -> dict | None:
|
|
"""None unless a full flush window has passed since the last report.
|
|
|
|
First stats record only adopts the cumulative baseline: deltas are
|
|
meaningless before the second stats tick (counters run since process
|
|
start), so the first window must not pretend to be a measured one.
|
|
Suricata's counters reset on restart: deltas never go negative, the
|
|
reset is flagged instead.
|
|
"""
|
|
if not self._totals:
|
|
return None
|
|
if self._baseline is None:
|
|
self._baseline = dict(self._totals)
|
|
self._last = now
|
|
return {"init": True, "alerts": self.alerts, "stats": dict(self._totals)}
|
|
if now - self._last < self.interval * 0.75: # tolerate flush jitter
|
|
return None
|
|
window = now - self._last
|
|
delta, restart = {}, False
|
|
for key, value in self._totals.items():
|
|
n = value - self._baseline.get(key, 0)
|
|
if n < 0:
|
|
restart = True
|
|
n = value
|
|
delta[key] = n
|
|
self._baseline = dict(self._totals)
|
|
self._last = now
|
|
return {
|
|
"init": False,
|
|
"restart": restart,
|
|
"window": round(window, 1),
|
|
"alerts": self.alerts,
|
|
"engine_alerts_delta": delta["engine_alerts"],
|
|
"cps": round(delta["packets"] / window, 1), # captured pps
|
|
"eps": round(delta["decoded"] / window, 1), # decoded pps
|
|
"dropped": delta["dropped"],
|
|
"delta": delta,
|
|
"stats": dict(self._totals),
|
|
}
|