- v0.31: AlertContact 重构(channel_type下拉+webhook_url替代三分立字段) - v0.32: Agent ProcessTop 增强(cmdline/user/cpu_pct/mem_pct) - v0.33: 端口监控 Tab(监听端口自动发现+down联动告警) - v0.34: 健康探针(Agent本地执行HTTP/TCP/Exec→结果随心跳上报) - Agent新增: ContextSwitch/FD/HealthProbe采集器 - 后端新增: health_probe handler + Agent model扩展 - HostMonitor新增Tab: 健康探针/端口监控
77 lines
2.3 KiB
Go
77 lines
2.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"
|
|
)
|
|
|
|
// 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
|
|
}
|