# Glance Python 远程服务器监控 Agent ## 项目简介 本项目为 Glance Dashboard 提供远程服务器监控数据采集,使用 Python3 + FastAPI 实现,兼容 Glance `server-stats` 组件前端展示。支持 Docker 部署,适合多服务器统一监控。 ## 功能特性 - 采集主机名、平台、启动时间、CPU 负载/温度、内存、磁盘等信息 - 提供 `/api/sysinfo/all` HTTP API,返回 Glance 兼容 JSON - 支持 Docker 镜像和 docker-compose 一键部署 - 可多实例部署,适配多服务器场景 - **主机名自动识别**:容器内自动优先读取宿主机 `/etc/hostname`,无需手动配置 ## API 说明 - 路径:`/api/sysinfo/all` - 方法:GET - 返回: ```json { "Hostname": "server1", "Platform": "Linux-5.15.0-1051-azure-x86_64-with-glibc2.29", "BootTime": 1710000000, "HostInfoIsAvailable": true, "CPU": { "Load1Percent": 12.5, "Load15Percent": 8.2, "TemperatureC": 45.0, "LoadIsAvailable": true, "TemperatureIsAvailable": true }, "Memory": { "IsAvailable": true, "UsedPercent": 55.3, "UsedMB": 2048, "TotalMB": 4096, "SwapIsAvailable": true, "SwapUsedMB": 256, "SwapTotalMB": 1024, "SwapUsedPercent": 25.0 }, "Mountpoints": [ { "Name": "/dev/sda1", "Path": "/", "UsedMB": 10240, "TotalMB": 20480, "UsedPercent": 50.0 } ] } ``` ## Glance 集成方法 1. 将 `agent/custom-api-example.yaml` 内容合并到 Glance 的主配置文件(如 `glance.yml`)对应 widgets 区域。 2. 每台服务器部署一个 agent,配置对应的 API 地址。 3. 前端展示效果与本地 server-stats 一致。 ### custom-api 配置示例 ```yaml - type: custom-api title: 远程服务器A url: http://192.168.1.200:8000/api/sysinfo/all ## 请修改为实际的IP地址 cache: 15s template: |
{{ .JSON.String "Hostname" }}
{{ if .JSON.Bool "HostInfoIsAvailable" }} uptime {{ else }}unknown uptime{{ end }}
PLATFORM
{{ if .JSON.Bool "HostInfoIsAvailable" }} {{ .JSON.String "Platform" }} {{ else }}Unknown{{ end }}
CPU
{{ if and (.JSON.Bool "CPU.TemperatureIsAvailable") (ge (.JSON.Float "CPU.TemperatureC") 80.0) }} {{ end }}
{{ if .JSON.Bool "CPU.LoadIsAvailable" }} {{ .JSON.Float "CPU.Load1Percent" }} % {{ else }}n/a{{ end }}
1M AVG
{{ .JSON.Float "CPU.Load1Percent" }} %
15M AVG
{{ .JSON.Float "CPU.Load15Percent" }} %
{{ if .JSON.Bool "CPU.TemperatureIsAvailable" }}
TEMP C
{{ .JSON.Float "CPU.TemperatureC" }} °
{{ end }}
{{ if .JSON.Bool "CPU.LoadIsAvailable" }}
{{ end }}
RAM
{{ if .JSON.Bool "Memory.IsAvailable" }} {{ .JSON.Float "Memory.UsedPercent" }} % {{ else }}n/a{{ end }}
RAM
{{ .JSON.Float "Memory.UsedGB" }}GB / {{ .JSON.Float "Memory.TotalGB" }}GB
{{ if .JSON.Bool "Memory.SwapIsAvailable" }}
SWAP
{{ .JSON.Float "Memory.SwapUsedGB" }}GB / {{ .JSON.Float "Memory.SwapTotalGB" }}GB
{{ end }}
{{ if .JSON.Bool "Memory.IsAvailable" }}
{{ if .JSON.Bool "Memory.SwapIsAvailable" }}
{{ end }} {{ end }}
DISK
{{ if gt (.JSON.Array "Mountpoints" | len) 0 }} {{ (index (.JSON.Array "Mountpoints") 0).Float "UsedPercent" }} % {{ else }}n/a{{ end }}
    {{ range .JSON.Array "Mountpoints" }}
  • {{ if .String "Name" }}{{ .String "Name" }}{{ else }}{{ .String "Path" }}{{ end }}
    {{ .Float "UsedGB" }}GB / {{ .Float "TotalGB" }}GB
  • {{ end }}
{{ if gt (.JSON.Array "Mountpoints" | len) 0 }}
{{ if gt (.JSON.Array "Mountpoints" | len) 1 }}
{{ end }} {{ end }}
``` ## 参考 - [Glance-Monitor](https://github.com/arch3rPro/Glance-Monitor) - [Document文档](https://github.com/arch3rPro/Glance-Monitor/blob/main/server-status-agent/README.md)