# ops-agent-go **Repository Path**: quickn/ops-agent-go ## Basic Information - **Project Name**: ops-agent-go - **Description**: 采用go语言写个运维终端 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-22 - **Last Updated**: 2026-06-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ops-agent-go 服务器运维终端 —— 基于 Go 语言开发,通过 RabbitMQ 与 monitor-server 通信。 ## 核心功能 ### 1. 定时采集系统信息 → 通过 RabbitMQ 上报 | 采集项 | 数据字段 | 参考 Java 类 | Go 实现方式 | |--------|----------|-------------|------------| | **CPU** | 使用率(sys)、空闲率(idle)、IO等待(iowait) | `CpuState.java` | `gopsutil/cpu.Percent()` | | **内存** | 总量(total)、已用(used)、可用(free)、使用率(usePer) | `MemState.java` | `gopsutil/mem.VirtualMemory()` | | **磁盘** | 盘符(fileSystem)、大小(size)、已用(used)、可用(avail) | `DeskState.java` | `gopsutil/disk.Usage()` | | **网络IO** | 接收/发送速率(rxbyt/txbyt)、包数(rxpck/txpck) | `NetIoState.java` | `gopsutil/net.IOCounters()` 两次采样取差值 | | **系统负载** | 1/5/15分钟负载(oneLoad/fiveLoad/fifteenLoad) | `SysLoadState.java` | `gopsutil/load.Avg()`(仅Linux) | | **进程数** | 总进程数(totalCount)、运行中(runningCount) | 新增字段 | `gopsutil/process.Pids()` | ### 2. 接收 Shell 命令 → 执行 → 返回结果 - 监听服务端通过 RabbitMQ 下发的 shell 命令 - 跨平台执行(Windows 用 `cmd.exe`,Linux 用 `/bin/sh`) - 执行结果通过 RabbitMQ 返回给服务端 ## 项目结构 ``` ops-agent-go/ ├── .gitignore # Git忽略规则 ├── config.yaml # 配置文件(修改此文件即可调整参数) ├── go.mod # Go模块定义 ├── go.sum # 依赖锁定文件 ├── main.go # 入口文件,整合所有模块 └── internal/ # 内部包 ├── config/ │ └── config.go # 配置结构体定义与 YAML 加载 ├── model/ │ ├── system.go # 数据模型:CPU/内存/磁盘/网络/进程/负载 │ └── message.go # MQ消息模型:MsgResult/CmdRequest/HeartBeat ├── collector/ │ ├── collector.go # 采集器:各采集函数的入口 │ └── platform_common.go # 平台实现:基于 gopsutil 库 ├── mq/ │ └── rabbitmq.go # RabbitMQ 客户端:连接/监听/响应/重连/上报 ├── executor/ │ └── executor.go # Shell命令执行器:跨平台 └── scheduler/ └── scheduler.go # 定时调度器:按间隔触发采集并回调上报 ``` ## RabbitMQ 通信协议 对标 monitor-server 项目的 MQ 协议: ``` 服务端 → Agent (指令下发) Exchange: exchange.monitor.cmd RoutingKey: routing.monitor.cmd.key.{agentId} 消息头: serverIp, msgType, timeout Agent → 服务端 (指令响应) Exchange: exchange.monitor.cmd.server RoutingKey: routingKey.monitor.cmd.server.{serverIp} 消息体: {"code":0,"data":"...","agentId":123,"msgType":"cmd"} Agent → 服务端 (数据上报) Exchange: exchange.monitor.data RoutingKey: routing.monitor.data 消息体: CollectData JSON ``` ## 快速开始 ### 前置条件 - Go 1.22+ - RabbitMQ 服务 ### 1. 修改配置 编辑 `config.yaml`,填写以下关键项: ```yaml agent: id: 1 # 改为服务端分配的 Agent ID service_id: 1 # 改为服务端对应的 serviceId rabbitmq: url: "amqp://guest:guest@127.0.0.1:5672/" # RabbitMQ 连接地址 ``` ### 2. 命令行运行 ```bash # 直接运行(无需编译,适合快速调试) go run main.go -c config.yaml # 编译后运行 go build -o ops-agent-go.exe . # Windows ./ops-agent-go.exe -c config.yaml # 查看版本 ./ops-agent-go.exe -v ``` ### 3. GoLand 运行调试 右键 `main.go` → **Run 'go build main.go'** 即可启动。 断点调试: 1. 在代码行号左侧单击添加断点 2. 右键 `main.go` → **Debug 'go build main.go'** 3. 如需传入配置文件参数:顶部工具栏 → **Edit Configurations** → 在 `Program arguments` 填入 `-c config.yaml` ### 4. VS Code 运行调试 在项目根目录创建 `.vscode/launch.json`: ```json { "version": "0.2.0", "configurations": [ { "name": "Launch ops-agent-go", "type": "go", "request": "launch", "mode": "debug", "program": "${workspaceFolder}/main.go", "args": ["-c", "config.yaml"] } ] } ``` 然后按 `F5` 即可启动调试。 ## 常见问题 ### 关键配置项 (config.yaml) | 配置项 | 说明 | 默认值 | |--------|------|--------| | `agent.id` | Agent ID(服务端分配) | 0 | | `agent.service_id` | 服务ID | 1 | | `rabbitmq.url` | RabbitMQ连接地址 | `amqp://guest:guest@127.0.0.1:5672/` | | `collector.interval` | 采集间隔(秒) | 60 | | `collector.enabled.cpu` | 是否采集CPU | true | | `collector.enabled.memory` | 是否采集内存 | true | | `collector.enabled.disk` | 是否采集磁盘 | true | | `collector.enabled.network` | 是否采集网络IO | true | | `collector.enabled.process` | 是否采集进程数 | true | | `collector.enabled.sysload` | 是否采集系统负载 | true | ## 技术栈 | 依赖 | 用途 | |------|------| | [gopsutil](https://github.com/shirou/gopsutil) | 跨平台系统信息采集 | | [amqp091-go](https://github.com/rabbitmq/amqp091-go) | RabbitMQ 客户端 | | [yaml.v3](https://gopkg.in/yaml.v3) | YAML 配置文件解析 | ## 常见问题 ### Q: 启动后报 "连接RabbitMQ失败" 检查 `config.yaml` 中 `rabbitmq.url` 是否正确,确认 RabbitMQ 服务已启动: ```bash # 检查 RabbitMQ 状态(Linux) systemctl status rabbitmq-server # Windows 检查服务是否运行 Get-Service RabbitMQ ``` ### Q: 采集网络 IO 时卡住很久 网络 IO 采集需要两次采样取差值(间隔 3 秒),这是正常行为,与 Java 版 monitor-agent 的 [OshiUtil.net()](file:///E:/gitee/monitor-agent/src/main/java/com/bszn/monitor/OshiUtil.java#L184) 逻辑一致。 ### Q: Windows 下中文输出乱码 Go 程序默认使用 UTF-8,cmd.exe 输出可能是 GBK 编码。命令执行结果中的中文可能在终端显示乱码,但不影响数据上报。 ### Q: 如何临时关闭某个采集项 编辑 `config.yaml`,将对应项设为 `false`: ```yaml collector: enabled: network: false # 关闭网络 IO 采集 sysload: false # Windows 下可关闭系统负载 ``` 修改后重启程序即可生效。 ## 对标项目 - monitor-agent (Java) — 数据采集和上报逻辑参考 - monitor-server (Java) — RabbitMQ 通信协议参考