Scaffold kernelwatch: fleet kernel status agent and dashboard
Agent: bash script run hourly by a hardened systemd timer on Debian 12/13 and Ubuntu 24.04. Collects running vs installed kernel (needrestart when available), pending/security updates via a read-only apt simulation, reboot-required state and apt list freshness, and POSTs them as JSON. Server: stdlib-only Python + SQLite (system python3 >= 3.11). Token-bound ingest API, login-protected dashboard with per-host status (stale/reboot/update/ok) and history, admin CLI, systemd unit and installer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
07749b01b0
27 files changed
+2406
No files matched your search
@@ -0,0 +1,194 @@
|
||||
# kernelwatch
|
||||
|
||||
A small, self-hosted dashboard that shows whether each machine in a Debian/Ubuntu
|
||||
fleet is running its newest kernel, has updates waiting, or needs a reboot.
|
||||
|
||||
- **Agent**: a bash script run hourly by a systemd timer on each host. It
|
||||
collects facts and POSTs them as JSON. It runs no daemon and listens on no port.
|
||||
- **Server**: a standard-library Python app with SQLite. It has a token-authenticated
|
||||
ingest API and a login-protected dashboard. You don't need pip, a venv or a
|
||||
build step.
|
||||
|
||||
Supported: agents on **Debian 12/13** and **Ubuntu 24.04 LTS**; server on
|
||||
**Debian 12+** or **Ubuntu 24.04** (needs the system `python3` ≥ 3.11).
|
||||
|
||||
```
|
||||
host ──(systemd timer)── kernelwatch-agent ──HTTPS POST /api/v1/report──┐
|
||||
host ──(systemd timer)── kernelwatch-agent ─────────────────────────────┤
|
||||
▼
|
||||
browser ──HTTPS──▶ Caddy/nginx ──▶ kernelwatch (127.0.0.1:8080) ──▶ SQLite
|
||||
```
|
||||
|
||||
## What the dashboard tells you
|
||||
|
||||
Each host gets one status. When several apply, the worst one wins:
|
||||
|
||||
| Status | Meaning |
|
||||
|----------|---------|
|
||||
| `stale` | No report in `stale_after_hours` (default 3h), or never. Other data can't be trusted. |
|
||||
| `reboot` | A newer kernel is installed but not running, or `/run/reboot-required` exists. |
|
||||
| `update` | A kernel or security update is available but not installed, or apt's package lists are too old (`apt_stale_after_hours`) for "no updates" to mean anything. |
|
||||
| `ok` | Running the newest installed kernel, with no security or kernel updates pending. |
|
||||
|
||||
Each row also lists the reasons behind its status, and clicking a host shows
|
||||
its full report and recent history.
|
||||
|
||||
How the agent works it out:
|
||||
|
||||
- **Running vs. installed kernel**: if [`needrestart`](https://github.com/liske/needrestart)
|
||||
is installed, the agent uses `needrestart -b -k`, which also catches
|
||||
ABI-compatible upgrades where the package name stays the same. Otherwise it
|
||||
compares `uname -r` with the newest installed `linux-image-*` package.
|
||||
Ubuntu 24.04 ships needrestart by default. **On Debian, install it**
|
||||
(`apt install needrestart`) for accurate results.
|
||||
- **Available updates**: a simulated `apt-get dist-upgrade`. It is read-only, takes
|
||||
no locks and writes no cache files. `dist-upgrade` is used so that new-ABI kernel
|
||||
packages pulled in by the meta-package show up. Lines whose origin contains
|
||||
`-security` count as security updates.
|
||||
- **Package list freshness**: the mtime of apt's update stamps. The agent
|
||||
**doesn't run `apt update`** itself; it relies on the stock `apt-daily.timer`.
|
||||
If that's disabled, the host shows `update` with "package lists N days old".
|
||||
|
||||
The agent sends raw facts and the server decides what they mean, so you can
|
||||
change the status rules without touching the agents.
|
||||
|
||||
## Server setup
|
||||
|
||||
On the dashboard host:
|
||||
|
||||
```sh
|
||||
git clone <this repo> && cd kernel-automation
|
||||
sudo ./server/install.sh
|
||||
sudo kernelwatch set-password admin # dashboard login
|
||||
```
|
||||
|
||||
This installs to `/opt/kernelwatch` and keeps config in `/etc/kernelwatch/server.toml`
|
||||
(see [`server/server.toml.example`](server/server.toml.example)) and data in
|
||||
`/var/lib/kernelwatch`. The `kernelwatch.service` unit runs it as the unprivileged
|
||||
`kernelwatch` user. Re-run `install.sh` to upgrade; it keeps your config.
|
||||
|
||||
### TLS reverse proxy
|
||||
|
||||
The server speaks plain HTTP on `127.0.0.1:8080`. Put a TLS proxy in front of it.
|
||||
The agents and your browser both connect through it.
|
||||
|
||||
**Caddy** (simplest):
|
||||
|
||||
```caddyfile
|
||||
kernelwatch.example.internal {
|
||||
reverse_proxy 127.0.0.1:8080
|
||||
}
|
||||
```
|
||||
|
||||
**nginx**:
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 443 ssl;
|
||||
server_name kernelwatch.example.internal;
|
||||
# ssl_certificate / ssl_certificate_key ...
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8080;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
With either proxy, set `trust_proxy_headers = true` in `server.toml` so login
|
||||
rate limiting sees real client IPs.
|
||||
|
||||
If you use a private CA (e.g. Caddy's `tls internal`), give the agents its
|
||||
root cert with `--ca` (below).
|
||||
|
||||
You can expose only `/api/v1/report` to the fleet network and keep the
|
||||
dashboard on a VPN or tailnet. That's a proxy rule, not a kernelwatch setting.
|
||||
|
||||
## Adding a host
|
||||
|
||||
On the server:
|
||||
|
||||
```sh
|
||||
sudo kernelwatch add-host web01
|
||||
# prints a token, shown once
|
||||
```
|
||||
|
||||
On the host (copy the `agent/` directory over, or clone the repo):
|
||||
|
||||
```sh
|
||||
sudo ./agent/install.sh --server https://kernelwatch.example.internal [--ca root.pem]
|
||||
# paste the token when prompted
|
||||
```
|
||||
|
||||
The installer puts the agent in `/usr/local/sbin`, writes `/etc/kernelwatch/agent.conf`
|
||||
and `agent.token` (root, 0600), enables `kernelwatch-agent.timer` and sends a first
|
||||
report. After that, the agent reports 2 minutes after boot and then hourly, with up
|
||||
to 15 minutes of jitter.
|
||||
|
||||
For unattended installs, set `KERNELWATCH_TOKEN=...` instead of using the prompt.
|
||||
|
||||
Useful on a host:
|
||||
|
||||
```sh
|
||||
sudo kernelwatch-agent --print # see exactly what would be sent
|
||||
sudo systemctl start kernelwatch-agent # report now
|
||||
systemctl list-timers kernelwatch-agent.timer
|
||||
journalctl -u kernelwatch-agent
|
||||
```
|
||||
|
||||
## Server admin commands
|
||||
|
||||
```
|
||||
sudo kernelwatch add-host NAME register a host, print its token
|
||||
sudo kernelwatch rotate-token NAME issue a new token (old one stops working)
|
||||
sudo kernelwatch remove-host NAME delete a host and its history
|
||||
sudo kernelwatch list-hosts hosts and when they last reported
|
||||
sudo kernelwatch set-password USER create a user / change password (logs out their sessions)
|
||||
sudo kernelwatch remove-user USER
|
||||
sudo kernelwatch list-users
|
||||
```
|
||||
|
||||
## Security notes
|
||||
|
||||
- **Host identity comes from the token, not the payload.** A host can only
|
||||
report as itself. Tokens are 256-bit random values; only their SHA-256 is stored.
|
||||
- **The agent** runs from a systemd unit with an empty capability set, a
|
||||
read-only filesystem view and a syscall filter. It runs as uid 0 only because
|
||||
Ubuntu's `/boot/vmlinuz-*` files are root-only, and needrestart has to read
|
||||
them. The token reaches it through `LoadCredential=` and goes to curl on stdin,
|
||||
so it never appears in a process listing.
|
||||
- **The dashboard** uses scrypt-hashed passwords, server-side sessions,
|
||||
`HttpOnly` + `Secure` + `SameSite=Strict` cookies, an Origin check on form
|
||||
posts, per-IP login rate limiting, and a strict CSP. Everything a host
|
||||
reports is HTML-escaped.
|
||||
- **It's read-only by design.** The server can't make hosts do anything.
|
||||
Adding remote actions (like "reboot this host") would turn it into a
|
||||
remote-execution system; think hard before doing that.
|
||||
|
||||
## Development
|
||||
|
||||
```sh
|
||||
make test # unit + integration tests (stdlib unittest)
|
||||
make lint # shellcheck, if available
|
||||
```
|
||||
|
||||
The tests run a real server on an ephemeral port. They also run the real agent
|
||||
script against stubbed `dpkg-query`/`apt-get`/`needrestart`/`uname` and a fake
|
||||
filesystem root (`KERNELWATCH_ROOT`), including one end-to-end agent → server
|
||||
report.
|
||||
|
||||
Run a local server against a scratch database:
|
||||
|
||||
```sh
|
||||
printf 'db_path = "/tmp/kw.db"\nsecure_cookies = false\n' > /tmp/kw.toml
|
||||
export KERNELWATCH_CONFIG=/tmp/kw.toml
|
||||
./server/kernelwatch-server set-password admin
|
||||
./server/kernelwatch-server serve # http://127.0.0.1:8080
|
||||
```
|
||||
|
||||
## Ideas for later
|
||||
|
||||
- Alerts on status changes (email/ntfy/webhook)
|
||||
- Ubuntu Livepatch / Pro status, so live-patched hosts aren't flagged for reboot
|
||||
- A JSON endpoint for scripting
|
||||
- Grouping or tagging hosts (prod/staging, location)
|
||||
Reference in new issue
Block a user