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.
Files changed (95) hide show
  1. sensitivebox/__init__.py +10 -0
  2. sensitivebox/alerts/__init__.py +94 -0
  3. sensitivebox/alerts/config.py +85 -0
  4. sensitivebox/alerts/defaults.toml +126 -0
  5. sensitivebox/alerts/engine.py +624 -0
  6. sensitivebox/alerts/events.py +87 -0
  7. sensitivebox/alerts/globs.py +84 -0
  8. sensitivebox/alerts/model.py +215 -0
  9. sensitivebox/alerts/mute.py +55 -0
  10. sensitivebox/alerts/replay.py +99 -0
  11. sensitivebox/alerts/routing.py +247 -0
  12. sensitivebox/api/__init__.py +6 -0
  13. sensitivebox/api/app.py +225 -0
  14. sensitivebox/api/data.py +109 -0
  15. sensitivebox/api/host.py +27 -0
  16. sensitivebox/api/model.py +90 -0
  17. sensitivebox/api/security.py +89 -0
  18. sensitivebox/api/server.py +108 -0
  19. sensitivebox/api/state.py +220 -0
  20. sensitivebox/api/static/assets/archivo-latin-wdth-normal-DY7AcnAa.woff2 +0 -0
  21. sensitivebox/api/static/assets/ibm-plex-mono-medium-BpyVxOr_.woff2 +0 -0
  22. sensitivebox/api/static/assets/index-BH9EydfS.js +10 -0
  23. sensitivebox/api/static/assets/index-RMruxW54.css +1 -0
  24. sensitivebox/api/static/assets/mark-on-dark-CpSxozdM.png +0 -0
  25. sensitivebox/api/static/assets/mark-on-light-DgUAeHTp.png +0 -0
  26. sensitivebox/api/static/assets/mark-raised-Bg5z6PGs.png +0 -0
  27. sensitivebox/api/static/index.html +14 -0
  28. sensitivebox/browser.py +106 -0
  29. sensitivebox/cli.py +467 -0
  30. sensitivebox/collector.py +319 -0
  31. sensitivebox/completion.py +117 -0
  32. sensitivebox/daemon.py +181 -0
  33. sensitivebox/data/icons/128.png +0 -0
  34. sensitivebox/data/icons/256.png +0 -0
  35. sensitivebox/data/icons/512.png +0 -0
  36. sensitivebox/data/proart-p16.tar.gz +0 -0
  37. sensitivebox/data/sensitivebox.desktop.in +8 -0
  38. sensitivebox/data/sensitivebox.service.in +11 -0
  39. sensitivebox/doctor.py +322 -0
  40. sensitivebox/fixtures.py +177 -0
  41. sensitivebox/history.py +298 -0
  42. sensitivebox/hwmon/__init__.py +20 -0
  43. sensitivebox/hwmon/classify.py +74 -0
  44. sensitivebox/hwmon/discovery.py +139 -0
  45. sensitivebox/hwmon/ids.py +35 -0
  46. sensitivebox/hwmon/model.py +78 -0
  47. sensitivebox/hwmon/normalise.py +42 -0
  48. sensitivebox/hwmon/reader.py +65 -0
  49. sensitivebox/inspect.py +175 -0
  50. sensitivebox/live.py +56 -0
  51. sensitivebox/notify/__init__.py +7 -0
  52. sensitivebox/notify/base.py +60 -0
  53. sensitivebox/notify/desktop.py +84 -0
  54. sensitivebox/notify/log.py +30 -0
  55. sensitivebox/operations.py +44 -0
  56. sensitivebox/output.py +76 -0
  57. sensitivebox/process.py +25 -0
  58. sensitivebox/py.typed +0 -0
  59. sensitivebox/redact.py +85 -0
  60. sensitivebox/report.py +357 -0
  61. sensitivebox/runtime.py +313 -0
  62. sensitivebox/sample.py +84 -0
  63. sensitivebox/service.py +112 -0
  64. sensitivebox/simulate.py +220 -0
  65. sensitivebox/sources/__init__.py +83 -0
  66. sensitivebox/sources/apps.py +135 -0
  67. sensitivebox/sources/bluez.py +96 -0
  68. sensitivebox/sources/bluez_watch.py +214 -0
  69. sensitivebox/sources/changes.py +121 -0
  70. sensitivebox/sources/cpu.py +152 -0
  71. sensitivebox/sources/dbus.py +174 -0
  72. sensitivebox/sources/logind.py +88 -0
  73. sensitivebox/sources/memory.py +127 -0
  74. sensitivebox/sources/net.py +128 -0
  75. sensitivebox/sources/power_profiles.py +116 -0
  76. sensitivebox/sources/power_supply.py +70 -0
  77. sensitivebox/sources/psi.py +78 -0
  78. sensitivebox/sources/report.py +85 -0
  79. sensitivebox/sources/wifi.py +90 -0
  80. sensitivebox/sources/wifi_nm.py +269 -0
  81. sensitivebox/status.py +205 -0
  82. sensitivebox/store/__init__.py +5 -0
  83. sensitivebox/store/changes.py +58 -0
  84. sensitivebox/store/db.py +423 -0
  85. sensitivebox/store/events.py +337 -0
  86. sensitivebox/store/mutes.py +58 -0
  87. sensitivebox/store/quality.py +51 -0
  88. sensitivebox/store/read.py +40 -0
  89. sensitivebox/store/schema.py +194 -0
  90. sensitivebox/store/status.py +105 -0
  91. sensitivebox-0.1.0.dist-info/METADATA +424 -0
  92. sensitivebox-0.1.0.dist-info/RECORD +95 -0
  93. sensitivebox-0.1.0.dist-info/WHEEL +4 -0
  94. sensitivebox-0.1.0.dist-info/entry_points.txt +4 -0
  95. sensitivebox-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,10 @@
1
+ """Sensitive Box: a read-only Linux hwmon sensor daemon with alerting and a local web UI."""
2
+
3
+ from importlib.metadata import PackageNotFoundError, version
4
+
5
+ try:
6
+ __version__ = version("sensitivebox")
7
+ except PackageNotFoundError: # running from a source tree that was never installed
8
+ __version__ = "0.0.0+unknown"
9
+
10
+ __all__ = ["__version__"]
@@ -0,0 +1,94 @@
1
+ """The alert engine: rules, states, missing data, sleep awareness, replay. See docs/BIBLE.md."""
2
+
3
+ from sensitivebox.alerts.config import (
4
+ RuleError,
5
+ load_defaults,
6
+ load_rule_set,
7
+ load_rules,
8
+ load_rules_file,
9
+ parse_rules,
10
+ )
11
+ from sensitivebox.alerts.engine import (
12
+ CLOSED_NO_DATA,
13
+ RECOVERED,
14
+ Clock,
15
+ Engine,
16
+ Evaluation,
17
+ ManualClock,
18
+ Step,
19
+ Transition,
20
+ TransitionKind,
21
+ format_duration,
22
+ )
23
+ from sensitivebox.alerts.events import (
24
+ EngineSettings,
25
+ InputEvent,
26
+ Journal,
27
+ LimitsChanged,
28
+ Resume,
29
+ RulesLoaded,
30
+ Suspend,
31
+ Tick,
32
+ )
33
+ from sensitivebox.alerts.model import (
34
+ Missing,
35
+ OnlyIf,
36
+ Rule,
37
+ State,
38
+ Threshold,
39
+ Value,
40
+ parse_duration,
41
+ )
42
+ from sensitivebox.alerts.replay import ReplayResult, replay
43
+ from sensitivebox.alerts.routing import (
44
+ Held,
45
+ Mute,
46
+ MuteDecision,
47
+ Notification,
48
+ PassThroughMute,
49
+ Routed,
50
+ Router,
51
+ )
52
+
53
+ __all__ = [
54
+ "CLOSED_NO_DATA",
55
+ "RECOVERED",
56
+ "Clock",
57
+ "Engine",
58
+ "EngineSettings",
59
+ "Evaluation",
60
+ "Held",
61
+ "InputEvent",
62
+ "Journal",
63
+ "LimitsChanged",
64
+ "ManualClock",
65
+ "Missing",
66
+ "Mute",
67
+ "MuteDecision",
68
+ "Notification",
69
+ "OnlyIf",
70
+ "PassThroughMute",
71
+ "ReplayResult",
72
+ "Resume",
73
+ "Routed",
74
+ "Router",
75
+ "Rule",
76
+ "RuleError",
77
+ "RulesLoaded",
78
+ "State",
79
+ "Step",
80
+ "Suspend",
81
+ "Threshold",
82
+ "Tick",
83
+ "Transition",
84
+ "TransitionKind",
85
+ "Value",
86
+ "format_duration",
87
+ "load_defaults",
88
+ "load_rule_set",
89
+ "load_rules",
90
+ "load_rules_file",
91
+ "parse_duration",
92
+ "parse_rules",
93
+ "replay",
94
+ ]
@@ -0,0 +1,85 @@
1
+ """Load rules from TOML: the user's ``rules.toml`` and the shipped ``defaults.toml``."""
2
+
3
+ import tomllib
4
+ from collections.abc import Sequence
5
+ from importlib import resources
6
+ from pathlib import Path
7
+ from typing import Any, cast
8
+
9
+ import msgspec
10
+
11
+ from sensitivebox.alerts.model import Rule
12
+
13
+
14
+ class RuleError(ValueError):
15
+ """A rules file that can't be used, with the rule it came from in the message."""
16
+
17
+
18
+ def parse_rules(data: Any) -> list[Rule]:
19
+ """Validate an already-parsed ``{"rule": [...]}`` mapping."""
20
+ if not isinstance(data, dict):
21
+ raise RuleError("expected a table with [[rule]] entries")
22
+ table = cast(dict[str, Any], data)
23
+ for key in table:
24
+ if key != "rule":
25
+ hint = " (did you mean [[rule]]?)" if key.rstrip("s") == "rule" else ""
26
+ raise RuleError(f"unknown top-level key {key!r}{hint}: a rules file has only [[rule]]")
27
+ raw: Any = table.get("rule", [])
28
+ if not isinstance(raw, list):
29
+ raise RuleError("`rule` must be an array of tables: write [[rule]]")
30
+ rules: list[Rule] = []
31
+ for n, entry in enumerate(raw, start=1): # pyright: ignore[reportUnknownVariableType, reportUnknownArgumentType]
32
+ label = f"rule {n}"
33
+ if isinstance(entry, dict) and isinstance(entry.get("id"), str): # pyright: ignore[reportUnknownMemberType]
34
+ label += f" ({entry['id']})"
35
+ try:
36
+ rules.append(msgspec.convert(entry, Rule))
37
+ except msgspec.ValidationError as exc:
38
+ raise RuleError(f"{label}: {exc}") from None
39
+ _check_unique(rules)
40
+ return rules
41
+
42
+
43
+ def _check_unique(rules: Sequence[Rule]) -> None:
44
+ seen: set[str] = set()
45
+ for rule in rules:
46
+ if rule.id in seen:
47
+ raise RuleError(f"duplicate rule id {rule.id!r}")
48
+ seen.add(rule.id)
49
+
50
+
51
+ def load_rules(text: str) -> list[Rule]:
52
+ try:
53
+ data = tomllib.loads(text)
54
+ except tomllib.TOMLDecodeError as exc:
55
+ raise RuleError(f"not valid TOML: {exc}") from None
56
+ return parse_rules(data)
57
+
58
+
59
+ def load_rules_file(path: Path) -> list[Rule]:
60
+ try:
61
+ return load_rules(path.read_text(encoding="utf-8"))
62
+ except RuleError as exc:
63
+ raise RuleError(f"{path}: {exc}") from None
64
+
65
+
66
+ def load_defaults() -> list[Rule]:
67
+ """The default rules shipped inside the package."""
68
+ text = resources.files("sensitivebox.alerts").joinpath("defaults.toml").read_text("utf-8")
69
+ return load_rules(text)
70
+
71
+
72
+ def load_rule_set(user_file: Path | None = None) -> list[Rule]:
73
+ """The default rules, overridden by id from the user's ``rules.toml`` when it exists.
74
+
75
+ A rule in the user's file with the same ``id`` replaces the default; a new ``id`` is added.
76
+ A missing file is fine; one that can't be read or parsed raises :class:`RuleError`.
77
+ """
78
+ rules = {rule.id: rule for rule in load_defaults()}
79
+ if user_file is not None and user_file.is_file():
80
+ try:
81
+ mine = load_rules_file(user_file)
82
+ except OSError as exc:
83
+ raise RuleError(f"{user_file}: {exc.strerror or exc}") from None
84
+ rules.update({rule.id: rule for rule in mine})
85
+ return list(rules.values())
@@ -0,0 +1,126 @@
1
+ # Default rules shipped with Sensitive Box (docs/BIBLE.md, "Alerting" → "Default rules").
2
+ #
3
+ # Signals are hwmon sensor IDs (<chip>/<bus id>/<channel>) or system signals that the daemon
4
+ # publishes (Prompt 3):
5
+ # psi/memory/some_avg10, psi/memory/full_avg10 PSI, %
6
+ # memory/available_pct MemAvailable / MemTotal, %
7
+ # vmstat/oom_kill_delta OOM kills since the previous poll
8
+ # power_supply/<name>/capacity, …/status battery %, "Charging"/"Discharging"/…
9
+ # power_supply/<ucsi-source-psy-…>/power_w negotiated USB-C power; only for online
10
+ # ports, so an empty port is MISSING, not 0 W
11
+ # Copy a rule into ~/.config/sensitivebox/rules.toml with the same id to override it.
12
+
13
+ [[rule]]
14
+ id = "cpu-hot"
15
+ name = "CPU hot"
16
+ sensor = ["k10temp/*/temp1", "coretemp/*/temp1"]
17
+ when = "above"
18
+ value = 90.0
19
+ for = "10s"
20
+ clear = 85.0
21
+ on_missing = "alert" # a vanished CPU sensor is worth knowing about, at info severity
22
+ severity = "critical"
23
+
24
+ [[rule]]
25
+ id = "gpu-hot"
26
+ name = "GPU hot"
27
+ sensor = "amdgpu/*/temp1" # edge
28
+ when = "above"
29
+ value = 85.0
30
+ for = "10s"
31
+ clear = 80.0
32
+ severity = "warning"
33
+
34
+ [[rule]]
35
+ id = "nvme-near-limit"
36
+ name = "NVMe near limit"
37
+ sensor = "nvme/*/temp1" # Composite
38
+ when = "above"
39
+ value = "max-5" # the drive's own temp1_max
40
+ for = "30s"
41
+ clear = "max-10"
42
+ severity = "warning"
43
+
44
+ [[rule]]
45
+ id = "wifi-chip-hot"
46
+ name = "Wi-Fi chip hot"
47
+ sensor = ["mt79*/*/temp1", "iwlwifi*/*/temp1"]
48
+ when = "above"
49
+ value = 75.0
50
+ for = "60s"
51
+ clear = 70.0
52
+ severity = "info"
53
+
54
+ # TODO(v1.1, compound rules): "Fan stalled": any fan = 0 RPM while CPU > 70 °C for 15 s,
55
+ # mode = "any", critical. A fan at 0 RPM on its own is a normal idle stop.
56
+
57
+ [[rule]]
58
+ id = "memory-pressure"
59
+ name = "Memory pressure"
60
+ sensor = "psi/memory/some_avg10"
61
+ when = "above"
62
+ value = 20.0
63
+ for = "30s"
64
+ clear = 10.0
65
+ severity = "warning"
66
+
67
+ [[rule]]
68
+ id = "memory-stall"
69
+ name = "Memory stall"
70
+ sensor = "psi/memory/full_avg10"
71
+ when = "above"
72
+ value = 5.0
73
+ for = "10s"
74
+ clear = 2.0
75
+ severity = "critical"
76
+
77
+ [[rule]]
78
+ id = "low-memory"
79
+ name = "Low memory"
80
+ sensor = "memory/available_pct"
81
+ when = "below"
82
+ value = 10.0
83
+ for = "30s"
84
+ clear = 15.0
85
+ severity = "warning"
86
+
87
+ [[rule]]
88
+ id = "oom-kill"
89
+ name = "App killed (OOM)"
90
+ sensor = "vmstat/oom_kill_delta"
91
+ when = "above"
92
+ value = 0
93
+ mode = "any"
94
+ on_missing = "close"
95
+ severity = "critical"
96
+
97
+ # "Data used today" is off by default. To enable it, add to rules.toml, e.g.:
98
+ # [[rule]]
99
+ # id = "data-today"
100
+ # name = "Data used today"
101
+ # sensor = "net/*/rx_today_gb"
102
+ # when = "above"
103
+ # value = 20.0
104
+ # severity = "info"
105
+
106
+ [[rule]]
107
+ id = "battery-low"
108
+ name = "Battery low"
109
+ sensor = "power_supply/{battery}/capacity" # {battery} binds the guard to the same battery
110
+ when = "below"
111
+ value = 15.0
112
+ clear = 20.0
113
+ on_missing = "close"
114
+ severity = "warning"
115
+ only_if = { signal = "power_supply/{battery}/status", equals = "Discharging" }
116
+
117
+ [[rule]]
118
+ id = "slow-charger"
119
+ name = "Slow charger"
120
+ sensor = "power_supply/ucsi-source-psy-*/power_w"
121
+ when = "below"
122
+ value = 15.0 # below 15 W; the trigger is strict, so exactly 15 W does not fire
123
+ for = "30s"
124
+ on_missing = "close"
125
+ severity = "info"
126
+ only_if = { signal = "power_supply/BAT*/status", equals = "Charging" }