The Hosts — Overview widget opens with a verdict ("VDNT-Util01: CPU high."
or "All 3 hosts healthy."), then one row per host under one header: CPU,
memory, disk / and load. Rows sort by host_glance: a down monitor, a
reading past critical, a quiet agent, a warning, then the healthy. Load
reads "16.8 / 8 cores", graded per core as Settings → Thresholds intends
(cores counted from the per-core CPU sub-resources, as on the host page).
A quiet agent's last figures are shown uncoloured, with when it last reported.
Ping and 24h uptime are gone from this widget: they belong to Monitors. So
are the unlabelled CPU sparkline and the 30-day uptime query it no longer
needs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
311 lines
13 KiB
Python
311 lines
13 KiB
Python
from __future__ import annotations
|
|
import asyncio
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
from quart import Blueprint, render_template, request, redirect, url_for, current_app, session
|
|
from sqlalchemy import select
|
|
from steward.ansible import executor, sources as ansible_src
|
|
from steward.alerts import service as alert_svc
|
|
from steward.auth.middleware import require_role
|
|
from steward.core import renames
|
|
from steward.core.audit import audit_request, log_audit
|
|
from steward.models.ansible import AnsibleRun, AnsibleRunStatus
|
|
from steward.models.hosts import Host
|
|
from steward.models.users import UserRole
|
|
from steward.hosts import service as host_svc
|
|
|
|
hosts_bp = Blueprint("hosts", __name__, url_prefix="/hosts")
|
|
|
|
|
|
def _ansible_source_names() -> list[str]:
|
|
try:
|
|
return [s["name"] for s in ansible_src.get_sources(current_app.config.get("ANSIBLE", {}))]
|
|
except Exception:
|
|
return []
|
|
|
|
|
|
@hosts_bp.get("/overview/widget")
|
|
@require_role(UserRole.viewer)
|
|
async def overview_widget():
|
|
"""Dashboard widget: each host's resources, worst first (#5251). Uptime and
|
|
latency belong to the Monitors widget, so this one doesn't read them."""
|
|
async with current_app.db_sessionmaker() as db:
|
|
hosts = (await db.execute(select(Host).order_by(Host.name))).scalars().all()
|
|
host_status = await host_svc.host_status(db, [h.id for h in hosts])
|
|
agent = await host_svc.agent_overview_by_host(
|
|
db, [h.name for h in hosts], host_svc.agent_stale_after_seconds(current_app.config))
|
|
rows = host_svc.host_glance(hosts, host_status, agent, current_app.config.get("THRESHOLDS", {}))
|
|
return await render_template("hosts/overview_widget.html", rows=rows)
|
|
|
|
|
|
@hosts_bp.get("/")
|
|
@require_role(UserRole.viewer)
|
|
async def list_hosts():
|
|
async with current_app.db_sessionmaker() as db:
|
|
result = await db.execute(select(Host).order_by(Host.name))
|
|
hosts = result.scalars().all()
|
|
host_status = await host_svc.host_status(db, [h.id for h in hosts])
|
|
uptime = await host_svc.compute_uptime(db)
|
|
agent = await host_svc.agent_overview_by_host(
|
|
db, [h.name for h in hosts], host_svc.agent_stale_after_seconds(current_app.config))
|
|
|
|
hosts = sorted(hosts, key=lambda h: (
|
|
host_svc.host_rank(host_status.get(h.id), agent.get(h.name)), h.name.lower()))
|
|
return await render_template(
|
|
"hosts/list.html",
|
|
hosts=hosts,
|
|
host_status=host_status,
|
|
uptime=uptime,
|
|
agent=agent,
|
|
)
|
|
|
|
|
|
@hosts_bp.get("/<host_id>")
|
|
@require_role(UserRole.viewer)
|
|
async def host_detail(host_id: str):
|
|
"""The host hub: monitors + agent (embedded fragment) + Ansible, one view."""
|
|
from steward.models.ansible_inventory import AnsibleTarget
|
|
from sqlalchemy.orm import selectinload
|
|
|
|
async with current_app.db_sessionmaker() as db:
|
|
host = (await db.execute(
|
|
select(Host).where(Host.id == host_id))).scalar_one_or_none()
|
|
if host is None:
|
|
return "Not found", 404
|
|
monitors = await host_svc.host_monitors(db, host_id)
|
|
uptime = (await host_svc.compute_uptime(db)).get(host_id)
|
|
monitor_keys = {(getattr(d["monitor"].type, "value", d["monitor"].type), d["monitor"].name)
|
|
for d in monitors}
|
|
alerts = [r for r in await alert_svc.active_rows(db, datetime.now(timezone.utc))
|
|
if alert_svc.watches_host(r["rule"], host.name, monitor_keys)]
|
|
linked_target = (await db.execute(
|
|
select(AnsibleTarget).where(AnsibleTarget.host_id == host_id)
|
|
.options(selectinload(AnsibleTarget.groups)))).scalar_one_or_none()
|
|
linkable_targets = (await db.execute(
|
|
select(AnsibleTarget).where(AnsibleTarget.host_id.is_(None))
|
|
.order_by(AnsibleTarget.name))).scalars().all()
|
|
|
|
from steward.models.monitors import MonitorType
|
|
return await render_template(
|
|
"hosts/detail.html",
|
|
host=host, monitors=monitors, uptime=uptime, alerts=alerts,
|
|
monitor_types=list(MonitorType),
|
|
linked_target=linked_target, linkable_targets=linkable_targets,
|
|
ansible_sources=_ansible_source_names(),
|
|
)
|
|
|
|
|
|
@hosts_bp.get("/new")
|
|
@require_role(UserRole.operator)
|
|
async def new_host():
|
|
return await render_template("hosts/form.html", host=None)
|
|
|
|
|
|
@hosts_bp.post("/")
|
|
@require_role(UserRole.operator)
|
|
async def create_host():
|
|
form = await request.form
|
|
host = Host(
|
|
name=form["name"].strip(),
|
|
address=form["address"].strip(),
|
|
)
|
|
async with current_app.db_sessionmaker() as db:
|
|
async with db.begin():
|
|
try:
|
|
await host_svc.check_name_free(db, host.name)
|
|
except ValueError as exc:
|
|
return str(exc), 400
|
|
db.add(host)
|
|
await log_audit(current_app, session.get("user_id"), session.get("username", ""),
|
|
"host.created", entity_type="host", entity_id=host.name,
|
|
detail={"address": host.address})
|
|
# Land on the new host's hub so monitors/agent/ansible can be set up next.
|
|
return redirect(url_for("hosts.host_detail", host_id=host.id))
|
|
|
|
|
|
@hosts_bp.get("/<host_id>/edit")
|
|
@require_role(UserRole.operator)
|
|
async def edit_host(host_id: str):
|
|
# Pure host config (name/address/monitors). Agent, Ansible target linking,
|
|
# and playbook runs live on the host detail page (the hub), not here.
|
|
async with current_app.db_sessionmaker() as db:
|
|
result = await db.execute(select(Host).where(Host.id == host_id))
|
|
host = result.scalar_one_or_none()
|
|
if host is None:
|
|
return "Not found", 404
|
|
|
|
return await render_template("hosts/form.html", host=host)
|
|
|
|
|
|
@hosts_bp.post("/<host_id>")
|
|
@require_role(UserRole.operator)
|
|
async def update_host(host_id: str):
|
|
form = await request.form
|
|
async with current_app.db_sessionmaker() as db:
|
|
async with db.begin():
|
|
result = await db.execute(select(Host).where(Host.id == host_id))
|
|
host = result.scalar_one_or_none()
|
|
if host is None:
|
|
return "Not found", 404
|
|
old_name, name = host.name, form["name"].strip()
|
|
if name != old_name:
|
|
try:
|
|
await host_svc.check_name_free(db, name, exclude_id=host.id)
|
|
except ValueError as exc:
|
|
return str(exc), 400
|
|
host.name = name
|
|
host.address = form["address"].strip()
|
|
# Agent and container metrics, and the rules and windows on them,
|
|
# go by the host's name: move them with it.
|
|
moved = await renames.carry_rename(db, renames.HOST_SOURCES, old_name, name)
|
|
if moved:
|
|
await audit_request("host.renamed", "host", name,
|
|
{"host_id": host_id, "renamed_from": old_name, "carried": moved})
|
|
return redirect(url_for("hosts.host_detail", host_id=host_id))
|
|
|
|
|
|
@hosts_bp.post("/<host_id>/ansible-link")
|
|
@require_role(UserRole.operator)
|
|
async def ansible_link(host_id: str):
|
|
"""Link, unlink, or create an AnsibleTarget from this host."""
|
|
from steward.models.ansible_inventory import AnsibleTarget
|
|
|
|
form = await request.form
|
|
action = form.get("action", "")
|
|
|
|
async with current_app.db_sessionmaker() as db:
|
|
async with db.begin():
|
|
if action == "unlink":
|
|
linked_result = await db.execute(
|
|
select(AnsibleTarget).where(AnsibleTarget.host_id == host_id)
|
|
)
|
|
linked = linked_result.scalar_one_or_none()
|
|
if linked:
|
|
linked.host_id = None
|
|
|
|
elif action == "link":
|
|
target_id = form.get("target_id", "").strip()
|
|
if target_id:
|
|
result = await db.execute(
|
|
select(AnsibleTarget).where(AnsibleTarget.id == target_id)
|
|
)
|
|
target = result.scalar_one_or_none()
|
|
if target:
|
|
# Clear any previous link for this host first
|
|
old_result = await db.execute(
|
|
select(AnsibleTarget).where(AnsibleTarget.host_id == host_id)
|
|
)
|
|
old = old_result.scalar_one_or_none()
|
|
if old and old.id != target_id:
|
|
old.host_id = None
|
|
target.host_id = host_id
|
|
|
|
elif action == "create":
|
|
host_result = await db.execute(select(Host).where(Host.id == host_id))
|
|
host = host_result.scalar_one_or_none()
|
|
if host:
|
|
new_target = AnsibleTarget(
|
|
id=str(uuid.uuid4()),
|
|
name=host.name,
|
|
address=host.address,
|
|
host_id=host_id,
|
|
)
|
|
db.add(new_target)
|
|
|
|
return redirect(f"/hosts/{host_id}")
|
|
|
|
|
|
@hosts_bp.post("/<host_id>/run-playbook")
|
|
@require_role(UserRole.operator)
|
|
async def run_playbook(host_id: str):
|
|
"""Run an Ansible playbook against just this host (ephemeral one-host inventory)."""
|
|
form = await request.form
|
|
async with current_app.db_sessionmaker() as db:
|
|
host = (await db.execute(select(Host).where(Host.id == host_id))).scalar_one_or_none()
|
|
if host is None:
|
|
return "Not found", 404
|
|
|
|
source_name = (form.get("source_name", "") or "").strip()
|
|
playbook = (form.get("playbook_path", "") or "").strip()
|
|
if not source_name or not playbook:
|
|
return "source and playbook are required", 400
|
|
try:
|
|
srcs = ansible_src.get_sources(current_app.config.get("ANSIBLE", {}))
|
|
except Exception:
|
|
return "Ansible sources are misconfigured", 400
|
|
source = next((s for s in srcs if s["name"] == source_name), None)
|
|
if source is None:
|
|
return "Source not found", 404
|
|
if playbook not in ansible_src.discover_playbooks(source["path"]):
|
|
return f"Playbook '{playbook}' not found in source '{source_name}'", 404
|
|
|
|
# Shared parser: discovered var__ fields → extra_vars_map, secret__ → secret_vars
|
|
# (unpersisted), plus tags/check. No --limit — there's exactly one host.
|
|
from steward.ansible.routes import _parse_run_params, _run_vars, check_run_form
|
|
params_or_none, secret_vars, err = _parse_run_params(form)
|
|
if err:
|
|
return err, 400
|
|
err = check_run_form(form, source, playbook, params_or_none, secret_vars)
|
|
if err:
|
|
return err, 400
|
|
|
|
run_id = str(uuid.uuid4())
|
|
inv_content = ansible_src.host_inventory_content(host)
|
|
run = AnsibleRun(
|
|
id=run_id,
|
|
playbook_path=playbook,
|
|
inventory_path=f"host: {host.name}",
|
|
source_name=source_name,
|
|
triggered_by=session.get("user_id"),
|
|
status=AnsibleRunStatus.running,
|
|
params=params_or_none,
|
|
)
|
|
async with current_app.db_sessionmaker() as db:
|
|
async with db.begin():
|
|
db.add(run)
|
|
|
|
task = asyncio.create_task(
|
|
executor.start_run(
|
|
current_app._get_current_object(), # type: ignore[attr-defined]
|
|
run_id, playbook, f"host: {host.name}", source["path"],
|
|
params_or_none, inv_content, secret_vars=secret_vars,
|
|
)
|
|
)
|
|
task.add_done_callback(
|
|
lambda t: t.exception() and current_app.logger.error(
|
|
"Host playbook run %s raised: %s", run_id, t.exception()))
|
|
|
|
await log_audit(current_app, session.get("user_id"), session.get("username", ""),
|
|
"ansible.host_run", entity_type="host", entity_id=host.name,
|
|
detail={"playbook": playbook, "source": source_name, "run_id": run_id,
|
|
"check": bool((params_or_none or {}).get("check")),
|
|
"vars": sorted(_run_vars(params_or_none, secret_vars))})
|
|
return redirect(url_for("ansible.run_detail", run_id=run_id))
|
|
|
|
|
|
@hosts_bp.post("/<host_id>/delete")
|
|
@require_role(UserRole.admin)
|
|
async def delete_host(host_id: str):
|
|
host_name = None
|
|
async with current_app.db_sessionmaker() as db:
|
|
async with db.begin():
|
|
result = await db.execute(select(Host).where(Host.id == host_id))
|
|
host = result.scalar_one_or_none()
|
|
if host:
|
|
host_name = host.name
|
|
await db.delete(host)
|
|
if host_name:
|
|
await log_audit(current_app, session.get("user_id"), session.get("username", ""),
|
|
"host.deleted", entity_type="host", entity_id=host_name)
|
|
return redirect(url_for("hosts.list_hosts"))
|
|
|
|
|
|
@hosts_bp.get("/uptime")
|
|
@require_role(UserRole.viewer)
|
|
async def uptime_page():
|
|
async with current_app.db_sessionmaker() as db:
|
|
result = await db.execute(select(Host).order_by(Host.name))
|
|
uptime = await host_svc.compute_uptime(db)
|
|
hosts = host_svc.worst_uptime_first(result.scalars().all(), uptime)
|
|
return await render_template("hosts/uptime.html", hosts=hosts, uptime=uptime)
|