Files
tracer-cloud--opensre/surfaces/cli/commands/watchdog.py
T
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

96 lines
3.1 KiB
Python

"""``opensre watchdog`` process-threshold monitor."""
from __future__ import annotations
import click
from pydantic import ValidationError
from surfaces.interactive_shell.utils.error_handling.errors import OpenSREError
from tools.system.watch_dog.config import WatchdogConfig
from tools.system.watch_dog.runner import run_watchdog
@click.command(name="watchdog")
@click.option(
"--pid", type=int, default=None, help="PID to monitor. Mutually exclusive with --name."
)
@click.option(
"--name", "process_name", type=str, default=None, help="Process-name regex to monitor."
)
@click.option("--pick-first", is_flag=True, help="Use the lowest PID when --name matches many.")
@click.option(
"--max-cpu", type=float, default=None, help="Alarm when CPU percent reaches this value."
)
@click.option(
"--cpu-window",
type=str,
default="30",
show_default=True,
help="CPU smoothing window in seconds, or with s/m/h suffix.",
)
@click.option(
"--max-runtime", type=str, default=None, help="Alarm after runtime exceeds s/m/h duration."
)
@click.option(
"--max-rss", type=str, default=None, help="Alarm when RSS exceeds bytes or K/M/G/T size."
)
@click.option(
"--interval", type=float, default=5.0, show_default=True, help="Sample period in seconds."
)
@click.option(
"--cooldown",
type=str,
default="5m",
show_default=True,
help="Minimum gap between repeated alarms per threshold.",
)
@click.option("--once", is_flag=True, help="Exit after the first threshold alarm.")
@click.option("--chat-id", type=str, default=None, help="Override TELEGRAM_DEFAULT_CHAT_ID.")
@click.option("--verbose", is_flag=True, help="Print one line per sampled process state.")
def watchdog_command(
pid: int | None,
process_name: str | None,
pick_first: bool,
max_cpu: float | None,
cpu_window: str,
max_runtime: str | None,
max_rss: str | None,
interval: float,
cooldown: str,
once: bool,
chat_id: str | None,
verbose: bool,
) -> None:
"""Monitor a process and send Telegram alarms when thresholds trip."""
try:
config = WatchdogConfig.model_validate(
{
"pid": pid,
"name": process_name,
"pick_first": pick_first,
"max_cpu": max_cpu,
"cpu_window": cpu_window,
"max_runtime": max_runtime,
"max_rss": max_rss,
"interval": interval,
"cooldown": cooldown,
"once": once,
"chat_id": chat_id,
"verbose": verbose,
}
)
except ValidationError as exc:
raise OpenSREError(
"Invalid watchdog configuration.",
suggestion=_validation_suggestion(exc),
) from exc
raise SystemExit(run_watchdog(config))
def _validation_suggestion(exc: ValidationError) -> str:
first = exc.errors()[0]
location = ".".join(str(part) for part in first.get("loc", ()) if part != "__root__")
prefix = f"{location}: " if location else ""
return f"{prefix}{first.get('msg', 'Check the provided options.')}"