Files
aiops/backend/pkg/agentcli/tools/aiops_context.go
T
Logan.Li e0d652d211 feat: 完善堡垒机与 Agent 智能运维能力
- 支持 Windows Agent 构建、安装脚本和统一上报协议

- 增强 RDP 剪贴板、虚拟盘和文件传输能力

- 新增 AIOps 上下文工具、告警 RCA trace 落库和回放接口

- 重构 docs 模块文档并补充终端、堡垒机、监控、AI Agent 设计
2026-07-05 23:49:36 +08:00

136 lines
5.5 KiB
Go

package tools
import (
"context"
"encoding/json"
"fmt"
)
// AIOpsContextProvider 是 Agent CLI 调用平台上下文的最小接口,具体查库实现在 pkg/aiopscontext。 // Logan.Li
type AIOpsContextProvider interface {
SearchAssets(ctx context.Context, keyword string, limit int) (interface{}, error)
ListAlerts(ctx context.Context, target, severity, status, timeRange string, limit int) (interface{}, error)
MetricSummary(ctx context.Context, target, metric, timeRange string, limit int) (interface{}, error)
}
// NewAIOpsAssetSearchTool 创建资产搜索工具。 // Logan.Li
func NewAIOpsAssetSearchTool(provider AIOpsContextProvider) Tool {
return &aiopsAssetSearchTool{provider: provider}
}
// NewAIOpsAlertListTool 创建告警查询工具。 // Logan.Li
func NewAIOpsAlertListTool(provider AIOpsContextProvider) Tool {
return &aiopsAlertListTool{provider: provider}
}
// NewAIOpsMetricSummaryTool 创建指标摘要工具。 // Logan.Li
func NewAIOpsMetricSummaryTool(provider AIOpsContextProvider) Tool {
return &aiopsMetricSummaryTool{provider: provider}
}
type aiopsAssetSearchTool struct {
provider AIOpsContextProvider
}
func (t *aiopsAssetSearchTool) Name() string { return "aiops_asset_search" }
func (t *aiopsAssetSearchTool) Description() string {
return "搜索 AIOps 平台 CMDB/资产库中的服务器资产,返回脱敏后的主机名、IP、协议、系统、状态、Agent 状态和资源使用率。排障前应先用它确认目标对象。"
}
func (t *aiopsAssetSearchTool) Parameters() map[string]interface{} {
return map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"keyword": map[string]interface{}{"type": "string", "description": "主机名、IP、标签或描述关键字,可为空"},
"limit": map[string]interface{}{"type": "integer", "description": "返回数量,默认 10,最大 50"},
},
}
}
func (t *aiopsAssetSearchTool) RiskLevel() RiskLevel { return RiskSafe }
func (t *aiopsAssetSearchTool) Execute(ctx context.Context, args map[string]interface{}) (*Result, error) {
data, err := t.provider.SearchAssets(ctx, StringParam(args, "keyword"), IntParam(args, "limit"))
return jsonResult(data, err)
}
type aiopsAlertListTool struct {
provider AIOpsContextProvider
}
func (t *aiopsAlertListTool) Name() string { return "aiops_alert_list" }
func (t *aiopsAlertListTool) Description() string {
return "查询 AIOps 平台近期告警,合并平台告警和 Agent 告警。可按目标、级别、状态、时间范围过滤,用于判断故障是否与近期告警相关。"
}
func (t *aiopsAlertListTool) Parameters() map[string]interface{} {
return map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"target": map[string]interface{}{"type": "string", "description": "主机名、IP、目标名称,可为空"},
"severity": map[string]interface{}{"type": "string", "description": "critical/warning/info,可为空"},
"status": map[string]interface{}{"type": "string", "description": "firing/resolved/active,可为空"},
"time_range": map[string]interface{}{"type": "string", "description": "15m/30m/1h/2h/6h/24h/7d,默认 24h"},
"limit": map[string]interface{}{"type": "integer", "description": "返回数量,默认 20,最大 100"},
},
}
}
func (t *aiopsAlertListTool) RiskLevel() RiskLevel { return RiskSafe }
func (t *aiopsAlertListTool) Execute(ctx context.Context, args map[string]interface{}) (*Result, error) {
data, err := t.provider.ListAlerts(ctx,
StringParam(args, "target"), StringParam(args, "severity"),
StringParam(args, "status"), StringParam(args, "time_range"),
IntParam(args, "limit"))
return jsonResult(data, err)
}
type aiopsMetricSummaryTool struct {
provider AIOpsContextProvider
}
func (t *aiopsMetricSummaryTool) Name() string { return "aiops_metric_summary" }
func (t *aiopsMetricSummaryTool) Description() string {
return "查询主机 Agent 指标摘要,返回 latest/min/max/avg/p95/trend 和少量采样点。适合验证 CPU、内存、load 等告警是否真实持续。"
}
func (t *aiopsMetricSummaryTool) Parameters() map[string]interface{} {
return map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"target": map[string]interface{}{"type": "string", "description": "主机名或 IP,必填"},
"metric": map[string]interface{}{"type": "string", "description": "cpu_usage/mem_usage/load1/load5/load15/cpu_iowait,默认 cpu_usage"},
"time_range": map[string]interface{}{"type": "string", "description": "15m/30m/1h/2h/6h/24h/7d,默认 1h"},
"limit": map[string]interface{}{"type": "integer", "description": "最多读取多少个样本,默认 240"},
},
"required": []string{"target"},
}
}
func (t *aiopsMetricSummaryTool) RiskLevel() RiskLevel { return RiskSafe }
func (t *aiopsMetricSummaryTool) Execute(ctx context.Context, args map[string]interface{}) (*Result, error) {
if StringParam(args, "target") == "" {
return &Result{Success: false, Error: "target 不能为空", Output: ""}, nil
}
data, err := t.provider.MetricSummary(ctx,
StringParam(args, "target"), StringParam(args, "metric"),
StringParam(args, "time_range"), IntParam(args, "limit"))
return jsonResult(data, err)
}
func jsonResult(data interface{}, err error) (*Result, error) {
if err != nil {
return &Result{Success: false, Error: err.Error(), Output: err.Error()}, nil
}
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return nil, fmt.Errorf("JSON 序列化失败: %w", err)
}
return &Result{Success: true, Output: string(b)}, nil
}