Glances 跨平台系统监控教程
Glances 是一款基于 Python 开发的现代系统监控工具,它在一个终端界面中汇聚了 CPU、内存、磁盘、网络、进程、Docker 容器等几乎所有系统信息。Glances 支持终端模式、Web 浏览器模式和 REST API 模式,还可以将数据导出到 InfluxDB、Prometheus 等时序数据库。在搬瓦工 VPS 上部署 Glances,你可以获得一个轻量但功能全面的监控方案。
一、安装
1.1 通过 pip 安装(推荐)
# 安装 pip
apt update
apt install python3-pip -y
# 安装 Glances(包含所有可选依赖)
pip3 install glances[all]
# 或最小安装
pip3 install glances
1.2 通过包管理器安装
# Ubuntu / Debian
apt install glances -y
# CentOS / RHEL
yum install epel-release -y
yum install glances -y
1.3 通过 Docker 运行
docker run --rm -it --pid host --network host -v /var/run/docker.sock:/var/run/docker.sock:ro nicolargo/glances:latest
二、终端模式
# 启动终端监控
glances
# 指定刷新间隔(秒)
glances -t 5
# 排序方式
glances --sort-processes cpu # 按 CPU 排序
glances --sort-processes mem # 按内存排序
glances --sort-processes io # 按 IO 排序
2.1 快捷键
- 1:显示每个 CPU 核心的使用率
- 2:显示左侧边栏
- 3:切换快速查看栏
- a:自动排序进程
- c:按 CPU 排序
- m:按内存排序
- i:按 I/O 排序
- p:按进程名排序
- d:显示/隐藏磁盘 I/O
- n:显示/隐藏网络
- s:显示/隐藏传感器
- D:显示/隐藏 Docker 容器
- f:显示/隐藏文件系统
- e:显示进程的扩展信息
- z:切换进程数量
- h:显示帮助
- q:退出
三、Web 模式
Glances 内置 Web 服务器,可以通过浏览器远程监控:
# 启动 Web 模式(默认端口 61208)
glances -w
# 指定绑定地址和端口
glances -w -B 0.0.0.0 -p 61208
# 设置密码保护
glances -w --password
# 浏览器访问
# http://your-vps-ip:61208
3.1 使用 Systemd 管理 Web 模式
cat > /etc/systemd/system/glances.service <<EOF
[Unit]
Description=Glances System Monitor
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/glances -w -B 0.0.0.0 --disable-plugin sensors
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable glances
systemctl start glances
systemctl status glances
3.2 配合 Nginx 反向代理
# /etc/nginx/conf.d/glances.conf
server {
listen 443 ssl;
server_name monitor.example.com;
ssl_certificate /etc/letsencrypt/live/monitor.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/monitor.example.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:61208;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# 基本认证
auth_basic "Glances Monitor";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
四、API 模式
# Glances Web 模式自带 REST API
# 获取所有数据
curl http://localhost:61208/api/3/all
# 获取 CPU 数据
curl http://localhost:61208/api/3/cpu
# 获取内存数据
curl http://localhost:61208/api/3/mem
# 获取磁盘数据
curl http://localhost:61208/api/3/diskio
# 获取网络数据
curl http://localhost:61208/api/3/network
# 获取进程列表
curl http://localhost:61208/api/3/processlist
五、数据导出
# 导出到 CSV 文件
glances --export csv --export-csv-file /tmp/glances.csv
# 导出到 JSON 文件
glances --export json --export-json-file /tmp/glances.json
# 导出到 InfluxDB
pip3 install influxdb-client
glances --export influxdb2 -C /etc/glances/glances.conf
六、配置文件
# 创建配置文件
mkdir -p /etc/glances
cat > /etc/glances/glances.conf <<'EOF'
[global]
refresh=3
[cpu]
careful=50
warning=70
critical=90
[mem]
careful=50
warning=70
critical=90
[diskio]
hide=loop.*
[network]
hide=lo,docker.*
[docker]
disable=false
[processlist]
sort_key=cpu_percent
EOF
七、Docker 容器监控
# Glances 可以显示 Docker 容器的资源使用
# 启动时确保挂载 Docker socket
glances --enable-plugin docker
# 在交互界面中按 D 切换 Docker 容器列表
# 显示每个容器的 CPU、内存、网络使用情况
八、告警配置
Glances 使用颜色区分告警级别:
- 绿色(OK):正常范围
- 蓝色(CAREFUL):需要关注
- 紫色(WARNING):警告级别
- 红色(CRITICAL):严重级别
总结
Glances 是功能最全面的现代系统监控工具,一个界面即可纵览所有系统资源。它的 Web 模式和 API 模式使其特别适合远程监控搬瓦工 VPS。如果你需要更专项的监控,可以配合 Atop 进程监控、NetHogs 网络监控 和 Cockpit Web 管理面板 一起使用。选购搬瓦工 VPS 请参考全部方案,使用优惠码 NODESEEK2026 可享受 6.77% 循环折扣。