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
124 lines
4.3 KiB
Go
124 lines
4.3 KiB
Go
// Package task 的 client.go 实现与后端任务/探针接口的 HTTP 交互。
|
|
// 涵盖拉取待执行任务(poll)、回传执行结果(submit result)、拉取探针配置(poll probes)。
|
|
// 通过共享 client 处理鉴权 + 重试,本文件仅负责业务路径和数据模型。
|
|
//
|
|
// 署名 Logan.Li
|
|
package task
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/url"
|
|
|
|
"ezops/agent/client"
|
|
"ezops/agent/model"
|
|
)
|
|
|
|
const (
|
|
pollPath = "/api/v1/agent/tasks/poll"
|
|
resultPathFmt = "/api/v1/agent/tasks/%s/result"
|
|
probePath = "/api/v1/agent/health-probes/config"
|
|
portPolicyPath = "/api/v1/agent/port-policy"
|
|
// configPath 合并配置接口(v2.2):一次拉取任务+探针+端口策略+采集频率。 // Logan.Li
|
|
configPath = "/api/v1/agent/config"
|
|
)
|
|
|
|
// Client 负责与后端任务/探针接口通信。
|
|
type Client struct {
|
|
client *client.Client
|
|
hostname string
|
|
logger *log.Logger
|
|
}
|
|
|
|
// NewClient 创建任务客户端实例。
|
|
func NewClient(cli *client.Client, hostname string, logger *log.Logger) *Client {
|
|
return &Client{client: cli, hostname: hostname, logger: logger}
|
|
}
|
|
|
|
// Poll 拉取当前主机待执行的任务列表。
|
|
func (c *Client) Poll(ctx context.Context) ([]Task, error) {
|
|
path := pollPath + "?hostname=" + url.QueryEscape(c.hostname)
|
|
|
|
var wrapper struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data []Task `json:"data"`
|
|
}
|
|
if err := c.client.DoJSON(ctx, "GET", path, nil, &wrapper); err != nil {
|
|
return nil, fmt.Errorf("任务轮询失败: %w", err)
|
|
}
|
|
return wrapper.Data, nil
|
|
}
|
|
|
|
// Submit 将任务执行结果回传至后端。
|
|
func (c *Client) Submit(ctx context.Context, res Result) error {
|
|
path := fmt.Sprintf(resultPathFmt, url.PathEscape(res.TaskID))
|
|
if err := c.client.DoJSON(ctx, "POST", path, res, nil); err != nil {
|
|
return fmt.Errorf("任务结果回传失败: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// PollProbes 拉取本机健康探针配置(v2.1)。
|
|
func (c *Client) PollProbes(ctx context.Context) ([]model.ProbeConfig, error) {
|
|
path := probePath + "?hostname=" + url.QueryEscape(c.hostname)
|
|
|
|
var wrapper struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data []model.ProbeConfig `json:"data"`
|
|
}
|
|
if err := c.client.DoJSON(ctx, "GET", path, nil, &wrapper); err != nil {
|
|
return nil, fmt.Errorf("探针配置拉取失败: %w", err)
|
|
}
|
|
if wrapper.Data == nil {
|
|
return []model.ProbeConfig{}, nil
|
|
}
|
|
return wrapper.Data, nil
|
|
}
|
|
|
|
// PollPortPolicy 拉取本机端口过滤策略(可选配置,拉取失败静默忽略) // Logan.Li
|
|
func (c *Client) PollPortPolicy(ctx context.Context) (*model.PortPolicy, error) {
|
|
path := portPolicyPath + "?hostname=" + url.QueryEscape(c.hostname)
|
|
|
|
var wrapper struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data model.PortPolicy `json:"data"`
|
|
}
|
|
if err := c.client.DoJSON(ctx, "GET", path, nil, &wrapper); err != nil {
|
|
return nil, fmt.Errorf("端口策略拉取失败: %w", err)
|
|
}
|
|
return &wrapper.Data, nil
|
|
}
|
|
|
|
// AgentConfig 合并配置响应(v2.2)。
|
|
//
|
|
// 旧版 Agent 每 5 秒要分别请求 tasks/poll、health-probes/config、port-policy
|
|
// 三个接口,配置类请求量是上报本身的 6 倍;合并后单次请求即可拿到全部配置。 // Logan.Li
|
|
type AgentConfig struct {
|
|
ConfigVersion string `json:"config_version"`
|
|
ReportInterval int `json:"report_interval"`
|
|
ConfigPollInterval int `json:"config_poll_interval"`
|
|
Tasks []Task `json:"tasks"`
|
|
HealthProbes []model.ProbeConfig `json:"health_probes"`
|
|
PortPolicy *model.PortPolicy `json:"port_policy,omitempty"`
|
|
}
|
|
|
|
// PollConfig 一次性拉取任务 + 探针 + 端口策略 + 采集频率(v2.2 合并接口)。
|
|
// 若服务端尚未升级(404),调用方应回退到分接口拉取。 // Logan.Li
|
|
func (c *Client) PollConfig(ctx context.Context) (*AgentConfig, error) {
|
|
path := configPath + "?hostname=" + url.QueryEscape(c.hostname)
|
|
|
|
var wrapper struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data AgentConfig `json:"data"`
|
|
}
|
|
if err := c.client.DoJSON(ctx, "GET", path, nil, &wrapper); err != nil {
|
|
return nil, fmt.Errorf("配置拉取失败: %w", err)
|
|
}
|
|
return &wrapper.Data, nil
|
|
}
|