# edge-monitor-openeuler **Repository Path**: lingering-zephyr-code/edge-monitor-openeuler ## Basic Information - **Project Name**: edge-monitor-openeuler - **Description**: openEuler边缘设备轻量级监控工具  - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2026-03-19 - **Last Updated**: 2026-06-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Edge Monitor OpenEuler 轻量级边缘计算系统监控方案,基于 Python 原生开发,专为 openEuler 系统优化设计。 ## 📋 项目简介 Edge Monitor 是一款面向边缘计算场景的轻量级监控解决方案,提供系统资源监控、多协议接入、数据持久化、集群管理等核心能力,支持本地自治自愈和边缘 AI 推理。 ## ✨ 功能特性 ### 核心功能 - **系统资源监控**: CPU、内存、磁盘使用率及系统负载实时监控 - **网络流量监控**: 网卡收发流量实时统计 - **本地告警模块**: 控制台高亮显示 + 日志持久化 + 历史记录 - **多协议接入**: 支持 MQTT / Modbus / RTSP 协议 - **时序数据库**: 内置 TSDB 存储引擎,支持数据降采样和查询 - **存储-转发机制**: 可靠的 SAF(Store-And-Forward)断点续传 - **插件化架构**: 支持社区扩展和自定义插件 ### 高级特性 - **边缘 AI 推理**: 负载预测能力 - **Mesh 边缘组网**: UDP 广播节点发现 + TCP 长连接 + Gossip 协议 - **多节点集群**: Gossip 发现 + Raft 共识 + Web 仪表盘 - **数据预处理**: 滑动窗口聚合、日志降噪、SQLite 本地缓冲 - **本地自治自愈**: 规则引擎驱动的自动化故障处理 ## 🛠️ 技术栈 | 分类 | 技术 | 版本要求 | |------|------|----------| | 语言 | Python | >= 3.8 | | 异步框架 | aiohttp | >= 3.9.0 | | 数据库 | aiosqlite | >= 0.19.0 | | MQTT | paho-mqtt | >= 1.6.0 | | Modbus | pymodbus | >= 3.0.0 | | 视频处理 | opencv-python | >= 4.5.0 | | 配置管理 | pyyaml | >= 5.4 | | 系统监控 | psutil | >= 5.8 | | 数据序列化 | msgpack | >= 1.0 | ## 🏗️ 项目结构 ``` edge-monitor-openeuler/ ├── ai/ # 边缘 AI 推理模块 │ ├── ai_pipeline.py # AI 处理管道 │ ├── ai_plugin.py # AI 插件接口 │ └── ai_predictor.py # 负载预测器 ├── async_*/ # 异步版本模块(async_bus, async_cluster, async_gateway, async_plugins, async_saf, async_utils) ├── bus/ # 消息总线 │ ├── webhook.py # Webhook 管理器 │ └── zenoh_bus.py # Zenoh 消息总线 ├── cluster/ # 集群管理 │ ├── discovery.py # Gossip 节点发现 │ ├── raft.py # Raft 共识协议 │ └── state.py # 集群状态管理 ├── config/ # 配置文件 │ ├── rules.json # 自愈规则配置 │ └── rules.yaml # 规则配置(YAML 格式) ├── core/ # 核心模块 │ ├── alert_manager.py # 告警管理器 │ ├── disk_monitor.py # 磁盘监控 │ ├── edge_monitor.py # 边缘监控主模块 │ ├── prometheus_metrics.py # Prometheus 指标 │ └── systemd_notify.py # Systemd 通知 ├── docs/ # 文档 │ └── async-design.md # 异步设计文档 ├── engine/ # 引擎模块 │ ├── preprocess.py # 数据预处理 │ └── rules_engine.py # 规则引擎 ├── gateway/ # 边缘网关 │ ├── dashboard/ # Web 仪表盘前端 │ ├── api_handler.py # API 处理器 │ ├── gateway.py # 网关主模块 │ ├── modbus_plugin.py # Modbus 插件 │ ├── mqtt_client.py # MQTT 客户端 │ └── rtsp_plugin.py # RTSP 插件 ├── mesh/ # Mesh 组网 │ ├── discovery.py # 节点发现 │ └── networking.py # 网络通信 ├── plugins/ # 插件系统 │ ├── alert_forward.py # 告警转发插件 │ ├── plugin_base.py # 插件基类 │ ├── plugin_manager.py # 插件管理器 │ └── res_monitor.py # 资源监控插件 ├── saf/ # 存储-转发模块 │ ├── saf_manager.py # SAF 管理器 │ ├── saf_monitor.py # SAF 监控 │ ├── saf_queue.py # 消息队列 │ └── saf_sender.py # 消息发送器 ├── systemd/ # Systemd 服务配置 │ ├── edge-gateway.service │ └── edge-monitor-python.service ├── utils/ # 工具模块 │ ├── cache.py # 缓存工具 │ ├── connection_pool.py # 连接池 │ └── xdg_paths.py # XDG 路径处理 ├── main.py # 监控主程序入口 ├── main_async.py # 异步版本入口 ├── gateway.py # 网关启动脚本 ├── requirements.txt # 依赖列表 └── config.ini # 监控配置文件 ``` ## 🚀 快速开始 ### 环境要求 - Python 3.8+ - openEuler 22.03 LTS 或兼容系统 - 建议内存 >= 2GB ### 安装依赖 ```bash git clone https://gitee.com/lingering-zephyr-code/edge-monitor-openeuler.git cd edge-monitor-openeuler pip3 install -r requirements.txt ``` ### 开发模式运行 ```bash # 启动 Edge Monitor(系统监控服务) python3 main.py # 启动 Edge Gateway(API 网关服务) python3 gateway/gateway.py ``` ### 生产环境部署 ```bash # 使用安装脚本 sudo ./install.sh # 启动服务 sudo systemctl start edge-monitor-python sudo systemctl start edge-gateway # 设置开机自启 sudo systemctl enable edge-monitor-python sudo systemctl enable edge-gateway ``` ## 🌐 服务访问 | 服务 | 地址 | 端口 | |------|------|------| | Edge Monitor Dashboard | http://localhost:9100/dashboard/index.html | 9100 | | Prometheus Metrics | http://localhost:9100/metrics | 9100 | | Edge Gateway API | http://localhost:8080 | 8080 | | Gateway Dashboard | http://localhost:8080/dashboard/index.html | 8080 | | TSDB Query | http://localhost:8080/tsdb/query | 8080 | ## 📝 配置说明 ### 监控配置 (`config.ini`) ```ini [alert] cpu_usage_max = 90 mem_usage_max = 85 disk_usage_max = 85 load_max = 4.0 alert_interval = 10 ``` ### 网关配置 (`gateway.ini`) ```ini [gateway] name = edge-gateway hostname = edge-node-01 [mqtt] enabled = true broker = localhost port = 1883 username = password = qos = 1 [cluster] enabled = false name = edge-cluster gossip_port = 9331 raft_port = 9332 known_peers = 192.168.1.100,192.168.1.101 [tsdb] enabled = true data_dir = data/tsdb max_mem_points = 10000 [saf] enabled = true db_path = data/saf_queue.db max_size = 100000 ttl_hours = 24 ``` ### 自愈规则配置 (`config/rules.json`) ```json { "rules": [ { "name": "high_cpu_protection", "condition": "cpu > 95", "duration": 60, "action": "write_log", "cooldown": 300, "params": {"message": "High CPU usage detected"} }, { "name": "memory_warning", "condition": "mem > 85", "duration": 30, "action": "write_log", "cooldown": 120, "params": {"message": "Memory usage exceeded threshold"} } ] } ``` ## 📡 服务说明 ### 核心服务 | 服务名称 | 类型 | 默认端口 | 协议 | 说明 | |---------|------|---------|------|------| | Edge Monitor | 系统监控 | 9100 | HTTP | CPU/内存/磁盘/网络监控 | | Edge Gateway | API 网关 | 8080 | HTTP | 集群管理、设备管理、API 服务 | | MQTT Broker | 消息代理 | 1883 | TCP | 设备间通信(需外部部署) | | Modbus TCP | 工业协议 | 5020 | TCP | Modbus 设备接入 | ### 集群服务 | 服务名称 | 端口 | 协议 | 说明 | |---------|------|------|------| | Gossip Discovery | 9331 | UDP | 节点发现与成员管理 | | Raft Consensus | 9332 | TCP | 分布式一致性协议 | ### 内置组件 | 组件 | 说明 | |------|------| | TSDB | 时序数据库,数据持久化与查询 | | SAF Queue | 存储转发队列,断点续传 | | Rules Engine | 规则引擎,自动化故障处理 | ## 🔌 API 接口 ### 监控指标 ```bash # 获取监控仪表盘数据 GET /api/v1/dashboard # 获取 Prometheus 指标 GET /metrics # 获取健康检查 GET /health ``` ### TSDB 查询 ```bash # 查询时序数据 GET /tsdb/query?m=system_cpu&f=usage&agg=avg&d=1h # 获取 TSDB 状态 GET /tsdb/status ``` ### SAF 管理 ```bash # 获取 SAF 队列状态 GET /saf/status # 获取死信队列 GET /saf/dead?limit=100 # 重新入队死信消息 GET /saf/requeue?id= # 清空队列 POST /saf/clear ``` ### 集群管理 ```bash # 获取集群状态 GET /api/v1/cluster/status # 获取节点列表 GET /api/v1/nodes ``` ### Modbus 设备管理 ```bash # 获取设备列表 GET /api/v1/modbus/devices # 添加设备 POST /api/v1/modbus/devices # 更新设备 PUT /api/v1/modbus/devices/ # 删除设备 DELETE /api/v1/modbus/devices/ ``` ## 🧪 测试指南 ### Mesh 组网测试 在多台设备上启动 `main.py`,观察日志中的邻居发现: ``` [Discovery] Found peer: node123 @ 192.168.1.100 ``` ### 自愈规则测试 使用 stress-ng 模拟高 CPU 负载: ```bash # 安装 stress-ng sudo dnf install stress-ng -y # 模拟 4 核 CPU 满负载,持续 60 秒 stress-ng --cpu 4 --timeout 60s ``` 观察终端日志是否触发规则: ``` [RulesEngine] Rule high_cpu_protection condition met, waiting for duration [RulesEngine] Executing action: write_log ``` ### 数据预处理验证 ```bash sqlite3 edge_queue.db "SELECT * FROM event_queue;" ``` ## 🔍 故障排除 ### 端口占用检查 ```bash # Linux ss -tlnp | grep -E "8080|9100|1883|5020" # Windows netstat -ano | findstr ":8080" ``` ### 服务启动失败 1. 检查 Python 依赖是否安装完整 2. 检查配置文件是否存在且格式正确 3. 查看系统日志 `journalctl -u edge-monitor-python` ### MQTT 连接失败 1. 确认 Mosquitto 服务已启动 2. 检查 `gateway.ini` 中的 MQTT 配置 3. 验证网络连通性 ## 🤝 贡献指南 欢迎任何形式的贡献!请阅读 [CONTRIBUTING.md](CONTRIBUTING.md) 了解如何开始。 ### 开发流程 1. Fork 项目 2. 创建特性分支 (`git checkout -b feature/xxx`) 3. 提交代码 (`git commit -am 'Add feature xxx'`) 4. 推送到分支 (`git push origin feature/xxx`) 5. 创建 Pull Request ### 代码规范 - 遵循 PEP 8 编码规范 - 使用 type hints - 编写单元测试 - 添加必要的文档注释 ## 📜 许可证 本项目采用 **MulanPSL-2.0** 许可证。详见 [LICENSE](LICENSE) 文件。 ## 🙏 致谢 - 感谢 openEuler 社区提供的构建环境 - 感谢所有贡献者的辛勤付出 --- **项目地址**: [https://gitee.com/lingering-zephyr-code/edge-monitor-openeuler](https://gitee.com/lingering-zephyr-code/edge-monitor-openeuler) **问题反馈**: 欢迎在 Issues 中提交问题和建议