perf: 修复 Agent 校验 CPU 热点并实现指标分层聚合与频率下发

CPU 根因:每个 Agent 请求都做一次 bcrypt 校验(实测 72ms),
30 台主机 18 req/s × 72ms ≈ 1.35 核,实测容器 CPU 135%。

- agentauth: 校验结果按 Token 哈希缓存(TTL 5min,轮换即失效),
  last_used_at 写入按 60s 节流。实测 CPU 135% → 1.6%,请求延迟 103ms → 1.6ms
- metricagg: 新增后台逐级降采样聚合 raw→1m/5m→1h→6h/1d,取代前端手动触发
- metrics_aggregate: 补 uniq_metric_agg 唯一索引(原 idx_agg 非唯一导致 upsert 插重复行)
- agent 合并配置接口 GET /agent/config:一次返回任务+探针+端口策略+频率,
  配置轮询 5s→30s;服务端可下发 report_interval,Agent 热更新 ticker 无需重装
- retention: agent_reports 保留期 7d→1d(趋势由聚合表承担)
- redis: 增加 maxmemory 512mb + volatile-lru
- fix(upgrade): 升级脚本改为 fail-fast,修复下载失败仍报 UPGRADE_OK 的静默谎报

// Logan.Li
This commit is contained in:
Logan.Li committed 2026-09-14 08:23:31 +00:00
1 parent 93abf81e40
commit 59320aa410
23 files changed
+902 -29

No files matched your search

+16 -2
View File
@@ -695,9 +695,23 @@ func (h *AgentInstallHandler) BatchUpgradeAgent(c *gin.Context) {
binaryFile = "aiops-agent-linux-arm64"
}
// 下载新二进制 + 获取版本号 + 重启服务
// 下载新二进制 + 校验 + 重启服务。
// 必须 fail-fast:旧版脚本用 `;` 串联并把 UPGRADE_OK 无条件 echo,
// 下载失败时同样报"成功",导致升级结果完全不可信。 // Logan.Li
upgradeScript := fmt.Sprintf(
`curl -fsSL -o /tmp/aiops-agent %s && chmod +x /tmp/aiops-agent && systemctl stop aiops-agent 2>/dev/null; mv /tmp/aiops-agent /usr/local/bin/aiops-agent && systemctl start aiops-agent; ver=$(/usr/local/bin/aiops-agent version 2>/dev/null | awk '/AIOps Agent/{print $3}'); echo "UPGRADE_OK ver=$ver"`,
`set -e; `+
`curl -fsSL --connect-timeout 10 -o /tmp/aiops-agent.new %s || { echo "UPGRADE_FAIL download"; exit 1; }; `+
`size=$(wc -c < /tmp/aiops-agent.new); `+
`if [ "$size" -lt 1000000 ]; then echo "UPGRADE_FAIL size=$size"; exit 1; fi; `+
`chmod +x /tmp/aiops-agent.new; `+
`/tmp/aiops-agent.new version >/dev/null 2>&1 || { echo "UPGRADE_FAIL not-executable"; exit 1; }; `+
`systemctl stop aiops-agent 2>/dev/null || true; `+
`mv /tmp/aiops-agent.new /usr/local/bin/aiops-agent; `+
`systemctl start aiops-agent; `+
`sleep 1; `+
`ver=$(/usr/local/bin/aiops-agent version 2>/dev/null | awk '/AIOps Agent/{print $3}'); `+
`if [ -z "$ver" ]; then echo "UPGRADE_FAIL version-empty"; exit 1; fi; `+
`echo "UPGRADE_OK ver=$ver"`,
shellQuote(serverURL+"/api/agent/bin/"+binaryFile),
)