1. 新增内网文件服务器心跳上报与离线监控功能:
- 为文件服务器添加周期心跳上报到中心机的能力
- 中心机新增文件服务器状态监控线程,超时自动判离线并钉钉告警管理员
- 新增数据库表存储文件服务器状态,提供管理端API查看在线状态
2. 修复ERP物料收发汇总报表插件:
- 调整插件版本到1.0.9,更新前端配置与UI适配新旧ERP系统
- 重构组织与仓库默认值逻辑,区分新版/旧版ERP的默认配置
- 统一所有场景下显式设置查询组织,移除旧版默认组织的兼容逻辑
- 新增代管仓仓库编码适配旧版系统
3. 优化文件服务器打包脚本路径,更新文档示例命令
69 lines
3.0 KiB
Python
69 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""中心机后台监控:内网文件服务器心跳超时判离线 → 钉钉告警管理员。
|
||
|
||
- 文件服务器周期主动上报心跳(/api/fileserver/heartbeat,默认 60s 一次,落库 last_seen);
|
||
- 本模块后台线程按 FILESERVER_MONITOR_INTERVAL 扫描「在线且未告警」的记录,
|
||
对 last_seen 超过 FILESERVER_HEARTBEAT_TIMEOUT 的判定离线并推送钉钉;
|
||
- 离线转换用「带条件的原子 UPDATE」认领(status=1 且 offline_notified=0 且 last_seen 超时),
|
||
gunicorn 多 worker 并发下仅一个 worker 认领成功,避免重复告警;
|
||
- 恢复上线由心跳接口处理:收到心跳即翻转在线并清除告警标记,无需本线程干预。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
import threading
|
||
import time
|
||
from datetime import datetime, timedelta
|
||
|
||
from controlplane.core import config
|
||
from controlplane.core.db import _db, _now
|
||
from controlplane.core.notify import notify_fileserver_down
|
||
|
||
log = logging.getLogger(__name__)
|
||
|
||
|
||
def _scan_once() -> None:
|
||
"""扫描一次:将心跳超时的在线文件服务器翻转为离线并告警(幂等 / 并发安全)。"""
|
||
if not config.MYSQL_HOST:
|
||
return
|
||
cutoff = (datetime.now() - timedelta(seconds=config.FILESERVER_HEARTBEAT_TIMEOUT)) \
|
||
.strftime("%Y-%m-%d %H:%M:%S")
|
||
with _db() as conn:
|
||
rows = conn.execute(
|
||
"SELECT host, hostname, ip, last_seen FROM fileserver_status "
|
||
"WHERE status = 1 AND offline_notified = 0").fetchall()
|
||
for r in rows:
|
||
host = r["host"]
|
||
try:
|
||
with _db() as conn:
|
||
cur = conn.execute(
|
||
"UPDATE fileserver_status SET status = 0, offline_notified = 1, "
|
||
"last_status_change = %s "
|
||
"WHERE host = %s AND status = 1 AND offline_notified = 0 AND last_seen < %s",
|
||
(_now(), host, cutoff))
|
||
conn.commit()
|
||
claimed = cur.rowcount > 0
|
||
except Exception: # noqa: BLE001 单条认领失败不影响其余扫描
|
||
log.exception("文件服务器离线认领失败(host=%s)", host)
|
||
continue
|
||
if not claimed:
|
||
continue # 已被其它 worker 认领,或刚恢复心跳
|
||
log.warning("检测到文件服务器离线:%s(最后心跳 %s)", host, r["last_seen"])
|
||
notify_fileserver_down(host, r["hostname"] or "", r["ip"] or "",
|
||
str(r["last_seen"] or ""))
|
||
|
||
|
||
def _monitor_loop() -> None:
|
||
while True:
|
||
try:
|
||
_scan_once()
|
||
except Exception: # noqa: BLE001 监控异常不退出线程,下轮重试
|
||
log.exception("文件服务器监控扫描异常")
|
||
time.sleep(max(5, config.FILESERVER_MONITOR_INTERVAL))
|
||
|
||
|
||
def start_fileserver_monitor() -> None:
|
||
"""启动文件服务器离线监控线程(daemon,随进程退出)。"""
|
||
threading.Thread(target=_monitor_loop, daemon=True,
|
||
name="fileserver-monitor").start()
|