diff --git a/README.md b/README.md index a4e5d36..acad763 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Base: `http://127.0.0.1:9109`. Errors are `{"error":{"code","message"}}`. |---|---| | `GET /healthz` | public, `{"status":"ok","version":...}` | | `GET /api/v1/systemd/units?state=failed` | `state` is an active state (`failed`, `active`, ...) | -| `GET /api/v1/systemd/units/{name}` | one unit with details | +| `GET /api/v1/systemd/units/{name}` | one unit with details; read-only, so only units systemd has loaded (unloaded or unknown is 404) | | `GET /api/v1/systemd/timers` | | | `GET /api/v1/journal?unit=&priority=err&lines=100` | priority by name or number | | `GET /api/v1/journal/follow?unit=&priority=` | SSE, event `journal` | diff --git a/TODO.md b/TODO.md index c217055..4d1f010 100644 --- a/TODO.md +++ b/TODO.md @@ -30,7 +30,7 @@ Design: [`plan.md`](plan.md) · Review fixes: [`plan/review.md`](plan/review.md) - [x] **A1** Shared system-bus `zbus::Connection` + `ManagerProxy` - [x] **A2** `list_units(state)` mapped to `UnitSummary` - [x] **A3** `list_timers()` via `*.timer` units + `TimerProxy` props (no `ListTimers` method exists) -- [x] **A4** `unit_status(name)`: `GetUnit` first, `LoadUnit` as fallback. `Unit` props always, `Service` props for `.service` only. +- [x] **A4** `unit_status(name)`: `GetUnit` only (read-only; `LoadUnit` is never called, an unloaded unit is NotFound). `Unit` props always, `Service` props for `.service` only. - [x] **A5** Unit-name validation. `NoSuchUnit` maps to `NotFound`. - [x] **A6** `examples/systemd_dump.rs` prints JSON from the live bus - [x] **A7** Unit tests for timestamp conversion and property mapping diff --git a/plan.md b/plan.md index 03bda5b..d1bdfa9 100644 --- a/plan.md +++ b/plan.md @@ -98,7 +98,7 @@ Precedence: built-in defaults < `--config /etc/skyg-linux-tracker.toml` < `SKYG_ ### Sources (live reads only) -- **systemd** (`sources/systemd.rs`): one shared `zbus::Connection` to the system bus. `list_units()` → `ManagerProxy::list_units()`; `list_timers()` → `list_timers()` (next-elapse realtime converted to RFC3339); `unit_status(name)` → `load_unit` + properties (`ActiveState`, `SubState`, `ExecMainStatus`, `NRestarts`, `ActiveEnterTimestamp`). No caching — every call hits D-Bus. +- **systemd** (`sources/systemd.rs`): one shared `zbus::Connection` to the system bus. `list_units()` → `ManagerProxy::list_units()`; `list_timers()` → `list_timers()` (next-elapse realtime converted to RFC3339); `unit_status(name)` → `get_unit` (read-only, never `load_unit`) + properties (`ActiveState`, `SubState`, `ExecMainStatus`, `NRestarts`, `ActiveEnterTimestamp`). No caching — every call hits D-Bus. - **journal** (`sources/journal.rs`): `Journal::open()`, `match_add("_SYSTEMD_UNIT", …)`, `seek_tail()`. Snapshot queries seek backward N entries. `follow()` wraps `Journal::wait()` in a `tokio::task::spawn_blocking` bridge (sd-journal is blocking) feeding a `tokio::sync::mpsc` — consumers (SSE endpoint, MCP tool, OTel log exporter) all read from channels. - **docker** (`sources/docker.rs`): shared `bollard::Docker` over the unix socket. `ps()` → `list_containers` + `inspect_container` per container for `State.Health`, `StartedAt` (→ uptime), restart count. `logs(name, follow)` → `bollard` log stream (`stdout`, `stderr`, `timestamps`, `since`) as a `Stream`. diff --git a/src/mcp/mod.rs b/src/mcp/mod.rs index 62a248f..15aa6af 100644 --- a/src/mcp/mod.rs +++ b/src/mcp/mod.rs @@ -205,7 +205,7 @@ impl TrackerService { } #[tool( - description = "Detailed status of one systemd unit: states, unit file path, enter/exit times and, for services, main_pid, exec_main_status, n_restarts (automatic restarts) and result (`success`, `exit-code`, `signal`, `timeout`). Unknown unit is an error." + description = "Detailed status of one systemd unit: states, unit file path, enter/exit times and, for services, main_pid, exec_main_status, n_restarts (automatic restarts) and result (`success`, `exit-code`, `signal`, `timeout`). Read-only: only units systemd currently has loaded are visible; an unloaded or unknown unit is a not-found error." )] async fn get_systemd_unit( &self, diff --git a/src/sources/fake.rs b/src/sources/fake.rs index 8e83084..33fc53d 100644 --- a/src/sources/fake.rs +++ b/src/sources/fake.rs @@ -120,7 +120,7 @@ impl SystemdSource for FakeSystemd { .iter() .find(|u| u.name == name) .cloned() - .ok_or_else(|| AppError::NotFound(format!("unit {name}"))) + .ok_or_else(|| AppError::NotFound(format!("unit not loaded or unknown: {name}"))) } } diff --git a/src/sources/mod.rs b/src/sources/mod.rs index 6e0f4e0..d1b25b1 100644 --- a/src/sources/mod.rs +++ b/src/sources/mod.rs @@ -83,7 +83,8 @@ pub trait SystemdSource: Send + Sync { async fn list_units(&self, state: Option<&str>) -> Result, AppError>; /// List `.timer` units with their schedule. async fn list_timers(&self) -> Result, AppError>; - /// Status of one unit. Unknown unit is `AppError::NotFound`. + /// Status of one unit. Read-only: never asks systemd to load a unit, so a + /// unit that is not loaded (or unknown) is `AppError::NotFound`. async fn unit_status(&self, name: &str) -> Result; } diff --git a/src/sources/systemd.rs b/src/sources/systemd.rs index c72a09e..ab4a15a 100644 --- a/src/sources/systemd.rs +++ b/src/sources/systemd.rs @@ -286,18 +286,20 @@ impl SystemdSource for DbusSystemd { Ok(out) } - /// Note: for a name that is not loaded this calls `LoadUnit`, which asks - /// systemd to load the unit (a side effect, though it does not start it). + /// Read-only: uses `GetUnit`, so only units systemd already has in memory + /// are visible. `LoadUnit` is never called (it would load arbitrary + /// installed units); an unloaded unit is reported as not found. async fn unit_status(&self, name: &str) -> Result { validate_unit_name(name)?; let conn = self.conn().await?; let mgr = self.manager().await?; let path = match mgr.get_unit(name.to_string()).await { Ok(p) => p, - Err(e) if is_no_such_unit(&e) => mgr - .load_unit(name.to_string()) - .await - .map_err(|e| self.err(e))?, + Err(e) if is_no_such_unit(&e) => { + return Err(AppError::NotFound(format!( + "unit not loaded or unknown: {name}" + ))) + } Err(e) => return Err(self.err(e)), }; let unit = UnitProxy::builder(&conn)