Files
FabledSteward/plugins/docker/scheduler.py
T
bvandeusen af60ca446d
CI / lint (push) Successful in 3s
CI / unit (push) Successful in 7s
CI / integration (push) Successful in 2m8s
fix(plugins): complete steward rename across bundled plugins; clear lint debt
The fabledscryer->steward rename had only ever reached host_agent. The other
five bundled plugins (http, snmp, traefik, unifi, docker) still imported
`from fabledscryer.*` (package no longer exists) and read FABLEDSCRYER_* env
vars — so every one of them was broken at import since the original rebrand.
CI stayed green only because none are enabled by default and migrations don't
import plugin modules. Now that they version in-tree, complete the rename:
- fabledscryer.* -> steward.* imports across all five plugins
- FABLEDSCRYER_* -> STEWARD_* in plugin migration env.py files
- author/repository/homepage + user-facing 'Fabled Scryer' strings -> Steward
- snmp/scheduler.py: also drop dead `now`/datetime; record_metric from steward

Adds tests/test_no_legacy_names.py — fails if 'scryer'/'roundtable' ever
reappear in shipped code (the drift bit twice; this stops a third time).

Also clears pre-existing ruff lint debt (unused imports, semicolon statements,
mid-file import) surfaced by the new lint lane.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-01 11:22:20 -04:00

94 lines
3.3 KiB
Python

# plugins/docker/scheduler.py
from __future__ import annotations
import json
import logging
from datetime import datetime, timezone
from steward.core.scheduler import ScheduledTask
from steward.core.alerts import record_metric
logger = logging.getLogger(__name__)
def make_task(app) -> ScheduledTask:
interval = int(
app.config["PLUGINS"]["docker"].get("scrape_interval_seconds", 60)
)
async def scrape():
await _do_scrape(app)
return ScheduledTask(
name="docker_scrape",
coro_factory=scrape,
interval_seconds=interval,
run_on_startup=True,
)
async def _do_scrape(app) -> None:
from .scraper import scrape_docker
from .models import DockerContainer, DockerMetric
cfg = app.config["PLUGINS"]["docker"]
socket_path = cfg.get("socket_path", "/var/run/docker.sock")
include_stopped = bool(cfg.get("include_stopped", False))
try:
containers = await scrape_docker(socket_path, include_stopped)
except ConnectionError as exc:
logger.error("Docker scrape failed: %s", exc)
return
except Exception:
logger.exception("Docker scrape error")
return
now = datetime.now(timezone.utc)
async with app.db_sessionmaker() as session:
async with session.begin():
for c in containers:
# Upsert container state
existing = await session.get(DockerContainer, c["name"])
if existing is None:
existing = DockerContainer(name=c["name"])
session.add(existing)
existing.container_id = c["container_id"]
existing.image = c["image"]
existing.status = c["status"]
existing.cpu_pct = c["cpu_pct"]
existing.mem_usage_bytes = c["mem_usage_bytes"]
existing.mem_limit_bytes = c["mem_limit_bytes"]
existing.mem_pct = c["mem_pct"]
existing.restart_count = c["restart_count"]
existing.ports_json = json.dumps(c["ports"])
existing.started_at = c["started_at"]
existing.scraped_at = now
# Time-series metric (running containers only)
if c["status"] == "running" and c["cpu_pct"] is not None:
session.add(DockerMetric(
container_name=c["name"],
scraped_at=now,
cpu_pct=c["cpu_pct"],
mem_pct=c["mem_pct"] or 0.0,
mem_usage_bytes=c["mem_usage_bytes"] or 0,
))
# Feed alert pipeline
await record_metric(
session=session,
source_module="docker",
resource_name=c["name"],
metric_name="cpu_pct",
value=c["cpu_pct"],
)
if c["mem_pct"] is not None:
await record_metric(
session=session,
source_module="docker",
resource_name=c["name"],
metric_name="mem_pct",
value=c["mem_pct"],
)