CPU 根因:每个 Agent 请求都做一次 bcrypt 校验(实测 72ms), 30 台主机 18 req/s × 72ms ≈ 1.35 核,实测容器 CPU 135%。 - agentauth: 校验结果按 Token 哈希缓存(TTL 5min,轮换即失效), last_used_at 写入按 60s 节流。实测 CPU 135% → 1.6%,请求延迟 103ms → 1.6ms - metricagg: 新增后台逐级降采样聚合 raw→1m/5m→1h→6h/1d,取代前端手动触发 - metrics_aggregate: 补 uniq_metric_agg 唯一索引(原 idx_agg 非唯一导致 upsert 插重复行) - agent 合并配置接口 GET /agent/config:一次返回任务+探针+端口策略+频率, 配置轮询 5s→30s;服务端可下发 report_interval,Agent 热更新 ticker 无需重装 - retention: agent_reports 保留期 7d→1d(趋势由聚合表承担) - redis: 增加 maxmemory 512mb + volatile-lru - fix(upgrade): 升级脚本改为 fail-fast,修复下载失败仍报 UPGRADE_OK 的静默谎报 // Logan.Li
168 lines
7.7 KiB
Markdown
168 lines
7.7 KiB
Markdown
# 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`、`windows-amd64.exe`。Docker 镜像内置同样的三个平台 Agent 二进制,覆盖主流服务器架构。// Logan.Li
|