# 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 -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 ``` ## 本地构建 ```bash cd agent make # CGO_ENABLED=0 静态编译当前平台 ``` Docker 多架构构建由顶层 `backend/Dockerfile` 的 `agent-builder` stage 完成(linux-amd64 / linux-arm64 / darwin-arm64)。