sensitivebox 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sensitivebox/__init__.py +10 -0
- sensitivebox/alerts/__init__.py +94 -0
- sensitivebox/alerts/config.py +85 -0
- sensitivebox/alerts/defaults.toml +126 -0
- sensitivebox/alerts/engine.py +624 -0
- sensitivebox/alerts/events.py +87 -0
- sensitivebox/alerts/globs.py +84 -0
- sensitivebox/alerts/model.py +215 -0
- sensitivebox/alerts/mute.py +55 -0
- sensitivebox/alerts/replay.py +99 -0
- sensitivebox/alerts/routing.py +247 -0
- sensitivebox/api/__init__.py +6 -0
- sensitivebox/api/app.py +225 -0
- sensitivebox/api/data.py +109 -0
- sensitivebox/api/host.py +27 -0
- sensitivebox/api/model.py +90 -0
- sensitivebox/api/security.py +89 -0
- sensitivebox/api/server.py +108 -0
- sensitivebox/api/state.py +220 -0
- sensitivebox/api/static/assets/archivo-latin-wdth-normal-DY7AcnAa.woff2 +0 -0
- sensitivebox/api/static/assets/ibm-plex-mono-medium-BpyVxOr_.woff2 +0 -0
- sensitivebox/api/static/assets/index-BH9EydfS.js +10 -0
- sensitivebox/api/static/assets/index-RMruxW54.css +1 -0
- sensitivebox/api/static/assets/mark-on-dark-CpSxozdM.png +0 -0
- sensitivebox/api/static/assets/mark-on-light-DgUAeHTp.png +0 -0
- sensitivebox/api/static/assets/mark-raised-Bg5z6PGs.png +0 -0
- sensitivebox/api/static/index.html +14 -0
- sensitivebox/browser.py +106 -0
- sensitivebox/cli.py +467 -0
- sensitivebox/collector.py +319 -0
- sensitivebox/completion.py +117 -0
- sensitivebox/daemon.py +181 -0
- sensitivebox/data/icons/128.png +0 -0
- sensitivebox/data/icons/256.png +0 -0
- sensitivebox/data/icons/512.png +0 -0
- sensitivebox/data/proart-p16.tar.gz +0 -0
- sensitivebox/data/sensitivebox.desktop.in +8 -0
- sensitivebox/data/sensitivebox.service.in +11 -0
- sensitivebox/doctor.py +322 -0
- sensitivebox/fixtures.py +177 -0
- sensitivebox/history.py +298 -0
- sensitivebox/hwmon/__init__.py +20 -0
- sensitivebox/hwmon/classify.py +74 -0
- sensitivebox/hwmon/discovery.py +139 -0
- sensitivebox/hwmon/ids.py +35 -0
- sensitivebox/hwmon/model.py +78 -0
- sensitivebox/hwmon/normalise.py +42 -0
- sensitivebox/hwmon/reader.py +65 -0
- sensitivebox/inspect.py +175 -0
- sensitivebox/live.py +56 -0
- sensitivebox/notify/__init__.py +7 -0
- sensitivebox/notify/base.py +60 -0
- sensitivebox/notify/desktop.py +84 -0
- sensitivebox/notify/log.py +30 -0
- sensitivebox/operations.py +44 -0
- sensitivebox/output.py +76 -0
- sensitivebox/process.py +25 -0
- sensitivebox/py.typed +0 -0
- sensitivebox/redact.py +85 -0
- sensitivebox/report.py +357 -0
- sensitivebox/runtime.py +313 -0
- sensitivebox/sample.py +84 -0
- sensitivebox/service.py +112 -0
- sensitivebox/simulate.py +220 -0
- sensitivebox/sources/__init__.py +83 -0
- sensitivebox/sources/apps.py +135 -0
- sensitivebox/sources/bluez.py +96 -0
- sensitivebox/sources/bluez_watch.py +214 -0
- sensitivebox/sources/changes.py +121 -0
- sensitivebox/sources/cpu.py +152 -0
- sensitivebox/sources/dbus.py +174 -0
- sensitivebox/sources/logind.py +88 -0
- sensitivebox/sources/memory.py +127 -0
- sensitivebox/sources/net.py +128 -0
- sensitivebox/sources/power_profiles.py +116 -0
- sensitivebox/sources/power_supply.py +70 -0
- sensitivebox/sources/psi.py +78 -0
- sensitivebox/sources/report.py +85 -0
- sensitivebox/sources/wifi.py +90 -0
- sensitivebox/sources/wifi_nm.py +269 -0
- sensitivebox/status.py +205 -0
- sensitivebox/store/__init__.py +5 -0
- sensitivebox/store/changes.py +58 -0
- sensitivebox/store/db.py +423 -0
- sensitivebox/store/events.py +337 -0
- sensitivebox/store/mutes.py +58 -0
- sensitivebox/store/quality.py +51 -0
- sensitivebox/store/read.py +40 -0
- sensitivebox/store/schema.py +194 -0
- sensitivebox/store/status.py +105 -0
- sensitivebox-0.1.0.dist-info/METADATA +424 -0
- sensitivebox-0.1.0.dist-info/RECORD +95 -0
- sensitivebox-0.1.0.dist-info/WHEEL +4 -0
- sensitivebox-0.1.0.dist-info/entry_points.txt +4 -0
- sensitivebox-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
"""Turns the sources into the signals the alert engine and the store consume, once per poll.
|
|
2
|
+
|
|
3
|
+
Polling is tiered (docs/research/new-relic-lessons.md §3). Cheap, fast-moving values are read on
|
|
4
|
+
every poll; slower ones are read less often and *held* in between, so the engine never sees a
|
|
5
|
+
false "no reading" just because a tier wasn't due:
|
|
6
|
+
|
|
7
|
+
====== =========================== ==========================================================
|
|
8
|
+
Tier Interval Sources
|
|
9
|
+
====== =========================== ==========================================================
|
|
10
|
+
fast the poll interval (1 s) hwmon sensors, PSI
|
|
11
|
+
medium 2 s ``/proc/meminfo``, ``/proc/vmstat``, network counters
|
|
12
|
+
slow 5 s ``/sys/class/power_supply``
|
|
13
|
+
apps at most every 20 s per-app cgroup memory, and only while memory is tight
|
|
14
|
+
====== =========================== ==========================================================
|
|
15
|
+
|
|
16
|
+
A tier's interval is never shorter than the poll interval. Reads are plain small file reads: no
|
|
17
|
+
subprocess, no D-Bus call. Everything takes ``sysfs_root``/``proc_root`` so tests run on fixtures.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from collections.abc import Callable, Mapping
|
|
21
|
+
from dataclasses import dataclass, field
|
|
22
|
+
from datetime import date
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
|
|
25
|
+
from sensitivebox import hwmon
|
|
26
|
+
from sensitivebox.alerts.engine import Clock
|
|
27
|
+
from sensitivebox.alerts.model import Missing, Value
|
|
28
|
+
from sensitivebox.hwmon.model import Limits, ReadStatus, Sensor
|
|
29
|
+
from sensitivebox.hwmon.reader import Opener, default_opener, read_int
|
|
30
|
+
from sensitivebox.sources.apps import AppMemory, app_slice_dir, read_app_memory, should_sample_apps
|
|
31
|
+
from sensitivebox.sources.cpu import CpuTimes, read_cpu_freqs, read_cpu_times, utilisation
|
|
32
|
+
from sensitivebox.sources.memory import MemInfo, read_meminfo, read_vmstat, read_zram
|
|
33
|
+
from sensitivebox.sources.net import NetMeter, read_net_counters
|
|
34
|
+
from sensitivebox.sources.power_supply import read_power_supplies
|
|
35
|
+
from sensitivebox.sources.psi import Pressure, read_pressure
|
|
36
|
+
from sensitivebox.sources.wifi import WifiSignal, read_proc_wireless
|
|
37
|
+
|
|
38
|
+
PSI_SOME = "psi/memory/some_avg10"
|
|
39
|
+
PSI_FULL = "psi/memory/full_avg10"
|
|
40
|
+
MEMORY_AVAILABLE_PCT = "memory/available_pct"
|
|
41
|
+
OOM_KILL_DELTA = "vmstat/oom_kill_delta"
|
|
42
|
+
|
|
43
|
+
APPS_TOP = 5
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass(frozen=True, slots=True)
|
|
47
|
+
class Tiers:
|
|
48
|
+
"""Minimum seconds between reads of each tier (the poll interval is the fast tier)."""
|
|
49
|
+
|
|
50
|
+
medium_s: float = 2.0
|
|
51
|
+
slow_s: float = 5.0
|
|
52
|
+
apps_s: float = 20.0
|
|
53
|
+
|
|
54
|
+
def __post_init__(self) -> None:
|
|
55
|
+
if min(self.medium_s, self.slow_s, self.apps_s) <= 0:
|
|
56
|
+
raise ValueError("tier intervals must be positive")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass(frozen=True, slots=True)
|
|
60
|
+
class Snapshot:
|
|
61
|
+
"""What one poll produced."""
|
|
62
|
+
|
|
63
|
+
at: float # the collector's (monotonic) clock
|
|
64
|
+
signals: Mapping[str, Value] # what the alert engine evaluates
|
|
65
|
+
metrics: Mapping[str, float] = field(default_factory=dict[str, float]) # stored, not alerted on
|
|
66
|
+
apps: tuple[AppMemory, ...] = () # top apps by memory; empty unless memory is tight
|
|
67
|
+
units: Mapping[str, str] = field(default_factory=dict[str, str])
|
|
68
|
+
display: Mapping[str, Value] = field(default_factory=dict[str, Value])
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class Collector:
|
|
72
|
+
"""Reads every source on its tier and returns a :class:`Snapshot` per call to ``collect``.
|
|
73
|
+
|
|
74
|
+
``collect`` is synchronous: a poll is a few dozen sub-millisecond file reads.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
def __init__(
|
|
78
|
+
self,
|
|
79
|
+
sysfs_root: Path,
|
|
80
|
+
proc_root: Path,
|
|
81
|
+
*,
|
|
82
|
+
clock: Clock,
|
|
83
|
+
poll_interval: float = 1.0,
|
|
84
|
+
tiers: Tiers = Tiers(), # noqa: B008 - frozen, shared by design
|
|
85
|
+
opener: Opener = default_opener,
|
|
86
|
+
today: Callable[[], date] = date.today,
|
|
87
|
+
) -> None:
|
|
88
|
+
if poll_interval <= 0:
|
|
89
|
+
raise ValueError("poll interval must be positive")
|
|
90
|
+
self._sysfs = sysfs_root
|
|
91
|
+
self._proc = proc_root
|
|
92
|
+
self._clock = clock
|
|
93
|
+
self._poll = poll_interval
|
|
94
|
+
self._tiers = tiers
|
|
95
|
+
self._opener = opener
|
|
96
|
+
self._today = today
|
|
97
|
+
self._chips = hwmon.discover(sysfs_root, opener=opener)
|
|
98
|
+
self.limits: dict[str, Limits] = {
|
|
99
|
+
sensor.id: sensor.limits for chip in self._chips for sensor in chip.sensors
|
|
100
|
+
}
|
|
101
|
+
self._units = {sensor.id: sensor.unit for chip in self._chips for sensor in chip.sensors}
|
|
102
|
+
self._units.update({PSI_SOME: "%", PSI_FULL: "%", MEMORY_AVAILABLE_PCT: "%"})
|
|
103
|
+
self._apps_dir = app_slice_dir(proc_root, sysfs_root, opener)
|
|
104
|
+
self._meter = NetMeter()
|
|
105
|
+
self._held: dict[str, Value] = {}
|
|
106
|
+
self._last_read: dict[str, float] = {}
|
|
107
|
+
self._mem: MemInfo | None = None
|
|
108
|
+
self._oom_last: int | None = None
|
|
109
|
+
self._oom: Value = Missing("no baseline")
|
|
110
|
+
self._apps: tuple[AppMemory, ...] = ()
|
|
111
|
+
self._cpu: CpuTimes | None = None
|
|
112
|
+
self._display: dict[str, Value] = {}
|
|
113
|
+
self.wireless: dict[str, WifiSignal] = {}
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def sensors(self) -> tuple[Sensor, ...]:
|
|
117
|
+
return tuple(sensor for chip in self._chips for sensor in chip.sensors)
|
|
118
|
+
|
|
119
|
+
def reset_baselines(self) -> None:
|
|
120
|
+
"""After sleep: counters and held values are stale. Re-read every tier next poll and
|
|
121
|
+
start rates and deltas from a fresh baseline (per-day network totals are kept)."""
|
|
122
|
+
meter = NetMeter()
|
|
123
|
+
meter.seed(self._meter.totals)
|
|
124
|
+
self._meter = meter
|
|
125
|
+
self._held.clear()
|
|
126
|
+
self._last_read.clear()
|
|
127
|
+
self._oom_last = None
|
|
128
|
+
self._oom = Missing("no baseline")
|
|
129
|
+
self._apps = ()
|
|
130
|
+
self._cpu = None
|
|
131
|
+
self._display.clear()
|
|
132
|
+
|
|
133
|
+
def collect(self) -> Snapshot:
|
|
134
|
+
now = self._clock()
|
|
135
|
+
signals: dict[str, Value] = {}
|
|
136
|
+
metrics: dict[str, float] = {}
|
|
137
|
+
|
|
138
|
+
for sensor_id, reading in hwmon.read_all(self._chips, self._opener).items():
|
|
139
|
+
if reading.status is ReadStatus.OK and reading.value is not None:
|
|
140
|
+
signals[sensor_id] = reading.value
|
|
141
|
+
else:
|
|
142
|
+
signals[sensor_id] = Missing(reading.reason or reading.status.value)
|
|
143
|
+
|
|
144
|
+
pressure = read_pressure(self._proc, "memory", self._opener)
|
|
145
|
+
signals.update(_psi_signals(pressure))
|
|
146
|
+
for resource in ("cpu", "io"):
|
|
147
|
+
current = read_pressure(self._proc, resource, self._opener)
|
|
148
|
+
for scope in ("some", "full"):
|
|
149
|
+
line = getattr(current, scope) if current is not None else None
|
|
150
|
+
for window in ("avg10", "avg60"):
|
|
151
|
+
identity = f"psi/{resource}/{scope}_{window}"
|
|
152
|
+
self._units[identity] = "%"
|
|
153
|
+
signals[identity] = (
|
|
154
|
+
getattr(line, window) if line is not None else Missing("PSI unavailable")
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
if self._due("medium", self._tiers.medium_s, now):
|
|
158
|
+
self._read_medium(now, metrics)
|
|
159
|
+
if self._due("slow", self._tiers.slow_s, now):
|
|
160
|
+
self._read_slow()
|
|
161
|
+
signals.update(self._held)
|
|
162
|
+
|
|
163
|
+
signals[OOM_KILL_DELTA] = self._oom
|
|
164
|
+
if not isinstance(self._oom, Missing):
|
|
165
|
+
self._oom = 0.0 # a delta is an event: published once, then back to zero
|
|
166
|
+
|
|
167
|
+
self._read_apps(now, pressure)
|
|
168
|
+
return Snapshot(now, signals, metrics, self._apps, dict(self._units), dict(self._display))
|
|
169
|
+
|
|
170
|
+
def _due(self, tier: str, interval: float, now: float) -> bool:
|
|
171
|
+
"""True when ``interval`` has passed since the tier was last read, allowing half a poll
|
|
172
|
+
of scheduling jitter so a 2 s tier on a 1 s poll doesn't slip to 3 s."""
|
|
173
|
+
last = self._last_read.get(tier)
|
|
174
|
+
if last is not None and now - last < max(interval, self._poll) - self._poll / 2:
|
|
175
|
+
return False
|
|
176
|
+
self._last_read[tier] = now
|
|
177
|
+
return True
|
|
178
|
+
|
|
179
|
+
def _read_medium(self, now: float, metrics: dict[str, float]) -> None:
|
|
180
|
+
self._mem = read_meminfo(self._proc, self._opener)
|
|
181
|
+
if self._mem is None or self._mem.total <= 0:
|
|
182
|
+
self._held[MEMORY_AVAILABLE_PCT] = Missing("no MemAvailable in /proc/meminfo")
|
|
183
|
+
else:
|
|
184
|
+
self._held[MEMORY_AVAILABLE_PCT] = self._mem.available / self._mem.total * 100.0
|
|
185
|
+
|
|
186
|
+
for name, attr in (
|
|
187
|
+
("total_bytes", "total"),
|
|
188
|
+
("available_bytes", "available"),
|
|
189
|
+
("used_bytes", "used"),
|
|
190
|
+
("swap_used_bytes", "swap_used"),
|
|
191
|
+
("swap_total_bytes", "swap_total"),
|
|
192
|
+
):
|
|
193
|
+
self._hold(
|
|
194
|
+
f"memory/{name}", float(getattr(self._mem, attr)) if self._mem else None, "B"
|
|
195
|
+
)
|
|
196
|
+
self._read_dashboard_system()
|
|
197
|
+
|
|
198
|
+
self._read_oom()
|
|
199
|
+
|
|
200
|
+
counters = read_net_counters(self._sysfs, opener=self._opener)
|
|
201
|
+
self.wireless = read_proc_wireless(self._proc, self._opener)
|
|
202
|
+
for identity in self._display:
|
|
203
|
+
if identity.startswith("net/"):
|
|
204
|
+
self._display[identity] = Missing("network interface unavailable")
|
|
205
|
+
for iface, rate in self._meter.update(counters, monotonic=now, day=self._today()).items():
|
|
206
|
+
for direction, value in (("rx", rate.rx_bps), ("tx", rate.tx_bps)):
|
|
207
|
+
name = f"net/{iface}/{direction}_bps"
|
|
208
|
+
self._units[name] = "B/s"
|
|
209
|
+
self._display[name] = value if value is not None else Missing("no baseline")
|
|
210
|
+
if rate.rx_bps is not None and rate.tx_bps is not None:
|
|
211
|
+
metrics[f"net/{iface}/rx_bps"] = rate.rx_bps
|
|
212
|
+
metrics[f"net/{iface}/tx_bps"] = rate.tx_bps
|
|
213
|
+
if not counters:
|
|
214
|
+
for direction in ("rx", "tx"):
|
|
215
|
+
self._display[f"net/{direction}_bps"] = Missing("network interface unavailable")
|
|
216
|
+
self._units[f"net/{direction}_bps"] = "B/s"
|
|
217
|
+
# The existing store has no persistent daily totals yet. Never label session totals
|
|
218
|
+
# as today's/month's bytes or invent zero. The dashboard still receives these entries.
|
|
219
|
+
for period in ("today", "month"):
|
|
220
|
+
for direction in ("rx", "tx"):
|
|
221
|
+
name = f"net/{period}/{direction}_bytes"
|
|
222
|
+
self._display[name] = Missing("persistent network totals unavailable")
|
|
223
|
+
self._units[name] = "B"
|
|
224
|
+
|
|
225
|
+
def _hold(self, identity: str, value: float | None, unit: str) -> None:
|
|
226
|
+
self._held[identity] = value if value is not None else Missing("source unavailable")
|
|
227
|
+
self._units[identity] = unit
|
|
228
|
+
|
|
229
|
+
def _read_dashboard_system(self) -> None:
|
|
230
|
+
current = read_cpu_times(self._proc, self._opener)
|
|
231
|
+
util = utilisation(self._cpu, current) if self._cpu and current else None
|
|
232
|
+
self._cpu = current
|
|
233
|
+
self._hold("cpu/util_pct", util.overall * 100 if util else None, "%")
|
|
234
|
+
for identity in self._held:
|
|
235
|
+
if identity.startswith(("cpu/core", "zram/", "gpu/")):
|
|
236
|
+
self._held[identity] = Missing("source unavailable")
|
|
237
|
+
if util is not None:
|
|
238
|
+
for cpu, share in zip(util.cpus, util.cores, strict=True):
|
|
239
|
+
self._hold(f"cpu/core{cpu}/util_pct", share * 100, "%")
|
|
240
|
+
for freq in read_cpu_freqs(self._sysfs, self._opener):
|
|
241
|
+
self._hold(f"cpu/core{freq.cpu}/freq_hz", freq.cur_hz, "Hz")
|
|
242
|
+
self.limits[f"cpu/core{freq.cpu}/freq_hz"] = Limits(min=freq.min_hz, max=freq.max_hz)
|
|
243
|
+
self._hold("zram/ratio", None, "x")
|
|
244
|
+
self._hold("zram/saved_bytes", None, "B")
|
|
245
|
+
for zram in read_zram(self._sysfs, self._opener):
|
|
246
|
+
self._hold(f"zram/{zram.name}/ratio", zram.ratio, "x")
|
|
247
|
+
self._hold(f"zram/{zram.name}/saved_bytes", float(zram.saved_bytes), "B")
|
|
248
|
+
self._hold("gpu/util_pct", None, "%")
|
|
249
|
+
drm = self._sysfs / "class/drm"
|
|
250
|
+
for path in sorted(drm.glob("card[0-9]*/device/gpu_busy_percent")):
|
|
251
|
+
value = read_int(path, self._opener)
|
|
252
|
+
self._hold(
|
|
253
|
+
f"gpu/{path.parent.parent.name}/util_pct",
|
|
254
|
+
float(value) if value is not None else None,
|
|
255
|
+
"%",
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
def _read_oom(self) -> None:
|
|
259
|
+
vmstat = read_vmstat(self._proc, self._opener)
|
|
260
|
+
count = None if vmstat is None else vmstat.oom_kill
|
|
261
|
+
if count is None:
|
|
262
|
+
self._oom = Missing("no oom_kill counter in /proc/vmstat")
|
|
263
|
+
self._oom_last = None
|
|
264
|
+
return
|
|
265
|
+
last, self._oom_last = self._oom_last, count
|
|
266
|
+
if last is None:
|
|
267
|
+
self._oom = Missing("no baseline")
|
|
268
|
+
else:
|
|
269
|
+
self._oom = float(max(count - last, 0)) # a counter that goes down is a new baseline
|
|
270
|
+
|
|
271
|
+
def _read_slow(self) -> None:
|
|
272
|
+
# Hold values between slow reads, but never across a read that lost the device.
|
|
273
|
+
for signal in self._held:
|
|
274
|
+
if signal.startswith("power_supply/"):
|
|
275
|
+
self._held[signal] = Missing("power supply unavailable")
|
|
276
|
+
for supply in read_power_supplies(self._sysfs, self._opener):
|
|
277
|
+
prefix = f"power_supply/{supply.name}"
|
|
278
|
+
if supply.kind == "Battery":
|
|
279
|
+
self._units[f"{prefix}/capacity"] = "%"
|
|
280
|
+
self._held[f"{prefix}/capacity"] = (
|
|
281
|
+
Missing("capacity unreadable") if supply.capacity is None else supply.capacity
|
|
282
|
+
)
|
|
283
|
+
self._held[f"{prefix}/status"] = (
|
|
284
|
+
Missing("status unreadable") if supply.status is None else supply.status
|
|
285
|
+
)
|
|
286
|
+
if supply.is_usb_source:
|
|
287
|
+
self._units[f"{prefix}/power_w"] = "W"
|
|
288
|
+
if supply.online is False:
|
|
289
|
+
self._held[f"{prefix}/power_w"] = Missing("port offline")
|
|
290
|
+
elif supply.power_w is None:
|
|
291
|
+
self._held[f"{prefix}/power_w"] = Missing("no voltage or current")
|
|
292
|
+
else:
|
|
293
|
+
self._held[f"{prefix}/power_w"] = supply.power_w
|
|
294
|
+
|
|
295
|
+
def _read_apps(self, now: float, pressure: Pressure | None) -> None:
|
|
296
|
+
if not should_sample_apps(self._mem, pressure):
|
|
297
|
+
self._apps = ()
|
|
298
|
+
self._last_read.pop("apps", None) # memory eased: sample at once next time it's tight
|
|
299
|
+
return
|
|
300
|
+
if self._apps_dir is None or not self._apps_dir.is_dir():
|
|
301
|
+
self._apps_dir = app_slice_dir(self._proc, self._sysfs, self._opener)
|
|
302
|
+
self._apps = ()
|
|
303
|
+
self._last_read.pop("apps", None)
|
|
304
|
+
if self._apps_dir is None:
|
|
305
|
+
return
|
|
306
|
+
if self._due("apps", self._tiers.apps_s, now):
|
|
307
|
+
self._apps = read_app_memory(self._apps_dir, top=APPS_TOP, opener=self._opener)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def _psi_signals(pressure: Pressure | None) -> dict[str, Value]:
|
|
311
|
+
if pressure is None:
|
|
312
|
+
reason = Missing("PSI unavailable (psi=0 or unsupported kernel)")
|
|
313
|
+
return {PSI_SOME: reason, PSI_FULL: reason}
|
|
314
|
+
full: Value = (
|
|
315
|
+
Missing("no 'full' line in memory pressure")
|
|
316
|
+
if pressure.full is None
|
|
317
|
+
else pressure.full.avg10
|
|
318
|
+
)
|
|
319
|
+
return {PSI_SOME: pressure.some.avg10, PSI_FULL: full}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Shell completion generated from public vocabulary, scenarios and configured rule IDs."""
|
|
2
|
+
|
|
3
|
+
import shlex
|
|
4
|
+
from collections.abc import Sequence
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
from sensitivebox.alerts import Rule
|
|
8
|
+
from sensitivebox.simulate import SCENARIOS
|
|
9
|
+
|
|
10
|
+
PUBLIC_COMMANDS = (
|
|
11
|
+
"watch",
|
|
12
|
+
"doctor",
|
|
13
|
+
"demo",
|
|
14
|
+
"apps",
|
|
15
|
+
"rules",
|
|
16
|
+
"completion",
|
|
17
|
+
"mute",
|
|
18
|
+
"unmute",
|
|
19
|
+
"quiet",
|
|
20
|
+
"report",
|
|
21
|
+
"open",
|
|
22
|
+
"enable",
|
|
23
|
+
"disable",
|
|
24
|
+
"log",
|
|
25
|
+
"rule",
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True, slots=True)
|
|
30
|
+
class Completion:
|
|
31
|
+
shell: str
|
|
32
|
+
script: str
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def completion(shell: str, rules: Sequence[Rule]) -> Completion:
|
|
36
|
+
arrays = "\n".join(
|
|
37
|
+
f" local -a {name}=({' '.join(shlex.quote(value) for value in values)})"
|
|
38
|
+
for name, values in (
|
|
39
|
+
("public_commands", PUBLIC_COMMANDS),
|
|
40
|
+
("scenarios", tuple(SCENARIOS)),
|
|
41
|
+
("rule_ids", tuple(r.id for r in rules)),
|
|
42
|
+
)
|
|
43
|
+
)
|
|
44
|
+
if shell == "bash":
|
|
45
|
+
script = """# Sensitive Box completion for bash; source this file.
|
|
46
|
+
_sbox_complete() {
|
|
47
|
+
@ARRAYS@
|
|
48
|
+
local current="${COMP_WORDS[COMP_CWORD]}" previous="${COMP_WORDS[COMP_CWORD-1]}"
|
|
49
|
+
local command='' item i
|
|
50
|
+
local -a candidates=(--json --help)
|
|
51
|
+
COMPREPLY=()
|
|
52
|
+
for item in "${COMP_WORDS[@]:1:COMP_CWORD-1}"; do
|
|
53
|
+
case "$item" in
|
|
54
|
+
watch|doctor|demo|apps|rules|completion|rule|mute|unmute|quiet|log|enable|disable|open|report)
|
|
55
|
+
command="$item"; break ;;
|
|
56
|
+
esac
|
|
57
|
+
done
|
|
58
|
+
if [[ "$previous" == --db ]]; then
|
|
59
|
+
mapfile -t COMPREPLY < <(compgen -f -- "$current")
|
|
60
|
+
return 0
|
|
61
|
+
fi
|
|
62
|
+
case "$command" in
|
|
63
|
+
'') candidates+=("${public_commands[@]}" --version --db) ;;
|
|
64
|
+
demo) candidates+=("${scenarios[@]}" --no-notify --interval) ;;
|
|
65
|
+
watch) candidates+=(--interval --db) ;;
|
|
66
|
+
doctor) candidates+=(--verbose) ;;
|
|
67
|
+
rules) candidates+=(--db "${rule_ids[@]}") ;;
|
|
68
|
+
report) candidates+=(--fixture --keep-app-names --output) ;;
|
|
69
|
+
open) candidates+=(log rule settings "${rule_ids[@]}") ;;
|
|
70
|
+
log) candidates+=(--since --limit --weekly --daemon --follow --db "${rule_ids[@]}") ;;
|
|
71
|
+
quiet) candidates+=(off --no-renotify --db) ;;
|
|
72
|
+
completion) candidates+=(bash zsh) ;;
|
|
73
|
+
rule|mute|unmute) candidates+=("${rule_ids[@]}") ;;
|
|
74
|
+
esac
|
|
75
|
+
for item in "${candidates[@]}"; do
|
|
76
|
+
[[ "$item" == "$current"* ]] && COMPREPLY+=("$item")
|
|
77
|
+
done
|
|
78
|
+
return 0
|
|
79
|
+
}
|
|
80
|
+
complete -F _sbox_complete sbox sensitivebox
|
|
81
|
+
"""
|
|
82
|
+
else:
|
|
83
|
+
script = """#compdef sbox sensitivebox
|
|
84
|
+
# Sensitive Box completion for zsh; initialize compinit, then source this file.
|
|
85
|
+
_sbox_complete() {
|
|
86
|
+
@ARRAYS@
|
|
87
|
+
local command='' item i
|
|
88
|
+
local -a candidates=(--json --help)
|
|
89
|
+
for ((i=2; i<CURRENT; i++)); do
|
|
90
|
+
item="${words[i]}"
|
|
91
|
+
case "$item" in
|
|
92
|
+
watch|doctor|demo|apps|rules|completion|rule|mute|unmute|quiet|log|enable|disable|open|report)
|
|
93
|
+
command="$item"; break ;;
|
|
94
|
+
esac
|
|
95
|
+
done
|
|
96
|
+
if [[ "${words[CURRENT-1]}" == --db ]]; then
|
|
97
|
+
_files
|
|
98
|
+
return
|
|
99
|
+
fi
|
|
100
|
+
case "$command" in
|
|
101
|
+
'') candidates+=("${public_commands[@]}" --version --db) ;;
|
|
102
|
+
demo) candidates+=("${scenarios[@]}" --no-notify --interval) ;;
|
|
103
|
+
watch) candidates+=(--interval --db) ;;
|
|
104
|
+
doctor) candidates+=(--verbose) ;;
|
|
105
|
+
rules) candidates+=(--db "${rule_ids[@]}") ;;
|
|
106
|
+
report) candidates+=(--fixture --keep-app-names --output) ;;
|
|
107
|
+
open) candidates+=(log rule settings "${rule_ids[@]}") ;;
|
|
108
|
+
log) candidates+=(--since --limit --weekly --daemon --follow --db "${rule_ids[@]}") ;;
|
|
109
|
+
quiet) candidates+=(off --no-renotify --db) ;;
|
|
110
|
+
completion) candidates+=(bash zsh) ;;
|
|
111
|
+
rule|mute|unmute) candidates+=("${rule_ids[@]}") ;;
|
|
112
|
+
esac
|
|
113
|
+
compadd -- "${candidates[@]}"
|
|
114
|
+
}
|
|
115
|
+
compdef _sbox_complete sbox sensitivebox
|
|
116
|
+
"""
|
|
117
|
+
return Completion(shell, script.replace("@ARRAYS@", arrays))
|
sensitivebox/daemon.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""The asyncio main loop: poll, evaluate, store, route, notify, once per interval.
|
|
2
|
+
|
|
3
|
+
One ``tick`` is: collect a snapshot, feed the alert engine (which journals its inputs into the
|
|
4
|
+
store, so they can be replayed), hand the metrics and the rule evaluations to the store, route
|
|
5
|
+
the engine's transitions through mute and cooldown, and send what is left to the notifiers. The
|
|
6
|
+
engine and the collector share one monotonic clock, injected, so tests drive the whole loop with
|
|
7
|
+
a manual clock and no sleeping.
|
|
8
|
+
|
|
9
|
+
Sleep: ``on_suspend``/``on_resume`` (wired to logind) pause everything. A suspended daemon skips
|
|
10
|
+
its ticks, so closing the lid never fires an alert and never a "no data" one. On resume every
|
|
11
|
+
rule warms up again and the collector starts its rates and deltas from a fresh baseline.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import asyncio
|
|
15
|
+
import contextlib
|
|
16
|
+
import logging
|
|
17
|
+
from collections.abc import Awaitable, Callable
|
|
18
|
+
from dataclasses import dataclass
|
|
19
|
+
from typing import Protocol
|
|
20
|
+
|
|
21
|
+
from sensitivebox.alerts.engine import Clock, Engine, Step
|
|
22
|
+
from sensitivebox.alerts.model import State
|
|
23
|
+
from sensitivebox.alerts.routing import Held, Notification, Routed, Router
|
|
24
|
+
from sensitivebox.collector import Snapshot
|
|
25
|
+
from sensitivebox.notify.base import NotifierHub
|
|
26
|
+
from sensitivebox.store import Store
|
|
27
|
+
from sensitivebox.store.mutes import ActiveMute
|
|
28
|
+
|
|
29
|
+
log = logging.getLogger(__name__)
|
|
30
|
+
|
|
31
|
+
NOTIFY_TIMEOUT_S = 2.0 # a hung notification daemon may delay a poll this long, no longer
|
|
32
|
+
LOG_EVERY = 60 # a poll that keeps failing is logged on the 1st failure and every 60th
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class SignalSource(Protocol):
|
|
36
|
+
"""What the daemon polls: the real :class:`Collector`, or the simulator."""
|
|
37
|
+
|
|
38
|
+
def collect(self) -> Snapshot: ...
|
|
39
|
+
def reset_baselines(self) -> None: ...
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True, slots=True)
|
|
43
|
+
class TickResult:
|
|
44
|
+
snapshot: Snapshot
|
|
45
|
+
step: Step
|
|
46
|
+
routed: Routed
|
|
47
|
+
muted: tuple[ActiveMute, ...] | None = None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class Daemon:
|
|
51
|
+
def __init__(
|
|
52
|
+
self,
|
|
53
|
+
*,
|
|
54
|
+
source: SignalSource,
|
|
55
|
+
engine: Engine,
|
|
56
|
+
router: Router,
|
|
57
|
+
store: Store,
|
|
58
|
+
notifiers: NotifierHub,
|
|
59
|
+
clock: Clock,
|
|
60
|
+
interval: float = 1.0,
|
|
61
|
+
sleep: Callable[[float], Awaitable[None]] | None = None,
|
|
62
|
+
on_loop: Callable[[], None] | None = None,
|
|
63
|
+
on_tick: Callable[[TickResult], None] | None = None,
|
|
64
|
+
notify_timeout: float = NOTIFY_TIMEOUT_S,
|
|
65
|
+
save_router: Callable[[str], None] | None = None,
|
|
66
|
+
muted: Callable[[], tuple[ActiveMute, ...]] | None = None,
|
|
67
|
+
) -> None:
|
|
68
|
+
if interval <= 0:
|
|
69
|
+
raise ValueError("poll interval must be positive")
|
|
70
|
+
self._muted = muted
|
|
71
|
+
self._save_router = save_router
|
|
72
|
+
self._source = source
|
|
73
|
+
self._engine = engine
|
|
74
|
+
self._router = router
|
|
75
|
+
self._store = store
|
|
76
|
+
self._notifiers = notifiers
|
|
77
|
+
self._clock = clock # the same monotonic clock the engine and the collector use
|
|
78
|
+
self._interval = interval
|
|
79
|
+
self._sleep = sleep
|
|
80
|
+
self._on_loop = on_loop
|
|
81
|
+
self._on_tick = on_tick
|
|
82
|
+
self._notify_timeout = notify_timeout
|
|
83
|
+
self._suspended = False
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def suspended(self) -> bool:
|
|
87
|
+
return self._suspended
|
|
88
|
+
|
|
89
|
+
def firing(self) -> int:
|
|
90
|
+
"""How many alerts are open right now."""
|
|
91
|
+
return sum(1 for state in self._engine.states().values() if state is State.FIRING)
|
|
92
|
+
|
|
93
|
+
# -- sleep ------------------------------------------------------------------------------
|
|
94
|
+
|
|
95
|
+
def on_suspend(self) -> None:
|
|
96
|
+
self._store.events.pause()
|
|
97
|
+
self._suspended = True
|
|
98
|
+
self._engine.suspend()
|
|
99
|
+
|
|
100
|
+
def on_resume(self) -> None:
|
|
101
|
+
self._engine.resume() # every rule warms up again; an open alert stays open
|
|
102
|
+
self._source.reset_baselines() # counters moved while we slept
|
|
103
|
+
self._suspended = False
|
|
104
|
+
|
|
105
|
+
# -- one poll ---------------------------------------------------------------------------
|
|
106
|
+
|
|
107
|
+
async def tick(self) -> TickResult | None:
|
|
108
|
+
"""One poll. ``None`` while suspended."""
|
|
109
|
+
if self._suspended:
|
|
110
|
+
return None
|
|
111
|
+
snapshot = self._source.collect()
|
|
112
|
+
self._store.events.observe(self._clock(), 3 * self._interval)
|
|
113
|
+
step = self._engine.evaluate(snapshot.signals)
|
|
114
|
+
self._store.record(snapshot.metrics, step) # the signals reached it via the journal
|
|
115
|
+
rules = {rule.id: rule for rule in self._engine.rules}
|
|
116
|
+
transitions = self._router.route(step.transitions, rules, defer_delivery=True)
|
|
117
|
+
released = self._router.poll(
|
|
118
|
+
self._clock(), rules, defer_delivery=True, skip=transitions.delivered
|
|
119
|
+
)
|
|
120
|
+
candidates = transitions.delivered + released.delivered
|
|
121
|
+
delivered: list[Notification] = []
|
|
122
|
+
self._notifiers.discard_closed(self._router.open_alerts())
|
|
123
|
+
if candidates:
|
|
124
|
+
try:
|
|
125
|
+
async with asyncio.timeout(self._notify_timeout):
|
|
126
|
+
for note in candidates:
|
|
127
|
+
if await self._notifiers.send([note]):
|
|
128
|
+
self._router.confirm_delivery(note, at=self._clock())
|
|
129
|
+
delivered.append(note)
|
|
130
|
+
except TimeoutError:
|
|
131
|
+
log.warning("notifiers took longer than %s s; moving on", self._notify_timeout)
|
|
132
|
+
routed = Routed(
|
|
133
|
+
tuple(delivered),
|
|
134
|
+
transitions.held
|
|
135
|
+
+ released.held
|
|
136
|
+
+ tuple(Held(n, "delivery failed") for n in candidates if n not in delivered),
|
|
137
|
+
)
|
|
138
|
+
self._store.events.record(routed)
|
|
139
|
+
self._store.events.sample(step.evaluations, rules, snapshot.units)
|
|
140
|
+
if self._save_router is not None:
|
|
141
|
+
self._save_router(self._router.checkpoint())
|
|
142
|
+
result = TickResult(snapshot, step, routed, self._muted() if self._muted else None)
|
|
143
|
+
if self._on_tick is not None:
|
|
144
|
+
self._on_tick(result)
|
|
145
|
+
return result
|
|
146
|
+
|
|
147
|
+
# -- the loop ---------------------------------------------------------------------------
|
|
148
|
+
|
|
149
|
+
async def run(self, stop: asyncio.Event) -> None:
|
|
150
|
+
"""Poll every ``interval`` seconds until ``stop`` is set, then flush the store.
|
|
151
|
+
|
|
152
|
+
Ticks are scheduled against a deadline, so slow polls don't drift. A poll that overruns
|
|
153
|
+
its slot is not made up for with a burst: the schedule restarts from now.
|
|
154
|
+
"""
|
|
155
|
+
deadline = self._clock()
|
|
156
|
+
failures = 0
|
|
157
|
+
try:
|
|
158
|
+
while not stop.is_set():
|
|
159
|
+
if self._on_loop is not None:
|
|
160
|
+
self._on_loop()
|
|
161
|
+
try:
|
|
162
|
+
await self.tick()
|
|
163
|
+
failures = 0
|
|
164
|
+
except Exception:
|
|
165
|
+
failures += 1
|
|
166
|
+
if failures % LOG_EVERY == 1:
|
|
167
|
+
log.exception("poll failed (%d in a row)", failures)
|
|
168
|
+
deadline += self._interval
|
|
169
|
+
now = self._clock()
|
|
170
|
+
if deadline < now:
|
|
171
|
+
deadline = now
|
|
172
|
+
await self._wait(max(deadline - now, 0.0), stop)
|
|
173
|
+
finally:
|
|
174
|
+
self._store.flush()
|
|
175
|
+
|
|
176
|
+
async def _wait(self, delay: float, stop: asyncio.Event) -> None:
|
|
177
|
+
if self._sleep is not None:
|
|
178
|
+
await self._sleep(delay)
|
|
179
|
+
return
|
|
180
|
+
with contextlib.suppress(TimeoutError): # the interval passed: poll again
|
|
181
|
+
await asyncio.wait_for(stop.wait(), delay)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|