feat: 完善堡垒机与 Agent 智能运维能力
- 支持 Windows Agent 构建、安装脚本和统一上报协议 - 增强 RDP 剪贴板、虚拟盘和文件传输能力 - 新增 AIOps 上下文工具、告警 RCA trace 落库和回放接口 - 重构 docs 模块文档并补充终端、堡垒机、监控、AI Agent 设计
This commit is contained in:
1 parent
a9513c2056
commit
e0d652d211
169 files changed
+12192
-9116
No files matched your search
@@ -63,8 +63,8 @@ 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"`
|
||||
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 {
|
||||
|
||||
+2
-22
@@ -1,3 +1,5 @@
|
||||
//go:build !windows
|
||||
|
||||
// Package task 实现 AIOps Agent 的任务拉取与执行能力。
|
||||
// Agent 通过轮询接口从后端获取待执行任务(命令/脚本),本地执行后回传结果。
|
||||
//
|
||||
@@ -13,28 +15,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 任务类型常量
|
||||
const (
|
||||
TypeCommand = "command" // 单条 shell 命令
|
||||
TypeScript = "script" // 多行 bash 脚本
|
||||
)
|
||||
|
||||
// Task 表示从后端拉取到的待执行任务。
|
||||
type Task struct {
|
||||
TaskID string `json:"task_id"` // 任务唯一标识,回传结果时使用
|
||||
Type string `json:"type"` // 任务类型:command / script
|
||||
Command string `json:"command"` // 命令内容(command 时为单条命令,script 时为脚本正文)
|
||||
Timeout int `json:"timeout"` // 执行超时,单位:秒;<=0 表示不限制
|
||||
}
|
||||
|
||||
// Result 表示任务执行结果,用于回传后端。
|
||||
type Result struct {
|
||||
TaskID string `json:"task_id"` // 对应的任务 ID
|
||||
ExitCode int `json:"exit_code"` // 进程退出码;超时/启动失败时为 -1
|
||||
Result string `json:"result"` // 合并后的 stdout+stderr 输出
|
||||
Status string `json:"status"` // 执行状态:success / failed / timeout
|
||||
}
|
||||
|
||||
// Executor 负责本地执行任务并产出结果。
|
||||
type Executor struct {
|
||||
logger *log.Logger
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
//go:build windows
|
||||
|
||||
// Package task 实现 Windows Agent 的 PowerShell 任务执行能力。 // Logan.Li
|
||||
package task
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Executor struct {
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func New(logger *log.Logger) *Executor {
|
||||
return &Executor{logger: logger}
|
||||
}
|
||||
|
||||
func (e *Executor) ExecuteTask(ctx context.Context, t Task) Result {
|
||||
res := Result{TaskID: t.TaskID}
|
||||
execCtx, cancel := e.withTimeout(ctx, t.Timeout)
|
||||
defer cancel()
|
||||
|
||||
var cmd *exec.Cmd
|
||||
var cleanup func()
|
||||
switch t.Type {
|
||||
case TypeCommand:
|
||||
cmd = exec.CommandContext(execCtx, "powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", t.Command)
|
||||
case TypeScript:
|
||||
path, err := writeTempPowerShell(t.Command)
|
||||
if err != nil {
|
||||
res.ExitCode = -1
|
||||
res.Status = "failed"
|
||||
res.Result = "写入临时 PowerShell 脚本失败: " + err.Error()
|
||||
return res
|
||||
}
|
||||
cleanup = func() { _ = os.Remove(path) }
|
||||
cmd = exec.CommandContext(execCtx, "powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", path)
|
||||
default:
|
||||
res.ExitCode = -1
|
||||
res.Status = "failed"
|
||||
res.Result = fmt.Sprintf("未知的任务类型: %q", t.Type)
|
||||
return res
|
||||
}
|
||||
if cleanup != nil {
|
||||
defer cleanup()
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
cmd.Stdout = &buf
|
||||
cmd.Stderr = &buf
|
||||
start := time.Now()
|
||||
err := cmd.Run()
|
||||
elapsed := time.Since(start)
|
||||
if execCtx.Err() == context.DeadlineExceeded {
|
||||
res.ExitCode = -1
|
||||
res.Status = "timeout"
|
||||
res.Result = fmt.Sprintf("任务执行超时(%ds),已输出:\n%s", t.Timeout, buf.String())
|
||||
e.logger.Printf("任务 %s 执行超时(%ds)", t.TaskID, t.Timeout)
|
||||
return res
|
||||
}
|
||||
res.ExitCode = exitCode(err)
|
||||
res.Result = buf.String()
|
||||
if res.ExitCode == 0 {
|
||||
res.Status = "success"
|
||||
e.logger.Printf("任务 %s 执行成功 exit=0 耗时=%s 输出=%dB", t.TaskID, elapsed, buf.Len())
|
||||
} else {
|
||||
res.Status = "failed"
|
||||
e.logger.Printf("任务 %s 执行失败 exit=%d 耗时=%s 输出=%dB", t.TaskID, res.ExitCode, elapsed, buf.Len())
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (e *Executor) withTimeout(parent context.Context, timeout int) (context.Context, context.CancelFunc) {
|
||||
if timeout <= 0 {
|
||||
return parent, func() {}
|
||||
}
|
||||
return context.WithTimeout(parent, time.Duration(timeout)*time.Second)
|
||||
}
|
||||
|
||||
func exitCode(err error) int {
|
||||
if err == nil {
|
||||
return 0
|
||||
}
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
return exitErr.ExitCode()
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func writeTempPowerShell(script string) (string, error) {
|
||||
path := filepath.Join(os.TempDir(), fmt.Sprintf("aiops-agent-task-%d.ps1", time.Now().UnixNano()))
|
||||
if err := os.WriteFile(path, []byte(script), 0600); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Package task 定义 Agent 远程任务类型。 // Logan.Li
|
||||
package task
|
||||
|
||||
const (
|
||||
TypeCommand = "command" // 单条 shell/PowerShell 命令 // Logan.Li
|
||||
TypeScript = "script" // 多行脚本 // Logan.Li
|
||||
)
|
||||
|
||||
// Task 表示从后端拉取到的待执行任务。 // Logan.Li
|
||||
type Task struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Type string `json:"type"`
|
||||
Command string `json:"command"`
|
||||
Timeout int `json:"timeout"`
|
||||
}
|
||||
|
||||
// Result 表示任务执行结果,用于回传后端。 // Logan.Li
|
||||
type Result struct {
|
||||
TaskID string `json:"task_id"`
|
||||
ExitCode int `json:"exit_code"`
|
||||
Result string `json:"result"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
Reference in new issue
Block a user