Files
FabledSteward/steward/models/monitors.py
T
bvandeusenandClaude Opus 5.5 ad4f7d9232
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 45s
CI / unit (push) Successful in 1m10s
CI / publish (push) Successful in 54s
fix(metrics): time-series rows get time-ordered ids, so an insert stops costing a disk read (#5149)
Measured on dev (385a579), agent posts cut to five samples still dropped:
flush=8.8s for 990 rows, 6.6s for 680, 8.4s for 1000 — about 110 rows/s,
~9 ms a row, with rules and evaluation near zero. One random read per
row fits a random uuid4 primary key whose index no longer fits in memory:
every new row lands on a random page of it. (Inferred from the rate; the
table and index sizes have not been read.)

- steward.core.ids.time_ordered_id: a UUIDv7-layout string (48-bit ms
  timestamp, then random), same 36-char shape as the stored uuid4 ids, so
  no migration. Python's uuid7() arrives in 3.14; the image runs 3.13.
- Used for plugin_metrics and its hourly rollup, monitor_results,
  audit_events, the docker metrics/hourly/events/logs tables, and the
  traefik and unifi time series.
- MAX_SAMPLES_PER_INGEST 5 → 2 (~400 rows) for now: it fits the agent's
  10 s even at the old rate, so hosts land data whichever way the new rate
  comes out. Raise it once the new rate is measured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 18:58:41 -04:00

121 lines
5.1 KiB
Python

from __future__ import annotations
import enum
import json
import uuid
from datetime import datetime, timezone
from sqlalchemy import (
Boolean, CheckConstraint, DateTime, Float, ForeignKey, Index, Integer, String, Text,
)
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base
from steward.core.ids import time_ordered_id
class MonitorType(str, enum.Enum):
"""The kinds of synthetic/uptime check a Monitor can run."""
icmp = "icmp" # ICMP echo via the system ping binary
tcp = "tcp" # TCP connect to target:port
dns = "dns" # DNS A/AAAA resolution, optional expected-IP assertion
http = "http" # HTTP(S) request — status / content / TLS expiry
# Whitelist for the CHECK constraint. `type` is a plain String + CHECK rather
# than a native Postgres enum so a new monitor type ships as a same-change
# DROP/ADD CONSTRAINT (family rule 36) instead of the heavier ALTER TYPE ADD
# VALUE a native enum would force.
MONITOR_TYPE_VALUES = tuple(t.value for t in MonitorType)
# How a page names each type.
MONITOR_TYPE_LABELS = {"icmp": "Ping", "tcp": "TCP", "dns": "DNS", "http": "HTTP"}
_TYPE_CHECK = "type IN ('icmp', 'tcp', 'dns', 'http')"
class Monitor(Base):
"""A single configured uptime/synthetic check of any type.
Unifies what used to be host-coupled ping/DNS facets (columns on `hosts`)
and the standalone HTTP plugin monitor. `host_id` is OPTIONAL: set when the
check belongs to a registered Host (then it surfaces on the host hub), or
NULL for a free-standing custom destination. Type-specific settings live in
`config_json` so adding a new type never widens this table.
"""
__tablename__ = "monitors"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
name: Mapped[str] = mapped_column(String(128), nullable=False)
type: Mapped[str] = mapped_column(String(16), nullable=False)
# Address (icmp/tcp/dns) or URL (http) — free-form, not tied to a Host row.
target: Mapped[str] = mapped_column(String(2048), nullable=False)
host_id: Mapped[str | None] = mapped_column(
String(36), ForeignKey("hosts.id", ondelete="CASCADE"), nullable=True, index=True
)
# Type-specific config as a JSON object. Keys by type:
# icmp -> {}
# tcp -> {"port": int}
# dns -> {"expected_ip": str | None}
# http -> {"method", "expected_status", "content_match", "headers",
# "timeout_seconds", "follow_redirects", "verify_ssl"}
config_json: Mapped[str] = mapped_column(Text, nullable=False, default="{}")
enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
# 0 = use the global monitors.poll_interval_seconds setting.
check_interval_seconds: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
# Updated after each check so the scheduler can compute next-run efficiently.
last_checked_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True
)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False,
default=lambda: datetime.now(timezone.utc),
)
__table_args__ = (
CheckConstraint(_TYPE_CHECK, name="ck_monitors_type"),
)
@property
def config(self) -> dict:
try:
return json.loads(self.config_json or "{}")
except (ValueError, TypeError):
return {}
def set_config(self, cfg: dict) -> None:
self.config_json = json.dumps(cfg or {})
class MonitorResult(Base):
"""One check result for a Monitor, across all types.
Superset of the old per-type result columns: response_ms is universal,
while status_code/content_matched/tls_expires_at (http) and resolved_ip
(dns) are populated only for the types that produce them.
"""
__tablename__ = "monitor_results"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=time_ordered_id
)
monitor_id: Mapped[str] = mapped_column(
String(36), ForeignKey("monitors.id", ondelete="CASCADE"),
nullable=False, index=True,
)
checked_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False,
default=lambda: datetime.now(timezone.utc), index=True,
)
is_up: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
response_ms: Mapped[float | None] = mapped_column(Float, nullable=True)
status_code: Mapped[int | None] = mapped_column(Integer, nullable=True) # http
resolved_ip: Mapped[str | None] = mapped_column(String(255), nullable=True) # dns
content_matched: Mapped[bool | None] = mapped_column(Boolean, nullable=True) # http
tls_expires_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True # http
)
error_msg: Mapped[str | None] = mapped_column(String(512), nullable=True)
__table_args__ = (
# The status/heartbeat queries page the latest N per monitor by time.
Index("ix_monitor_results_monitor_checked", "monitor_id", "checked_at"),
)