Glances:跨平台系统资源监控,浏览器实时查看服务器状态
服务器运行异常时,第一反应通常是 SSH 上去看看 CPU 和内存。htop 虽然好用,但只能在终端里看,无法远程通过浏览器查看,也没有历史告警记录。Glances 解决了这个问题------它提供了一个现代化的 Web UI,可以在浏览器中实时查看服务器的 CPU、内存、磁盘、网络、进程、Docker 容器状态,并且支持 Prometheus 指标导出,无缝接入现有监控体系。
什么是 Glances
Glances 是一个用 Python 编写的跨平台系统监控工具,名字来源于"一眼看清全局"。它有三种运行模式:
- 独立命令行模式:类似 htop,直接在终端运行
- Web Server 模式:启动内置 Web 服务器,通过浏览器访问
- 客户端-服务端模式:在远程服务器运行 Glances 服务端,本地客户端连接查看
与 Netdata 相比,Glances 更轻量,无需持久化数据库,适合作为"即时查看"工具;与 htop 相比,Glances 信息维度更丰富,Web UI 模式无需 SSH 即可访问。
监控维度:
- CPU:总使用率、每核心使用率、IO Wait
- 内存:物理内存、Swap 使用率
- 磁盘 I/O:读写速度、IOPS
- 网络:实时上下行带宽,接口列表
- 进程:CPU/内存占用 Top 列表,支持排序和过滤
- Docker 容器:每个容器的 CPU/内存/网络,无需额外配置
- 传感器:CPU 温度(需要硬件支持)
- 告警历史:自动记录超阈值事件
服务器配置
Glances 资源占用极低,监控工具本身不应消耗太多资源。
推荐使用 雨云服务器 rainyun-com 的 1 核 1GB 机型 ,注册填优惠码 2026off 领 5 折,Glances 在这样的配置上运行非常流畅,即使宿主机资源紧张,监控服务本身也不会拖慢系统。
端口规划:
- 61208:Web UI 端口
- 61209:REST API 端口
部署方式
方式一:Docker 部署(推荐)
bash
mkdir -p /opt/glances/config
cd /opt/glances
创建 /opt/glances/docker-compose.yml:
yaml
version: "3.8"
services:
glances:
image: nicolargo/glances:latest-full
container_name: glances
pid: host
network_mode: host
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- /run/user/1000/podman/podman.sock:/run/user/1000/podman/podman.sock:ro
- ./config/glances.conf:/glances/conf/glances.conf:ro
environment:
- GLANCES_OPT=-w
restart: unless-stopped
关键配置说明:
pid: host:共享主机 PID 命名空间,使 Glances 能看到所有进程network_mode: host:使用主机网络,获取准确的网络统计数据/var/run/docker.sock:挂载 Docker socket,Glances 自动监控所有容器GLANCES_OPT=-w:以 Web Server 模式启动(-w即--webserver)
启动:
bash
docker compose up -d
方式二:直接安装
bash
# Ubuntu/Debian
pip3 install glances[all]
# 以 Web 服务器模式运行
glances -w --port 61208
配置文件
创建 /opt/glances/config/glances.conf:
ini
[global]
# 刷新间隔(秒)
refresh_rate=2
[outputs]
# Web UI 端口
browser_port=61208
[cpu]
# CPU 告警阈值(%)
careful=50
warning=70
critical=90
[mem]
# 内存告警阈值(%)
careful=50
warning=70
critical=90
[memswap]
careful=50
warning=70
critical=90
[load]
# 系统负载告警(基于 CPU 核心数的倍数)
careful=0.7
warning=1.0
critical=5.0
[disk]
# 磁盘使用率告警(%)
careful=50
warning=70
critical=90
[network]
# 网络接口黑名单(不显示这些接口)
blacklist=lo
[docker]
# Docker 容器监控(默认开启)
disable=False
[ports]
# 端口连通性检查
refresh=30
# 检查本地 Web 服务
[[ports_0]]
host=localhost
port=80
description=HTTP
timeout=3
[[ports_1]]
host=localhost
port=443
description=HTTPS
timeout=3
[alert]
# 保留告警历史
disable=False
配置 Caddy 反向代理与基础认证
为 Web UI 添加密码保护和 HTTPS,创建 /opt/glances/Caddyfile:
monitor.yourdomain.com {
basicauth {
# 使用 caddy hash-password 生成密码哈希
admin $2a$14$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}
reverse_proxy localhost:61208 {
header_up X-Real-IP {remote_host}
}
log {
output file /var/log/caddy/glances-access.log
}
}
生成 Caddy 密码哈希:
bash
docker run --rm caddy:2-alpine caddy hash-password --plaintext "YourSecurePassword"
# 将输出的哈希值填入 Caddyfile 的 basicauth 块中
更新 docker-compose.yml 加入 Caddy:
yaml
caddy:
image: caddy:2-alpine
container_name: caddy
network_mode: host
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
restart: unless-stopped
volumes:
caddy_data:
Web UI 功能介绍
访问 http://服务器IP:61208(或配置域名后通过 HTTPS 访问),你将看到:
顶部信息栏:
- 主机名、OS 版本、内核版本
- 运行时间(Uptime)
- CPU 型号和核心数
主面板区域:
- CPU:实时使用率折线图,每个核心独立显示
- 内存/Swap:使用量和百分比
- 磁盘 I/O:各分区读写速度
- 网络 I/O:各网络接口实时带宽
进程列表:
- 按 CPU 或内存排序(点击列标题)
- 显示进程名、PID、用户、CPU%、内存%、状态
- 支持在搜索框过滤特定进程
Docker 容器面板:
当 /var/run/docker.sock 正确挂载后,自动显示所有运行中的容器:
- 容器名称和状态(绿色运行/红色停止)
- 每个容器的 CPU 使用率和内存占用
- 网络 I/O 统计
告警历史(Alerts):
页面底部显示超过告警阈值的历史事件,包括时间、类型(CPU/MEM/DISK)、严重程度(Careful/Warning/Critical)。
REST API 使用
Glances 提供完整的 REST API,端口 61209:
bash
# 获取所有数据
curl http://localhost:61209/api/3/all
# 获取 CPU 数据
curl http://localhost:61209/api/3/cpu
# 返回:{"cpucore": 4, "user": 12.5, "system": 3.2, "idle": 84.3, ...}
# 获取内存数据
curl http://localhost:61209/api/3/mem
# 返回:{"total": 4096, "used": 1234, "free": 2862, "percent": 30.1, ...}
# 获取 Docker 容器列表
curl http://localhost:61209/api/3/docker
# 获取磁盘 I/O
curl http://localhost:61209/api/3/diskio
# 获取网络接口信息
curl http://localhost:61209/api/3/network
# 获取告警历史
curl http://localhost:61209/api/3/alert
Prometheus 集成
Glances 内置 Prometheus 导出器,可以直接将指标推送到 Prometheus。
在 glances.conf 中开启 Prometheus 导出:
ini
[prometheus]
host=0.0.0.0
port=9091
prefix=glances
labels=hostname:`hostname`
在 docker-compose.yml 中暴露 Prometheus 端口:
yaml
services:
glances:
# ... 其他配置 ...
ports:
- "61208:61208"
- "9091:9091"
Prometheus scrape 配置(prometheus.yml):
yaml
scrape_configs:
- job_name: 'glances'
static_configs:
- targets: ['your-server-ip:9091']
scrape_interval: 15s
Grafana 仪表板:
在 Grafana 中导入 Dashboard ID 2387(Glances 官方 Dashboard),即可看到完整的系统监控图表,包括:
- CPU/内存/磁盘/网络的历史趋势
- 进程数量变化
- Docker 容器资源消耗
多服务器监控
在被监控的服务器上以服务端模式运行:
bash
glances -s --port 61209
在管理机上以客户端模式连接:
bash
glances -c 192.168.1.100:61209
或者在管理机的 Web UI 中,通过浏览器查看多个服务器(Glances 支持在 Web 界面切换不同服务器)。
与 Netdata 对比
| 特性 | Glances | Netdata |
|---|---|---|
| 资源占用 | 极低(约 50MB RAM) | 较高(约 200MB+ RAM) |
| 历史数据 | 无(需接 Prometheus) | 内置(短期) |
| Web UI | 简洁,实时 | 功能丰富,有仪表板 |
| 告警 | 基础告警 | 完善的告警系统 |
| 插件系统 | 有,Python 插件 | 有,多语言 |
| 配置复杂度 | 低 | 中 |
| Docker 支持 | 原生 | 原生 |
Glances 更适合快速部署、轻量监控;Netdata 更适合需要长期历史数据和复杂告警的场景。如果已有 Prometheus + Grafana 栈,Glances 作为数据源接入是很好的选择。
常见问题
看不到 Docker 容器?
确认 docker.sock 挂载正确,并且 Glances 容器用户有权限读取:
bash
chmod 666 /var/run/docker.sock
# 或将 glances 用户加入 docker 组
CPU 温度不显示?
安装传感器工具并确保硬件支持:
bash
apt install lm-sensors
sensors-detect
在 glances.conf 中启用传感器插件:
ini
[sensors]
disable=False
总结
Glances 是运维日常工作中不可缺少的"快速体检"工具。不需要复杂的配置,几分钟内就能跑起来,通过浏览器随时随地查看服务器状态。配合 Prometheus + Grafana,Glances 还能成为完整监控体系的数据采集层。