Files
xiaoxia-saas/scripts/ci/runner_monitor/runner_metrics.py
T
xiaoxia 6eac0b2cf2
Worker Base Image Build / Build Worker Base Images (worker-base-builder-cache, infra/docker/worker-base-builder.Dockerfile, worker-base-builder, builder) (push) Failing after 1m54s
Worker Base Image Build / Build Worker Base Images (worker-base-runtime-cache, infra/docker/worker-base-runtime.Dockerfile, worker-base-runtime, runtime) (push) Failing after 1m36s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Has been skipped
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Validate - Code Quality (push) Failing after 1m29s
CI/CD Pipeline / Validate - Type Check (mypy) (push) Successful in 1m4s
CI/CD Pipeline / Validate - Migration (alembic) (push) Successful in 1m2s
CI/CD Pipeline / Unit Tests (push) Successful in 3m38s
CI/CD Pipeline / Integration Tests (push) Successful in 2m0s
CI/CD Pipeline / Frontend Lint (push) Successful in 28s
CI/CD Pipeline / Frontend Unit Tests (push) Failing after 44s
CI/CD Pipeline / PR Build API Image (push) Has been skipped
CI/CD Pipeline / PR Build Web Image (push) Has been skipped
CI/CD Pipeline / PR Build Worker Image (push) Has been skipped
CI/CD Pipeline / Build Staging API Image (push) Failing after 2m16s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 8m14s
CI/CD Pipeline / Build Staging Worker Image (push) Failing after 2m0s
CI/CD Pipeline / Build Production API Image (push) Has been skipped
CI/CD Pipeline / Build Production Web Image (push) Has been skipped
CI/CD Pipeline / Build Production Worker Image (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Staging E2E Tests (push) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (push) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (push) Has been skipped
chore(ci): 同步main分支CI配置与scripts/ci脚本 - 与develop对齐
同步内容:
1. CI流水线配置(ci-pipeline.yml)与develop对齐
2. PR构建脚本docker_build_only.sh增加buildx→docker build回退
3. pre-build步骤worker基础镜像构建增加buildx回退
4. 单元测试脚本全量覆盖率改为仅报告不阻塞
5. diff-cover依赖加入requirements-dev.txt
6. worker base builder/runtime Dockerfile同步
7. test_config_oss.py clear=False→clear=True修复OSS污染
8. Frontend Lint增加prettier依赖
2026-07-24 10:36:29 +08:00

83 lines
2.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Runner 系统指标采集 - CPU/内存/磁盘(通过 SSH 连接构建服务器)
⚠️ 第一版:骨架 + 接口定义,SSH 实装后续补充
原因:跨机器 SSH 需要密钥管理和网络权限,先把监控框架搭好。
接口约定(与 alert_manager 对接):
metrics = RunnerMetricsCollector().collect_all()
# 返回: [{"host": "...", "cpu_percent": 75.2, "mem_percent": 80.1, "disk_percent": 65.0,
# "disk_total_gb": 500, "disk_used_gb": 325, "status": "ok"}, ...]
当 SSH 不可用时,返回空列表,不影响其他监控功能。
"""
from runner_monitor import config
class RunnerMetricsCollector:
"""Runner 系统指标采集器
第一版:返回空数据(SSH 实装待后续迭代)
接口已定义好,alert_manager 直接消费。
"""
def __init__(self, ssh_hosts=None, ssh_key_path=None, ssh_user=None):
self.ssh_hosts = ssh_hosts or config.SSH_HOSTS
self.ssh_key_path = ssh_key_path or config.SSH_KEY_PATH
self.ssh_user = ssh_user or config.SSH_USER
def collect_all(self):
"""采集所有 runner 的系统指标
Returns:
list[dict]: 每台机器的指标数据
"""
if not self.ssh_hosts:
# 没有配置 SSH 主机,返回空列表
return []
results = []
for host in self.ssh_hosts:
try:
metrics = self._collect_one(host)
results.append(metrics)
except Exception as e:
results.append(
{
"host": host,
"status": "error",
"error": str(e),
}
)
return results
def _collect_one(self, host):
"""采集单台机器的指标(SSH 实装待后续)
当前直接返回 not_available 状态。
后续实现方案:用 paramiko 或 subprocess + ssh 命令执行:
- top / mpstat 取 CPU
- free 取内存
- df -h 取磁盘
"""
return {
"host": host,
"status": "not_available",
"cpu_percent": None,
"mem_percent": None,
"disk_percent": None,
"disk_total_gb": None,
"disk_used_gb": None,
"note": "SSH metrics collection not implemented yet",
}
# ── 便捷方法 ──────────────────────────────────────
@staticmethod
def is_available():
"""是否有可用的指标采集(SSH 已配置)"""
return bool(config.SSH_HOSTS and config.SSH_KEY_PATH)