with shutting down plex and raspberry if runtime falls below 300 and 200 secs

This commit is contained in:
2026-08-10 21:19:06 +02:00
parent e75c3c2358
commit a5b324bf66
3 changed files with 200 additions and 1 deletions
+69
View File
@@ -829,12 +829,81 @@ The collector also sends an ntfy notification when:
- Battery charge crosses below 50%
- Battery charge crosses below 5%
- Estimated battery runtime crosses below 5 minutes
- Local Raspberry Pi shutdown is requested below 200 seconds of runtime
Alert state is stored in `/var/lib/rrd/bx950mi-alert-state.json`, allowing
changes to be detected across cron runs. The first run establishes a baseline
without sending notifications. Threshold notifications can fire again after a
value recovers above the threshold and later drops below it.
### Automatic shutdown of the Plex server
When the UPS status is `OB` or `LB` and estimated runtime is below five
minutes, the collector requests a shutdown of `plex.local` over SSH as user
`ups`. The request is sent once per outage. A failed SSH request is retried by
the next cron run, while a return to `OL` resets the shutdown state.
Generate a dedicated key on the UPS monitor:
```bash
sudo install -d -m 700 /opt/ups/.ssh
sudo ssh-keygen -t ed25519 -f /opt/ups/.ssh/plex_shutdown -N ""
sudo ssh-keyscan -H plex.local | sudo tee /opt/ups/.ssh/known_hosts
sudo chmod 600 /opt/ups/.ssh/plex_shutdown /opt/ups/.ssh/known_hosts
```
Verify the scanned SSH host-key fingerprint against `plex.local` through a
trusted channel before enabling automatic shutdown.
On `plex.local`, create the restricted account and authorize shutdown:
```bash
sudo useradd --create-home --shell /bin/bash ups
sudo visudo -f /etc/sudoers.d/ups-shutdown
```
Add this exact sudoers rule:
```sudoers
ups ALL=(root) NOPASSWD: /usr/sbin/shutdown -h now
```
Add the public key from `/opt/ups/.ssh/plex_shutdown.pub` to
`/home/ups/.ssh/authorized_keys` on `plex.local`, prefixed with a forced command
and SSH restrictions:
```text
restrict,command="sudo /usr/sbin/shutdown -h now" ssh-ed25519 AAAA... ups-shutdown
```
Verify SSH before relying on the automatic action:
```bash
sudo ssh -i /opt/ups/.ssh/plex_shutdown \
-o UserKnownHostsFile=/opt/ups/.ssh/known_hosts ups@plex.local
```
This test will shut down `plex.local`. The hostname, user, key, and known-hosts
file can be overridden for the cron job with:
```text
UPS_SHUTDOWN_HOST
UPS_SHUTDOWN_USER
UPS_SHUTDOWN_KEY
UPS_SHUTDOWN_KNOWN_HOSTS
```
### Automatic shutdown of the Raspberry Pi
When UPS status is `OB` or `LB` and estimated runtime drops below 200 seconds,
the collector sends an ntfy warning and requests shutdown of the Raspberry Pi
running the script. It calls systemd-logind through D-Bus using the `dbus-next`
Python package; it does not invoke a shell command.
Because the collector runs from root's crontab, systemd-logind permits the
power-off request. A failed request is retried by the next cron run, and the
shutdown state resets after the UPS returns to `OL`.
---
# 19. Testing the Python program