Add kernelwatch status [--json]
CI / tests (Python 3.10) (pull_request) Successful in 25s
CI / tests (Python 3.11) (pull_request) Successful in 24s
CI / tests (Python 3.12) (pull_request) Successful in 24s
CI / tests (Python 3.13) (pull_request) Successful in 25s
CI / lint (pull_request) Successful in 4s
CI / tests (Python 3.10) (pull_request) Successful in 25s
CI / tests (Python 3.11) (pull_request) Successful in 24s
CI / tests (Python 3.12) (pull_request) Successful in 24s
CI / tests (Python 3.13) (pull_request) Successful in 25s
CI / lint (pull_request) Successful in 4s
Prints the fleet like the dashboard, worst hosts first, from the same evaluate() call, so the CLI and the dashboard cannot disagree. --json gives generated_at, whole-fleet counts and a stable per-host object with no secrets; --host and --level (repeatable) trim the host list. No network surface. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9d80d3ea4c
commit
63b9151b09
4 files changed
+295
No files matched your search
@@ -10,6 +10,7 @@ Levels, worst first:
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import date, datetime, timezone
|
||||
from typing import Any
|
||||
@@ -208,3 +209,49 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float,
|
||||
"run agent/deploy.sh")
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def _iso(ts: float | None) -> str | None:
|
||||
return None if ts is None else time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(ts))
|
||||
|
||||
|
||||
def fleet_status(rows: list[dict[str, Any]], now: float, cfg: Config) -> dict[str, Any]:
|
||||
"""The whole fleet as plain data, worst hosts first (as the dashboard orders them).
|
||||
|
||||
`rows` are db.latest_reports() entries. A stable, secret-free shape for scripts:
|
||||
status and reasons come from evaluate(), the same call the dashboard makes."""
|
||||
hosts = []
|
||||
for row in rows:
|
||||
report = row["report"] or {}
|
||||
status = evaluate(row["received_at"], row["report"], now, cfg, row["host"])
|
||||
livepatch = None
|
||||
if report.get("livepatch_state") is not None:
|
||||
livepatch = {"state": report["livepatch_state"],
|
||||
"supported": report.get("livepatch_supported"),
|
||||
"upgrade_required": report.get("livepatch_upgrade_required"),
|
||||
"fixes": report.get("livepatch_fix_count")}
|
||||
hosts.append({
|
||||
"name": row["host"],
|
||||
"status": status.level,
|
||||
"reasons": status.reasons,
|
||||
"last_report_at": _iso(row["received_at"]),
|
||||
"last_report_age_seconds": None if row["received_at"] is None else max(0, int(now - row["received_at"])),
|
||||
"os": report.get("os_pretty_name"),
|
||||
"kernel_running": report.get("kernel_running"),
|
||||
"kernel_newest_installed": report.get("kernel_newest_installed"),
|
||||
"reboot_required": report.get("reboot_required"),
|
||||
"updates_pending": report.get("upgradable_count"),
|
||||
"security_updates": report.get("security_count"),
|
||||
"kernel_updates": report.get("kernel_upgradable"),
|
||||
"services_need_restart": report.get("services_need_restart"),
|
||||
"livepatch": livepatch,
|
||||
"uptime_seconds": report.get("uptime_seconds"),
|
||||
"agent_version": report.get("agent_version"),
|
||||
"_rank": status.rank,
|
||||
})
|
||||
hosts.sort(key=lambda h: (h["_rank"], h["name"]))
|
||||
counts = {level: 0 for level in LEVELS}
|
||||
for h in hosts:
|
||||
counts[h["status"]] += 1
|
||||
del h["_rank"]
|
||||
return {"generated_at": _iso(now), "counts": counts, "hosts": hosts}
|
||||
Reference in new issue
Block a user