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

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

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

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

168 lines
7.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# AIOps Agent
轻量主机采集代理,定时采集 CPU/内存/磁盘/网络/端口/进程 等指标,通过 HTTP 上报至 AIOps 后端。
## 目录结构
```
agent/
├── main.go # 入口:CLI 解析、采集编排、上报循环
├── Makefile # 本地构建(单架构)
│
├── collector/ # 采集器 —— 从 /proc /sys netstat 读原始数据
│ ├── cpu.go # CPU 使用率、核数、负载
│ ├── mem.go # 内存 total/used/cached
│ ├── swap.go # Swap 分区
│ ├── disk.go # 磁盘(按挂载点)
│ ├── diskio.go # 磁盘 IO(读写速率/IOPS)
│ ├── network.go # TCP 连接状态、网卡流量
│ ├── port.go # 监听端口发现
│ ├── proctop.go # 进程 TOP N(按 CPU 排序)
│ ├── fd.go # 系统文件句柄使用量
│ ├── context.go # 上下文切换/中断次数
│ ├── health_probe.go # 健康探针(HTTP GET / TCP / CMD)
│ ├── system.go # 系统信息(OS/Kernel/Uptime)
│ └── util.go # 通用工具(读文件、解析等)
│
├── model/ # 数据模型
│ ├── model.go # Report 结构体(一次采集的完整报文)
│ ├── model_ext.go # NetworkStat / DiskIOStat / SwapStat 等
│ └── model_proctop.go # ProcessStat(含 Cmdline/User/CPU%/Mem%/FD)
│
├── client/ # HTTP 客户端(连接池复用、Keep-Alive)
│ └── http.go
├── reporter/ # 上报器 —— 将 Report JSON 序列化后 POST 到后端
│ └── http.go
├── task/ # 任务系统(后端下发命令,Agent 执行并回传结果)
│ ├── client.go
│ └── executor.go
│
├── deploy/ # 部署文件
│ ├── install.sh # 一键安装脚本(自动检测架构)
│ └── aiops-agent.service # systemd 服务模板
│
├── bin/ # 编译产物(Docker 多架构构建后拷贝)
├── go.mod / go.sum # Go 依赖
└── README.md
```
## 架构
```
┌─────────────┐ 采集 ┌──────────────────────────────┐
│ /proc/cpu │──────────▶│ collector/ │
│ /proc/mem │ │ cpu / mem / disk / net / ... │
│ netstat │ └──────────┬───────────────────┘
│ /sys/... │ │ Go struct
└─────────────┘ ▼
┌────────────────┐
│ model.Report │ ← 一次采集的完整数据
└───────┬────────┘
│ JSON 序列化
▼
┌────────────────┐
│ reporter/ │ ← HTTP POST /api/v1/agent/report
└───────┬────────┘
│
▼
┌─────────────────────┐
│ AIOps Backend │
│ (10.10.10.75:8080) │
└─────────────────────┘
```
- **采集层**:从 `/proc`、`/sys`、`netstat` 等系统接口读取原始数据
- **模型层**:统一的数据结构 `Report`,涵盖系统指标、进程 TOP、端口、FD、探针等
- **上报层**:共享 HTTP 连接池,JSON 序列化后 POST 到后端
- **任务层**:独立协程轮询后端任务(脚本执行、探针配置拉取)
## 快速开始
```bash
# 输出全部上报指标(Zabbix-agent -p 风格),调试用
./aiops-agent output
# 按 CPU 占用输出 TOP 5 进程
./aiops-agent output -top-n 5
# 正常采集上报(daemon)
./aiops-agent -server http://10.10.10.75:8080 -token <TOKEN> -interval 30
```
## 参数
| 参数 | 默认 | 说明 |
|------|------|------|
| `-server` | (空) | 后端地址,daemon 模式必填 |
| `-token` | (空) | Bearer 鉴权令牌 |
| `-hostname` | 系统主机名 | 上报标识 |
| `-interval` | `10` | 采集间隔(秒),建议 30~60 |
| `-top-n` | `3` | 进程 TOP N,按 CPU 占用排序 |
## output 输出
格式:`key [type|value]`,type=`s`(string) `u`(uint) `d`(double)。
```text
agent.hostname [s|opsx-server]
system.hostname [s|opsx-server]
agent.version [s|v2.1.0]
agent.ip [s|10.10.10.75]
system.os [s|Ubuntu 24.04 LTS]
system.kernel [s|6.8.0-90-generic]
system.uptime [u|603932]
cpu.usage_percent [d|0.00]
cpu.core_count [u|8]
cpu.load1 / load5 / load15 [d|...]
memory.total_gb / used_gb / usage_percent [d|...]
swap.total_gb / used_gb / usage_percent [d|...]
disk[/].total_gb / used_gb / usage_percent [d|...] (每挂载点一行)
ports.discovery[*] [s|{JSON}] (Zabbix LLD 格式)
net.listen[PORT].process / .protocol [s|...]
net.tcp.established / time_wait / close_wait [u|...]
net.rx_bps / tx_bps [d|...]
disk_io.read_bps / write_bps / read_iops / write_iops
proc.top[N].name / pid / user / cpu_pct / mem_pct / fd_count / threads
fd.allocated / max / usage_pct [u|...]
ctx.ctx_per_sec / irq_per_sec [d|...]
agent.heartbeat.version / uptime_sec / self_rss_mb / report_seq
```
## 安装
```bash
# x86_64
curl -o /usr/local/bin/aiops-agent http://10.10.10.75:8080/agent/aiops-agent-linux-amd64
# ARM64
curl -o /usr/local/bin/aiops-agent http://10.10.10.75:8080/agent/aiops-agent-linux-arm64
# 一键安装(自动检测架构并注册 systemd)
curl -fsSL http://10.10.10.75:8080/agent/install.sh | sudo bash -s -- --server http://10.10.10.75:8080 --token <TOKEN>
```
### Windows
Windows Agent 使用同一套 `/api/v1/agent/report` 上报协议,主机监控图表不需要单独分叉。第一版已支持 CPU、内存、逻辑磁盘、监听端口、TCP 连接数、进程 TOP、系统信息、健康探针和 PowerShell 任务执行;Linux 专属的 Load、IOWait、inode、FD、磁盘 IO 等字段在 Windows 上会保持 0 或空值。// Logan.Li
在管理员 PowerShell 中执行:
```powershell
powershell -ExecutionPolicy Bypass -Command "iwr http://10.10.10.75:8080/agent/install.ps1 -UseBasicParsing -OutFile $env:TEMP\install-aiops-agent.ps1; & $env:TEMP\install-aiops-agent.ps1 -Server 'http://10.10.10.75:8080' -Token '<TOKEN>'"
```
手动下载:
```powershell
Invoke-WebRequest -Uri http://10.10.10.75:8080/agent/aiops-agent-windows-amd64.exe -OutFile C:\ProgramData\AIOps\aiops-agent.exe -UseBasicParsing
```
## 本地构建
```bash
cd agent
make # CGO_ENABLED=0 静态编译当前平台
```
顶层 `./run.sh build agent` 会交叉编译 `linux-amd64`、`linux-arm64`、`darwin-arm64`、`windows-amd64.exe`。Docker 镜像会携带当前目标架构 Linux Agent 和 Windows AMD64 Agent。// Logan.Li