99 lines
3.3 KiB
Python
99 lines
3.3 KiB
Python
"""Monitoring router — metrics, collector controls, disk space."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import time
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from media_library_viewer_api.clients.ssh import RemoteSSHClient
|
|
from media_library_viewer_api.dependencies import get_ssh_client
|
|
|
|
logger = logging.getLogger(__name__)
|
|
from media_library_viewer_api.clients.resources import (
|
|
disk_space,
|
|
read_resource_metrics,
|
|
resource_collector_debug_info,
|
|
resource_collector_status,
|
|
restart_resource_collector,
|
|
start_resource_collector,
|
|
stop_resource_collector,
|
|
)
|
|
from media_library_viewer_api.config import get_settings
|
|
|
|
router = APIRouter(prefix="/api/monitoring", tags=["monitoring"])
|
|
|
|
|
|
@router.get("/status")
|
|
def get_status(ssh: RemoteSSHClient = Depends(get_ssh_client)) -> dict[str, str]:
|
|
"""Return collector running status."""
|
|
status = resource_collector_status(ssh)
|
|
logger.info("Monitoring status requested: %s", status)
|
|
return {"status": status}
|
|
|
|
|
|
@router.get("/metrics")
|
|
def get_metrics(
|
|
max_lines: int = 70_000,
|
|
last_seconds: int | None = None,
|
|
ssh: RemoteSSHClient = Depends(get_ssh_client),
|
|
) -> dict[str, Any]:
|
|
"""Return resource metric samples from the remote collector."""
|
|
rows = read_resource_metrics(ssh, max_lines=max_lines)
|
|
if last_seconds is None:
|
|
filtered = rows
|
|
cutoff_ts = 0.0
|
|
else:
|
|
cutoff_ts = time.time() - last_seconds
|
|
filtered = [row for row in rows if float(row.get("ts", 0)) >= cutoff_ts]
|
|
logger.info("Monitoring metrics requested total=%s filtered=%s last_seconds=%s", len(rows), len(filtered), last_seconds)
|
|
return {
|
|
"samples": filtered,
|
|
"total_samples": len(rows),
|
|
"filtered_samples": len(filtered),
|
|
"cutoff_ts": cutoff_ts,
|
|
}
|
|
|
|
|
|
@router.get("/disk")
|
|
def get_disk_space(ssh: RemoteSSHClient = Depends(get_ssh_client)) -> dict[str, Any]:
|
|
"""Return disk space for the configured media root."""
|
|
settings = get_settings()
|
|
path = settings.media_root or "/"
|
|
logger.info("Monitoring disk requested path=%s", path)
|
|
return disk_space(ssh, path)
|
|
|
|
|
|
@router.post("/start")
|
|
def post_start(ssh: RemoteSSHClient = Depends(get_ssh_client)) -> dict[str, str]:
|
|
"""Start the remote resource collector."""
|
|
message = start_resource_collector(ssh)
|
|
logger.info("Monitoring collector start result: %s", message)
|
|
return {"message": message}
|
|
|
|
|
|
@router.post("/stop")
|
|
def post_stop(ssh: RemoteSSHClient = Depends(get_ssh_client)) -> dict[str, str]:
|
|
"""Stop the remote resource collector."""
|
|
message = stop_resource_collector(ssh)
|
|
logger.info("Monitoring collector stop result: %s", message)
|
|
return {"message": message}
|
|
|
|
|
|
@router.post("/restart")
|
|
def post_restart(ssh: RemoteSSHClient = Depends(get_ssh_client)) -> dict[str, str]:
|
|
"""Restart the remote resource collector."""
|
|
message = restart_resource_collector(ssh)
|
|
logger.info("Monitoring collector restart result: %s", message)
|
|
return {"message": message}
|
|
|
|
|
|
@router.get("/diagnostics")
|
|
def get_diagnostics(ssh: RemoteSSHClient = Depends(get_ssh_client)) -> dict[str, str]:
|
|
"""Return collector debug info for troubleshooting."""
|
|
diagnostics = resource_collector_debug_info(ssh)
|
|
logger.info("Monitoring diagnostics requested")
|
|
return {"diagnostics": diagnostics}
|