PingWatch 网络设备离线监控系统

- FastAPI 后端 + Vue 3 前端
- Docker Compose 一键部署
- Casdoor OAuth 认证集成
- LogHive 集中式日志
- 设备批量 CSV 导入/导出
- WebSocket 实时状态推送
- 企业微信告警通知
- fping 高性能并发 Ping 检测

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-09 15:02:04 +08:00
commit 848f804169
55 changed files with 5941 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
from .device import Device, DeviceTypeEnum
from .ping_record import PingRecord
from .alert_event import AlertEvent, AlertTypeEnum
from .user import User, UserRoleEnum
__all__ = [
"Device", "DeviceTypeEnum",
"PingRecord",
"AlertEvent", "AlertTypeEnum",
"User", "UserRoleEnum",
]
+34
View File
@@ -0,0 +1,34 @@
import enum
from datetime import datetime
from sqlalchemy import Column, Integer, String, Boolean, DateTime, BigInteger, Enum
from .device import Base
class AlertTypeEnum(str, enum.Enum):
offline = "offline" # 设备离线
recovered = "recovered" # 设备恢复
system = "system" # 系统告警(如上游断网检测)
class AlertEvent(Base):
"""告警事件表"""
__tablename__ = "alert_events"
id = Column(BigInteger, primary_key=True, autoincrement=True)
device_id = Column(Integer, nullable=False, index=True, comment="关联设备 ID")
alert_type = Column(Enum(AlertTypeEnum), nullable=False, comment="告警类型")
message = Column(String(1024), default="", comment="告警消息摘要")
# 离线起止
start_at = Column(DateTime, nullable=False, comment="离线开始时间")
end_at = Column(DateTime, nullable=True, comment="恢复时间")
duration_minutes = Column(Integer, nullable=True, comment="离线时长(分钟)")
is_resolved = Column(Boolean, default=False, comment="是否已恢复")
notification_sent = Column(Boolean, default=False, comment="是否已发送通知")
acknowledged_at = Column(DateTime, nullable=True, comment="用户确认时间")
created_at = Column(DateTime, default=datetime.now, comment="创建时间")
def __repr__(self):
return f"<AlertEvent(id={self.id}, device={self.device_id}, type={self.alert_type})>"
+46
View File
@@ -0,0 +1,46 @@
import enum
from datetime import datetime
from sqlalchemy import Column, Integer, String, Float, Boolean, DateTime, Enum, Text
from sqlalchemy.orm import DeclarativeBase
class Base(DeclarativeBase):
pass
class DeviceTypeEnum(str, enum.Enum):
server = "server" # 服务器
olt = "olt" # OLT
switch = "switch" # 交换机
firewall = "firewall" # 防火墙
other = "other" # 其他
class Device(Base):
__tablename__ = "devices"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(128), nullable=False, comment="设备名称")
ip = Column(String(45), nullable=False, index=True, comment="IP 地址")
device_type = Column(Enum(DeviceTypeEnum), nullable=False, default=DeviceTypeEnum.other, comment="设备类型")
location = Column(String(256), default="", comment="物理位置/地址")
project_name = Column(String(256), default="", comment="所属项目")
tags = Column(String(512), default="", comment="标签,逗号分隔")
# Ping 设置
ping_interval = Column(Integer, default=30, comment="ping 间隔(秒)")
alert_threshold = Column(Integer, default=5, comment="连续失败次数判离线")
is_enabled = Column(Boolean, default=True, comment="是否启用监控")
# 运行状态
current_status = Column(String(16), default="unknown", comment="当前状态: online/offline/unknown")
consecutive_failures = Column(Integer, default=0, comment="当前连续失败次数")
last_ping_time = Column(DateTime, nullable=True, comment="最后一次 ping 时间")
last_online_time = Column(DateTime, nullable=True, comment="最后一次在线时间")
last_offline_time = Column(DateTime, nullable=True, comment="最后一次离线时间")
created_at = Column(DateTime, default=datetime.now, comment="创建时间")
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
def __repr__(self):
return f"<Device(id={self.id}, name={self.name}, ip={self.ip})>"
+18
View File
@@ -0,0 +1,18 @@
from datetime import datetime
from sqlalchemy import Column, Integer, Float, Boolean, DateTime, BigInteger
from .device import Base
class PingRecord(Base):
"""单次 ping 结果记录"""
__tablename__ = "ping_records"
id = Column(BigInteger, primary_key=True, autoincrement=True)
device_id = Column(Integer, nullable=False, index=True)
is_alive = Column(Boolean, nullable=False, comment="是否通")
response_time_ms = Column(Float, nullable=True, comment="响应时间毫秒,不通则为 NULL")
round_num = Column(Integer, nullable=False, comment="轮次编号(从 1 递增)")
created_at = Column(DateTime, default=datetime.now, index=True, comment="记录时间")
def __repr__(self):
return f"<PingRecord(device={self.device_id}, alive={self.is_alive}, rtt={self.response_time_ms})>"
+27
View File
@@ -0,0 +1,27 @@
import enum
from datetime import datetime
from sqlalchemy import Boolean, Column, Integer, String, DateTime, Enum
from .device import Base
class UserRoleEnum(str, enum.Enum):
admin = "admin" # 管理员:可管理设备
viewer = "viewer" # 查看者:仅查看数据
class User(Base):
"""本地用户表,关联 Casdoor"""
__tablename__ = "users"
id = Column(Integer, primary_key=True, autoincrement=True)
casdoor_uid = Column(String(128), unique=True, nullable=False, comment="Casdoor 中的用户 ID")
username = Column(String(128), nullable=False, comment="用户名")
display_name = Column(String(128), default="", comment="显示名称")
role = Column(Enum(UserRoleEnum), nullable=False, default=UserRoleEnum.viewer, comment="角色")
wecom_userid = Column(String(128), default="", comment="企业微信 UserID(用于个人通知)")
is_active = Column(Boolean, default=True, comment="是否启用")
last_login_at = Column(DateTime, nullable=True, comment="最后登录时间")
created_at = Column(DateTime, default=datetime.now, comment="创建时间")
def __repr__(self):
return f"<User(id={self.id}, name={self.username}, role={self.role})>"