feat(monitors): unify ping/dns/http into one Monitor entity + custom targets
Collapse the three former check types into a single core `Monitor` entity
with one management surface (/monitors), one result table (monitor_results),
and a single scheduled task. Every type can now watch a free-standing custom
destination (optional host_id) — not just a registered Host.
- models: Monitor + MonitorResult replace PingResult/DnsResult; Host loses its
ping/dns facet columns (now Monitor rows linked by host_id).
- checks: monitors/{ping,dns,http}.py pure probes + runner.run_monitor
dispatcher; one monitor_check scheduler with a per-monitor due-filter.
- status: single monitor_status_source replaces the three sources.
- UI: /monitors blueprint (type-aware add/edit/list/widget); host hub shows a
host's linked monitors + "add monitor for this host"; nav + widget registry
+ alert metric catalog rewired. http plugin folded into core and removed.
- migration 0022 merges the http branch, data-migrates host facets +
http_monitors + all three result histories, drops the old tables/columns.
Resolves the per-host ping/dns auto-attach issue (#275): monitors are now
explicit, never auto-added to every host.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016Jg27rgypiW2efULXJDtMC
This commit is contained in:
+15
-37
@@ -1,43 +1,17 @@
|
||||
"""ICMP / TCP reachability checks — pure probes returning a result dict."""
|
||||
from __future__ import annotations
|
||||
import asyncio
|
||||
import logging
|
||||
import time
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from steward.core.alerts import record_metric
|
||||
from steward.models.hosts import Host, ProbeType
|
||||
from steward.models.monitors import PingResult, PingStatus
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
TCP_TIMEOUT = 5.0
|
||||
DEFAULT_TCP_PORT = 80
|
||||
|
||||
|
||||
async def ping_check(host: Host, session: AsyncSession) -> None:
|
||||
"""Probe a single host and write ping_result + metrics."""
|
||||
if host.probe_type == ProbeType.icmp:
|
||||
result = await _icmp_ping(host.address)
|
||||
else:
|
||||
result = await _tcp_ping(host.address, host.probe_port or DEFAULT_TCP_PORT)
|
||||
|
||||
status = PingStatus.up if result["up"] else PingStatus.down
|
||||
session.add(PingResult(
|
||||
host_id=host.id,
|
||||
status=status,
|
||||
response_time_ms=result.get("response_time_ms"),
|
||||
))
|
||||
await session.flush()
|
||||
|
||||
await record_metric(session, "ping", host.name, "up", 1.0 if result["up"] else 0.0)
|
||||
await record_metric(
|
||||
session, "ping", host.name, "response_time_ms",
|
||||
result.get("response_time_ms") or 0.0,
|
||||
)
|
||||
|
||||
|
||||
async def _tcp_ping(address: str, port: int) -> dict:
|
||||
async def tcp_check(address: str, port: int) -> dict:
|
||||
"""TCP connect probe. Returns {is_up, response_ms}."""
|
||||
start = time.monotonic()
|
||||
try:
|
||||
reader, writer = await asyncio.wait_for(
|
||||
@@ -46,13 +20,17 @@ async def _tcp_ping(address: str, port: int) -> dict:
|
||||
)
|
||||
writer.close()
|
||||
await writer.wait_closed()
|
||||
return {"up": True, "response_time_ms": round((time.monotonic() - start) * 1000, 2)}
|
||||
return {"is_up": True, "response_ms": round((time.monotonic() - start) * 1000, 2)}
|
||||
except Exception:
|
||||
return {"up": False, "response_time_ms": None}
|
||||
return {"is_up": False, "response_ms": None}
|
||||
|
||||
|
||||
async def _icmp_ping(address: str) -> dict:
|
||||
"""Use system ping command (setuid binary; no raw socket privilege needed in Python)."""
|
||||
async def icmp_check(address: str) -> dict:
|
||||
"""ICMP echo via the system ping binary (setuid; no raw-socket privilege).
|
||||
|
||||
Falls back to a TCP connect on the default port if the ping binary is
|
||||
unavailable or errors, so a host with ICMP filtered still gets a signal.
|
||||
"""
|
||||
start = time.monotonic()
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
@@ -62,8 +40,8 @@ async def _icmp_ping(address: str) -> dict:
|
||||
)
|
||||
await asyncio.wait_for(proc.wait(), timeout=5.0)
|
||||
if proc.returncode == 0:
|
||||
return {"up": True, "response_time_ms": round((time.monotonic() - start) * 1000, 2)}
|
||||
return {"up": False, "response_time_ms": None}
|
||||
return {"is_up": True, "response_ms": round((time.monotonic() - start) * 1000, 2)}
|
||||
return {"is_up": False, "response_ms": None}
|
||||
except Exception as exc:
|
||||
logger.warning(f"ICMP ping failed for {address}: {exc}, falling back to TCP")
|
||||
return await _tcp_ping(address, DEFAULT_TCP_PORT)
|
||||
logger.warning("ICMP ping failed for %s: %s, falling back to TCP", address, exc)
|
||||
return await tcp_check(address, DEFAULT_TCP_PORT)
|
||||
|
||||
Reference in New Issue
Block a user