# -*- coding: utf-8 -*- """内网文件服务器状态 API:心跳上报接收 + 管理端状态查看。 - 文件服务器(fileserver,8802)周期主动上报心跳,本模块落库 fileserver_status; - 中心机监控线程(core/monitor.py)依据 last_seen 超时判离线,并钉钉告警管理员; - 心跳上报仅需 X-API-Token(底座凭证),查看需管理员票据。 """ from __future__ import annotations from fastapi import APIRouter, Header, HTTPException, Query from pydantic import BaseModel, Field from controlplane.core.auth import _check_token, _require_admin from controlplane.core.db import _db, _now router = APIRouter() class FileserverHeartbeatIn(BaseModel): """文件服务器心跳载荷(主动上报状态)。host 为唯一标识,默认=对外下载根。""" host: str = Field(default="", max_length=255) base_url: str = Field(default="", max_length=255) hostname: str = Field(default="", max_length=128) ip: str = Field(default="", max_length=64) port: int = 0 file_count: int = 0 total_size: int = 0 version: str = Field(default="", max_length=64) uptime_sec: int = 0 @router.post("/api/fileserver/heartbeat") def fileserver_heartbeat(b: FileserverHeartbeatIn, x_api_token: str = Header(default="", alias="X-API-Token")) -> dict: """接收文件服务器心跳:upsert 状态快照,last_seen 刷新;离线中收到心跳则恢复在线。""" _check_token(x_api_token) host = b.host.strip() if not host: raise HTTPException(400, "host 不能为空") now = _now() with _db() as conn: row = conn.execute( "SELECT status, offline_notified FROM fileserver_status WHERE host = %s", (host,)).fetchone() if row: # 离线期间恢复上线:标记在线、清除离线告警标记(恢复时不再推送) was_offline = (not row["status"]) or bool(row["offline_notified"]) conn.execute( "UPDATE fileserver_status SET base_url=%s, hostname=%s, ip=%s, port=%s, " "file_count=%s, total_size=%s, version=%s, uptime_sec=%s, last_seen=%s, " "status=1, offline_notified=0" + (", last_status_change=%s" if was_offline else "") + " WHERE host=%s", (b.base_url.strip(), b.hostname.strip(), b.ip.strip(), int(b.port or 0), int(b.file_count or 0), int(b.total_size or 0), b.version.strip(), int(b.uptime_sec or 0), now) + ((now, host) if was_offline else (host,))) else: conn.execute( "INSERT INTO fileserver_status(host, base_url, hostname, ip, port, file_count, " "total_size, version, uptime_sec, last_seen, status, offline_notified, " "created_at, last_status_change) " "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 1, 0, %s, %s)", (host, b.base_url.strip(), b.hostname.strip(), b.ip.strip(), int(b.port or 0), int(b.file_count or 0), int(b.total_size or 0), b.version.strip(), int(b.uptime_sec or 0), now, now, now)) conn.commit() return {"ok": True, "host": host, "last_seen": now} @router.get("/api/admin/fileserver/status") def fileserver_status_list(limit: int = Query(default=100, ge=1, le=1000), x_admin_token: str = Header(default="", alias="X-Admin-Token"), x_api_token: str = Header(default="", alias="X-API-Token")) -> dict: """管理端查看文件服务器在线状态列表(按最近心跳倒序)。""" _require_admin(x_admin_token, x_api_token) with _db() as conn: rows = [dict(r) for r in conn.execute( "SELECT id, host, base_url, hostname, ip, port, file_count, total_size, version, " " uptime_sec, last_seen, status, last_status_change, offline_notified, " " created_at " "FROM fileserver_status ORDER BY last_seen DESC LIMIT %s", (limit,))] for r in rows: r["status"] = bool(r["status"]) r["offline_notified"] = bool(r["offline_notified"]) return {"fileservers": rows}