配置轮询每 30s 都会收到相同的 report_interval 并触发 ticker.Reset, 而 Reset 会重新计时 —— 30s 的 ticker 永远等不到触发, 实际上报被无形拖长到 60~90s(实测 17 次/60s,应为 52 次)。 改为记录已生效的间隔,仅在值真正变化时才 Reset。 配置轮询间隔同样处理,否则每次轮询都 Reset 会让 ticker 永不触发。 验证:升级后上报 53 次/60s,Redis 采样时间戳精确 30s 间隔。 // Logan.Li
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 到后端
- 任务层:独立协程轮询后端任务(脚本执行、探针配置拉取)
快速开始
# 输出全部上报指标(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)。
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
安装
# 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 -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>'"
手动下载:
Invoke-WebRequest -Uri http://10.10.10.75:8080/agent/aiops-agent-windows-amd64.exe -OutFile C:\ProgramData\AIOps\aiops-agent.exe -UseBasicParsing
本地构建
cd agent
make # CGO_ENABLED=0 静态编译当前平台
顶层 ./run.sh build agent 会交叉编译 linux-amd64、linux-arm64、windows-amd64.exe。Docker 镜像内置同样的三个平台 Agent 二进制,覆盖主流服务器架构。// Logan.Li