glances 4.5.8 配置参数详解
glances 4.5.8 配置文件 glances.conf 全量 57 段解析:阈值体系、告警动作、网络/进程/端口插件参数与生产模板,关键修改均经 REST API 实测验证。
glances 4.5.8 配置参数详解
目标:对 glances 4.5.8 的配置文件 glances.conf 做全量段落解析与关键参数实测验证,覆盖全局参数、输出与 Web 服务参数、全部监控插件段的阈值体系、告警段与 24 个导出器段,并给出可直接落地的生产配置模板。全部示例修改均在 Ubuntu 24.04.5 服务器(pipx 安装 glances 4.5.8)上实测生效。
一、环境信息
- 操作系统:Ubuntu 24.04.5 LTS(x86_64,内核 6.8.0-138-generic,12 CPU 核)
- glances 版本:4.5.8(pipx 安装
glances[all],glances --version实测回显Glances version: 4.5.8 / Glances API version: 4 / PsUtil version: 7.2.2) - 配置文件:
/etc/glances/glances.conf(以 v4.5.8 官方模板初始化,共 1086 行、50+ 配置段,wc -l实测) - 配置模板来源:https://github.com/nicolargo/glances/blob/v4.5.8/conf/glances.conf (查询日期 2026-10-23)
- 验证手段:修改配置后重启 Web 模式,通过 REST API(端口 61208,
/api/4/...)核对配置生效
二、前置条件
本篇是配置专题篇,不重复安装流程,要求读者环境已满足:① glances 4.5.8 已安装且 glances --version 可正常输出(安装实测见 《Ubuntu 24.04 安装 glances 4.5.8 指南》);② 具备 root 或 sudo 写 /etc/glances/ 的权限;③ 用于验证的 REST API 可访问(本篇用 curl http://127.0.0.1:61208/api/4/...,对应 glances -w 已运行)。
初始化系统级配置文件(v4.5.8 官方模板):
mkdir -p /etc/glances
curl -sSL -o /etc/glances/glances.conf \
https://raw.githubusercontent.com/nicolargo/glances/v4.5.8/conf/glances.conf
wc -l /etc/glances/glances.conf
grep -c '^\[' /etc/glances/glances.conf
1086 /etc/glances/glances.conf
57
💡 说明:模板共 57 个配置段(含
[amp_*]应用监控段),几乎全部键都给出了解释注释与默认值,是权威的第一手参数文档。
三、参数查看与修改方式
3.1 配置文件查找顺序
glances 按以下顺序查找配置文件,命中即停(来源:官方文档「Configuration」章节,查询日期 2026-10-23):
- 命令行
-C <path>/--config <path>显式指定; /etc/glances/glances.conf(系统级);~/.config/glances/glances.conf(用户级,root 用户即/root/.config/glances/glances.conf);- Python 安装目录内的系统级路径(pipx 场景在 venv 内)。
显式指定配置文件启动:
glances -C /etc/glances/glances.conf -w
INFO: Uvicorn running on http://0.0.0.0:61208 (Press CTRL+C to quit)
3.2 修改后如何生效
glances 没有在线 reload 机制,修改 glances.conf 后必须重启 glances 进程。实测流程(Web 模式为例):
# 找到运行中的 glances 进程号并停止
ps aux | grep '[g]lances -w'
kill <PID>
# 重新启动
nohup glances -w >/var/log/glances-web.log 2>&1 &
sleep 10
curl -s http://127.0.0.1:61208/api/4/version
"4.5.8"
验证要点:重启后 API 正常返回即配置已随新进程加载;systemd 托管场景用 systemctl restart glances-web.service(见《Ubuntu 24.04 安装 glances 4.5.8 指南》第九章)。
3.3 验证配置加载的三个手段
- API 对照法(本篇主要手段):改阈值/开关后重启,用
/api/4/<插件>检查行为变化(如告警事件、隐藏接口、扫描列表); - debug 日志法:
glances -d -w启动,日志中可见插件加载与配置解析细节; - --issue 法:
glances --issue一次性测试全部插件并输出环境摘要,配置错误会在此暴露。
四、参数总览
glances.conf 采用 INI 风格:[段名] 对应插件/功能,段内每行一个 键=值,# 开头为注释。57 个段按功能分为五组(键的默认值均来自 v4.5.8 官方模板原文):
| 分组 | 段 | 作用 |
|---|---|---|
| 全局与输出 | [global]、[outputs] | 刷新率、历史、插件目录;TUI/WebUI/服务端安全参数 |
| 概览与系统 | [quicklook]、[system]、[cpu]、[percpu]、[gpu]、[npu]、[mpp]、[mem]、[memswap]、[load] | 首屏概览与核心资源监控 |
| 设备与网络 | [network]、[ip]、[connections]、[wifi]、[diskio]、[fs]、[irq]、[folders] | 网卡/IP/连接/磁盘/文件系统/目录 |
| 外设与扩展 | [cloud]、[raid]、[smart]、[hddtemp]、[sensors]、[processcount]、[processlist]、[ports]、[vms]、[containers]、[amps]、[alert] | 温度/RAID/进程/端口扫描/容器/告警 |
| 集群与导出 | [serverlist]、[passwords]、[export] 及 24 个导出器段、[graph]、[amp_*] | 多机浏览、凭据、导出通道 |
阈值通用规则(所有监控段一致):*_careful(绿色正常/黄色注意)→ *_warning(橙色警告)→ *_critical(红色严重);未显式配置时绝大多数插件默认 50/70/90。另外每个段都支持 disable=False 开关与 refresh=<秒> 独立刷新率(不配则用 [global] 的 refresh)。
五、核心参数分类详解
5.1 [global] 全局参数
控制全局行为:统计刷新、版本检查、历史长度与外部插件目录。官方模板默认值与逐键说明:
[global]
refresh=2 # 统计刷新间隔(秒),可被命令行 -t <sec> 覆盖
check_update=true # 是否联网检查 PyPI 上的新版本
history_size=1200 # 历史采样点上限(默认刷新率下约 1 小时)
#strftime_format=%Y-%m-%d %H:%M:%S %Z # 日期显示格式
#plugin_dir=/home/user/dev/plugins # 外部自定义插件目录
refresh:所有插件的缺省刷新率;TUI 模式下交互按键a可在 2s/3s/4s/5s/10s/15s/30s/60s 间临时切换(不落盘)。history_size:决定 TUIh键历史曲线与 API/api/4/<插件>/history的数据量;内存受限服务器可调小。check_update:离线/内网服务器建议改false,避免每次启动请求 PyPI。
实测:把 refresh=2 改为 refresh=1 并重启后,--stdout-json load 的输出节奏变为每秒一行(实测两次采样间隔 1 秒,回显见本篇 5.8 节验证流程)。
5.2 [outputs] 输出与 Web 服务参数
该段管辖 TUI/WebUI/XML-RPC 三类界面的公共外观与 Web 服务安全,是 4.x 安全加固的关键段。模板中注释默认值(均未启用,注释即默认行为):
[outputs]
#separator=True # TUI/WebUI 顶部与下部分隔线
#left_menu=network,wifi,... # WebUI 左侧菜单插件列表
#max_processes_display=25 # WebUI 进程显示条数上限
#disable_bg=True # TUI 关闭背景色
#url_prefix=/glances/ # WebUI 与 API 的 URL 前缀(尾斜杠必须)
#webui_root_path= # WebUI 静态文件根路径(Debian 系包缺静态文件时用)
#cors_origins=* # CORS 允许来源(默认 * 任意来源)
#cors_credentials=False
#cors_methods=*
#cors_headers=*
#ssl_keyfile=./glances.local+3-key.pem # TLS 证书与私钥
#ssl_certfile=./glances.local+3.pem
#jwt_secret_key=your-secure-secret-key-here # JWT 签名密钥(openssl rand -hex 32 生成)
#jwt_expire_minutes=60 # JWT 令牌有效期(分钟)
#webui_allowed_hosts=localhost,127.0.0.1,myserver.example.com # Web/API Host 头白名单
#mcp_path=/mcp # MCP 服务器挂载路径
#mcp_allowed_hosts=localhost,127.0.0.1
#xmlrpc_allowed_hosts=localhost,127.0.0.1 # XML-RPC(-s)Host 头白名单
要点解读:url_prefix 适合反代场景(如 Nginx 把 /glances/ 转发过来);公网暴露时优先配置 webui_allowed_hosts(DNS rebinding 防护)与 cors_origins(收紧跨域),二者缺省均为不限制;JWT 认证用于 WebUI/API 的令牌鉴权,jwt_secret_key 不设时每进程随机生成(重启后旧令牌失效)。
5.3 [quicklook] 概览插件
首屏左上角的 CPU/MEM/LOAD 概览条。默认值:
[quicklook]
disable=False
list=cpu,mem,load # 显示哪些统计(可选 cpu,mem,load,swap,gpu_mem,gpu_proc)
bar_char=▪ # 进度条字符
cpu_careful=50
cpu_warning=70
cpu_critical=90
mem_careful=50
mem_warning=70
mem_critical=90
swap_careful=50
swap_warning=70
swap_critical=90
load_careful=70 # load 阈值按百分比(相对核数)
load_warning=100
load_critical=500
实测:把 cpu_careful=5 后重启,/api/4/quicklook 仍返回概览统计(实测回显含 cpu_name、percpu 数组等字段,glances 4.5.8 的 quicklook 已扩展为带 CPU 型号信息的增强概览),TUI 中 CPU 条即时以黄色高亮——阈值类键的效果直接反映在颜色分级上。
5.4 [system] 系统信息插件
TUI/WebUI 第一行的主机名、操作系统与架构显示:
[system]
disable=False
#refresh=60 # 默认 60 秒刷新(系统信息极少变化)
#system_info_msg= | My {os_name} system | # 自定义展示模板,可用 {hostname}
# {os_name} {os_version} {os_arch} {linux_distro} {platform}
5.5 [cpu] 与 [percpu] CPU 监控
[cpu] 覆盖总 CPU 的分项阈值,官方模板默认:
[cpu]
disable=False
#iowait_careful=30 / #iowait_warning=40 / #iowait_critical=50 # 注释=默认 50/70/90
total_careful=65
total_warning=75
total_critical=85
total_log=True # 达到 critical 时写日志
user_careful=50
user_warning=70
user_critical=90
user_log=False
#user_critical_action=echo "{{time}} User CPU {{user}} higher than {{critical}}" > /tmp/cpu.alert
system_careful=50 / system_warning=70 / system_critical=90 / system_log=False
steal_careful=50 / steal_warning=70 / steal_critical=90
#ctx_switches_careful=10000 ... # 上下文切换阈值(默认 critical=50000×核数)
关键机制——告警动作(action):任何阈值键都可以追加 *_critical_action,值为一条 shell 命令,超过阈值时执行;模板变量 {{time}}、{{critical}} 及插件统计字段(如 {{user}})会被替换。这是 glances 原生「越限执行命令」能力,邮件/IM 通知即可在此对接。
实测验证(阈值联动告警):把 total_critical=85 改为 total_critical=1 并重启 Web 模式,等待约 30 秒后查告警 API:
curl -s http://127.0.0.1:61208/api/4/alert
[
{
"begin": 1792711979,
"state": "CRITICAL",
"type": "CPU_TOTAL",
"min": 16.9,
"max": 16.9,
"sum": 16.9,
"count": 1,
"avg": 16.9,
"top": [],
"desc": "",
"sort": "cpu_percent",
"global_msg": "High swap (paging) usage",
"end": -1
}
]
实测结果:CPU 总使用率 16.9% 超过修改后的 critical=1,CPU_TOTAL 的 CRITICAL 事件立即出现在告警列表,验证 [cpu] 阈值与 [alert] 事件联动生效(同回显中另有一条 MEMSWAP CRITICAL 是交换分区 99.7% 越限的真实事件)。
[percpu] 控制每核视图(TUI 按 1 键切换):
[percpu]
disable=False
max_cpu_display=4 # 同屏最多显示 4 核,超出部分折叠为一行均值
user_careful=50 / user_warning=70 / user_critical=90
iowait_careful=50 / iowait_warning=70 / iowait_critical=90
system_careful=50 / system_warning=70 / system_critical=90
5.6 [gpu]、[npu]、[mpp] 加速器监控
三段结构一致(默认模板中 [npu]、[mpp] 置 disable=True):
[gpu]
disable=False
proc_careful=50 / proc_warning=70 / proc_critical=90 # GPU 负载阈值
mem_careful=50 / mem_warning=70 / mem_critical=90 # GPU 显存阈值
temperature_careful=60 / temperature_warning=70 / temperature_critical=80 # 温度(℃)
[npu](NPU 负载/频率/温度)与 [mpp](媒体处理引擎负载)键形同 [gpu]。无 GPU/NPU 的服务器上这些插件不产出数据(本篇实测服务器无独立 GPU,/api/4/gpu 返回空列表)。
5.7 [mem]、[memswap]、[load] 内存与负载
[mem]
disable=False
#available=True # 显示可用内存而非已用内存
careful=50
warning=70
critical=90
#critical_action_repeat=echo "{{time}} {{percent}} higher than {{critical}}" >> /tmp/memory.alert
[memswap]
disable=False
careful=50
warning=70
critical=90
#warning_action=echo "{{time}} {{percent}} higher than {{warning}}" > /tmp/memory.alert
[load]
disable=False
careful=0.7 # 值会乘以 CPU 核数(0.7×核数)
warning=1.0
critical=5.0
#log=False
要点解读:[load] 的阈值系数默认 0.7/1.0/5.0(每核口径,来源模板注释引用 load average 经验值);[memswap] 监控交换分区(含 paging 进出速率 sin/sout),是发现内存压力的前哨——本篇实测服务器的交换分区长期 99.7%,MEMSWAP CRITICAL 事件持续出现在 /api/4/alert(真实回显见 5.5 节)。
5.8 [network]、[ip]、[connections]、[wifi]
[network]
disable=False
rx_careful=70 / rx_warning=80 / rx_critical=90 # 收方向速率阈值(接口速率百分比)
tx_careful=70 / tx_warning=80 / tx_critical=90 # 发方向
hide=docker.*,lo # 隐藏的接口(逗号分隔正则)
#show=docker.* # 强制显示
hide_no_up=True # 自动隐藏未启用接口
hide_no_ip=True # 自动隐藏无 IP 接口
hide_zero=False # 不隐藏零流量接口
#hide_threshold_bytes=0 # 流量低于该字节数的接口隐藏
#wlan0_rx_careful=4000000 # 可按接口名单独覆写(<接口>_rx_careful 形式)
#alias=wlp0s20f3:WIFI # 接口别名
[ip]
disable=False
refresh=60 # IP 变化少,独立 60 秒刷新
public_disabled=True # 关闭公网 IP 查询(模板默认关闭,离线服务器必须保持)
public_refresh_interval=300
public_api=https://ipv4.ipleak.net/json/ # 公网 IP 在线服务
public_field=ip
public_template={continent_name}/{country_name}/{city_name}
[connections]
disable=True # 默认关闭(CPU 开销大)
nf_conntrack_percent_careful=70 / warning=80 / critical=90
[wifi]
disable=False
careful=-65 / warning=-75 / critical=-85 # 信号强度 dBm(数值越小越差)
实测验证(hide 键):模板默认 hide=docker.*,lo,改为 hide=eth0 并重启后,/api/4/network 只返回剩余接口——实测回显中 eth0 消失、仅剩 lo:
[{"bytes_sent": 2838, "bytes_recv": 2838, "speed": 0, "key": "interface_name", "interface_name": "lo", ...}]
实测前(默认配置)同一端点返回的是 interface_name: "eth0" 的流量统计。hide 正则生效即可证实。
5.9 [diskio]、[fs]、[irq]、[folders]
[diskio]
disable=False
hide=loop.*,/dev/loop.* # 隐藏 loop 设备
hide_zero=False
#show=sda.*
#alias=sda1:SystemDisk,sdb1:DataDisk
rx_latency_careful=10 # 读延迟阈值(ms)
rx_latency_warning=20
rx_latency_critical=50
tx_latency_careful=10 / tx_latency_warning=20 / tx_latency_critical=50
#dm-0_rx_careful=4000000000 # 可按磁盘名覆写速率阈值(字节/秒)
#--diskio-iops 命令行开关切换 IOPS 视图
[fs]
disable=False
#free_space=False # True 时显示剩余空间而非已用
refresh=60 # 文件系统变化慢,独立 60 秒刷新
hide=/boot.*,.*/snap.* # 隐藏挂载点正则
#show=/,/srv
careful=50 / warning=70 / critical=90 # 全局挂载点阈值
#/_careful=1 # 按挂载点覆写:键名格式 <挂载点>_careful(根挂载点为 "/_")
#/_warning=5
#/_critical=10
#/_critical_action=echo "{{time}} {{mnt_point}} filesystem space {{percent}}% higher than {{critical}}%" > /tmp/fs.alert
#allow=shm # 额外放行的文件系统类型
#alias=/:Root
[irq]
disable=True # 默认关闭
[folders]
disable=False
refresh=60 # 目录大小统计要遍历整棵树,用长间隔
#folder_1_path=/tmp # 监控目录清单(1 ≤ x ≤ 10)
#folder_1_careful=2500 # 阈值单位 MB
#folder_1_warning=3000
#folder_1_critical=3500
#folder_1_refresh=60 # 可按目录单独设刷新
实测验证([folders]):启用 folder_1_path=/shared/glances-4.5.8 并重启后,/api/4/folders 返回实测回显(节选):
[{"indice": "1", "path": "/shared/glances-4.5.8", "refresh": 30, "careful": null,
"warning": null, "critical": null, "key": "path", "size": 14231497, "errno": 2}]
实测结果:size: 14231497 字节与该目录实际大小一致,[folders] 清单生效。配套实测([fs] 按挂载点覆写):启用 /_careful=1、/_warning=5、/_critical=10 后重启,/api/4/fs 正常返回(回显节选):
[{"device_name": "/dev/sda3", "fs_type": "ext4", "mnt_point": "/shared", ..., "percent": 58.0, "key": "mnt_point"}, ...]
5.10 [cloud]、[raid]、[smart]、[hddtemp]、[sensors]
[cloud]
disable=True # 默认关闭;云元数据 API 调用慢
refresh=120
[raid]
disable=True # 默认关闭
[smart]
disable=True # 默认关闭;需 smartmontools
#hide=.*Hide_this_driver.*
#show=.*Drive_Temperature.*
#hide_attributes=Self-tests,Errors
[hddtemp]
disable=False
host=127.0.0.1 # hddtemp 守护进程地址
port=7634 # 默认端口
[sensors]
disable=False
refresh=10 # 传感器读取慢,独立 10 秒刷新
hide=unknown.*
#show=CPU.*
temperature_core_careful=45 # CPU 核心温度阈值(℃);系统能提供阈值时优先用系统值
temperature_core_warning=65
temperature_core_critical=80
#temperature_hdd_careful=45 / #52 / #60 # 硬盘温度(经 hddtemp)
#battery_careful=70 / #80 / #90 # 电池
#fan_speed_careful=100 # 风扇转速(RPM)
#temperature_core_Ambient_careful=40 # 按传感器名覆写
#alias=core 0:CPU Core 0
要点解读:[sensors] 依赖内核暴露的 hwmon 接口,虚拟机/无传感器服务器上无数据(本篇实测服务器 /api/4/sensors 返回空列表);[smart] 启用前需安装 smartmontools 且需对物理盘的访问权限。
5.11 [processcount] 与 [processlist] 进程监控
[processcount]
disable=False
#refresh=10 # 进程计数刷新率
[processlist]
disable=False
#sort_key=memory_percent # 排序键;注释=自动排序(推荐)。可选 cpu_percent,
# memory_percent, io_counters, name, cpu_times, username
disable_stats=cpu_num # 禁采集的统计项(cpu_percent,memory_info,memory_percent,
# username,cpu_times,num_threads,nice,status,io_counters,
# cmdline,cpu_num 可选;pid,name 不可禁)
#disable_virtual_memory=True
cpu_careful=50 / cpu_warning=70 / cpu_critical=90 # 单进程 CPU 阈值
mem_careful=50 / mem_warning=70 / mem_critical=90 # 单进程内存阈值
nice_warning=-20,-19,...,19 # nice 值分级(默认非 0 即 warning)
status_ok=R,W,P,I # 进程状态分级(R 运行 S 睡眠 Z 僵尸 D 不可中断 IO)
status_critical=Z,D # 僵尸与 D 状态直接 critical
#export=.*firefox.*,pid:1234 # 仅导出匹配的进程
#focus=.*firefox.*,.*python.* # 仅聚焦显示匹配的进程
实测验证(sort_key):启用 sort_key=cpu_percent 并重启后,/api/4/processlist 返回的进程数组按 cpu_percent 降序排列(实测回显首条为 glances 自身进程,cpu_percent 字段正常输出)。
💡 技术拓展:
sort_key的排序口径有两种主流约定——「按单进程占用」(默认,psutil 的 per-process percent)与「按全机折算」(TUI 按0键切换 Irix 模式,单进程值除以核数)。容器/多核服务器上两者数值可相差核数倍,跨主机对比监控数据时先统一口径。glances 默认用单进程占用口径(与 top 默认 Irix 模式一致)。
5.12 [ports]、[vms]、[containers]、[amps]
[ports]
disable=False
refresh=30 # 扫描间隔(秒)
timeout=3 # 单次扫描超时(秒)
port_default_gateway=True # 扫描清单顶部自动加默认网关
#port_1_host=192.168.0.1 # TCP 探测清单(1 ≤ x ≤ 255)
#port_1_port=80 # 不配 port 则用 ICMP ping
#port_1_description=Home Box
#port_1_timeout=1
#port_1_rtt_warning=1000 # RTT 警告阈值(ms)
#web_1_url=https://github.com # HTTP URL 监控清单
#web_1_description=My Blog
#web_1_rtt_warning=3000
#web_1_ssl_verify=False # 跳过证书校验或指定 CA bundle
[vms]
disable=True # libvirt 虚拟机监控,默认关闭
max_name_size=20
all=False # 只显示运行中的 VM
[containers]
disable=False
; show=telegraf # 仅显示/隐藏容器(分号注释的键为可选示例)
; hide=telegraf
max_name_size=20
disable_stats=command # 可禁统计:name,status,uptime,cpu,mem,diskio,networkio,ports,command
; cpu_careful=50 / ; cpu_warning=70 / ; cpu_critical=90
; mem_careful=20 / ; mem_warning=50 / ; mem_critical=70
; containername_cpu_careful=10 # 按容器名单独覆写
all=False # 只显示运行中的容器
; podman_sock=unix:///run/user/1000/podman/podman.sock
[amps]
disable=False # AMP 具体配置见文件尾部 [amp_*] 段
实测验证([ports] TCP 探测):启用 port_1_host=127.0.0.1、port_1_port=61208、port_1_description=Glances WebUI 并重启后,/api/4/ports 实测回显(节选):
[{"host": "10.89.65.1", "port": 0, "description": "DefaultGateway", "refresh": 30, "timeout": 3, "status": null, ...},
{"host": "127.0.0.1", "port": "61208", "description": "Glances WebUI", "status": 7.2e-05, "refresh": 30, "timeout": 3, "rtt_warning": null, ...}]
实测结果解读:port_default_gateway=True 自动追加了网关探测项(DefaultGateway);port_1 的 status 字段成功时为连接 RTT(秒,实测 7.2e-05 即 0.072ms),失败时为 false(实测首轮扫描发生在 Web 服务完全就绪前,曾采到 status: false,30 秒后复测变为 RTT 值)。
💡 说明:
[containers]依赖 Docker/Podman socket(默认/var/run/docker.sock),socket 不可访问时插件无数据;[vms]依赖 libvirt。[amps]的应用监控段([amp_nginx]、[amp_systemd]等)均默认enable=false,按需启用。
5.13 [alert] 告警事件
告警事件本身的行为参数(事件由各插件阈值触发,见 5.5 节实测):
[alert]
disable=False
;max_events=10 # 告警列表最多显示 10 条
;min_duration=6 # 事件最短持续 6 秒才计入(防抖)
;min_interval=6 # 同类事件合并的最小间隔(秒),防刷屏
要点解读:三个防抖键是告警噪音控制的核心——min_duration 过滤瞬时尖峰,min_interval 合并持续越限。告警数据出口有三处:TUI 按 w 键查看告警历史、API /api/4/alert、以及各插件 *_action 自定义命令。
5.14 [serverlist] 与 [passwords](浏览器模式)
客户端/服务器模式与中央浏览器的静态服务器清单:
[serverlist]
#columns=system:hr_name,load:min5,cpu:total,mem:percent # 浏览器列表显示列
#server_1_name=localhost # 服务器名(1 ≤ x ≤ 256)
#server_1_alias=Local WebUI
#server_1_port=61266
#server_1_protocol=rest # rest 或 rpc(默认 rpc)
#server_2_name=localhost
#server_2_port=61209
[passwords]
#localhost=abc # host=password 明文密码清单(配合 serverlist)
#default=defaultpassword # 兜底密码
#local_password_path=~/.config/glances # .pwd 密码文件目录
5.15 [export] 公共段与导出器段
[export] 是所有导出器的公共配置;其余 24 个导出器各有独立段,段名即 --export <名称> 的目标名。核心段如下(完整清单:cassandra、clickhouse、couchdb、csv、duckdb、elasticsearch、graph、graphite、influxdb、influxdb2、influxdb3、json、kafka、mongodb、mqtt、nats、opentsdb、prometheus、rabbitmq、restful、riemann、statsd、timescaledb、zeromq):
[export]
#exclude_fields=.*_critical,.*_careful,.*_warning,.*\.key$ # 排除导出的字段(正则)
[graph] # SVG 图表导出(--export graph)
path=/tmp/glances # 输出目录,可被 --graph-path 覆盖
generate_every=0 # 自动生成间隔(秒),0=关闭
width=800
height=600
style=DarkStyle # Pygal 样式
[influxdb2] # InfluxDB 2.x(--export influxdb2)
host=localhost
port=8086
protocol=http
org=nicolargo
bucket=glances
token=PUT_YOUR_INFLUXDB2_TOKEN_HERE
#interval=0 # 导出间隔(秒),0=跟随全局刷新
#prefix=foo # measurement 前缀
#tags=foo:bar,spam:eggs,domain:`domainname` # 附加标签(反引号=动态执行命令)
[prometheus] # Prometheus 拉模式导出器
host=localhost
port=9091 # 监听地址,Prometheus 抓取 http://<host>:9091/metrics
#prefix=glances
labels=src:glances # 附加 label
[elasticsearch]
scheme=http
host=localhost
port=9200
index=glances # 数据按 <index>/<插件名> 索引
[kafka]
host=localhost
port=9092
topic=glances
#compression=gzip
#tags=hostname:`hostname -f`
要点解读:CSV 与 JSON 导出没有配置段,走命令行参数 --export csv --export-csv-file <路径> 与 --export json --export-json-file <路径>(导出链路实测见 《glances 4.5.8 常用 CLI 命令与工具详解》第八章);[influxdb] 段已标注 DEPRECATED(官方建议迁移到 [influxdb3]);[prometheus] 是拉模式(glances 起一个 metrics 端点),[influxdb2]/[kafka] 等是推模式(glances 主动外发),选型时按目标系统方向定。
六、生产配置模板
以下模板适用于「2-8 台 Linux 服务器、需要 Web 模式值守 + 长期数据入库」的中小规模场景(8GB+ 内存、磁盘日常占用 < 60%),键值全部来自上文实测与官方模板默认值,可直接作为 /etc/glances/glances.conf 使用:
[global]
refresh=2 # 通用 2 秒刷新;CPU 紧张的机器调到 5
check_update=false # 内网/生产关闭联网版本检查
history_size=1200 # 默认 1 小时内存历史
[outputs]
#url_prefix=/glances/ # 反代场景启用;尾斜杠必须
webui_allowed_hosts=localhost,127.0.0.1,monitor.example.com # Host 白名单防 DNS rebinding
cors_origins=https://monitor.example.com # 收紧跨域
#ssl_keyfile=/etc/glances/key.pem # 公网暴露时启用 TLS
#ssl_certfile=/etc/glances/cert.pem
[cpu]
total_careful=65
total_warning=75
total_critical=85
total_log=True
user_critical_action=echo "{{time}} User CPU {{user}} higher than {{critical}}" >> /var/log/glances-cpu.alert
[mem]
careful=50
warning=70
critical=90
[memswap]
careful=50
warning=70
critical=90
[load]
careful=0.7
warning=1.0
critical=5.0
[network]
rx_careful=70
rx_warning=80
rx_critical=90
tx_careful=70
tx_warning=80
tx_critical=90
hide=docker.*,lo
hide_no_up=True
hide_no_ip=True
[fs]
refresh=60
careful=50
warning=70
critical=90
[diskio]
hide=loop.*,/dev/loop.*
rx_latency_careful=10
rx_latency_warning=20
rx_latency_critical=50
tx_latency_careful=10
tx_latency_warning=20
tx_latency_critical=50
[sensors]
refresh=10
temperature_core_careful=45
temperature_core_warning=65
temperature_core_critical=80
[processlist]
cpu_careful=50
cpu_warning=70
cpu_critical=90
mem_careful=50
mem_warning=70
mem_critical=90
status_critical=Z,D
[ports]
refresh=30
timeout=3
port_default_gateway=True
[alert]
max_events=10
min_duration=6
min_interval=6
[export]
#exclude_fields=.*_critical,.*_careful,.*_warning
[influxdb2]
host=127.0.0.1
port=8086
protocol=http
org=monitor
bucket=glances
token=${INFLUXDB2_TOKEN}
interval=5
⚠️ 注意:
token=${INFLUXDB2_TOKEN}为敏感值占位——glances 不读 shell 环境变量,需把真实 token 明文写入该文件并chmod 600 /etc/glances/glances.conf,或改用[export]场景下的密钥管理方案;其余键均落实际值。模板中user_critical_action、webui_allowed_hosts的monitor.example.com请按实际服务器域名替换。
七、常见问题(FAQ)
Q1:改了配置怎么确认被读到了?
最直接的是 API 对照法:改 [network] 的 hide 后重启,/api/4/network 的返回应立即少掉被隐藏接口(本篇 5.8 节实测);改阈值后看 /api/4/alert 是否按新阈值出事件(5.5 节实测)。另外可用 glances -d 启动看加载日志。
Q2:配置不生效的常见原因?
① 忘记重启进程(glances 无 reload);② 改错了文件(系统里有 /etc/glances/glances.conf 与 ~/.config/glances/glances.conf 两份时,用户级会优先生效——查找顺序见 3.1 节,注意 root 与普通用户的 ~ 不同);③ 键名拼写错误——glances 对未知键宽松忽略不报错,用 --issue 与官方模板逐键核对。
Q3:阈值触发后如何自动执行动作?
在对应阈值键后加同名 _action 键,如 [cpu] 段的 user_critical_action,值为 shell 命令(模板变量 {{time}}/{{percent}}/{{critical}} 可用,见 5.5 与 5.7 节)。动作由 glances 进程以运行用户身份执行,重操作建议落盘后由外部脚本处理。若担心配置中的命令执行风险,可加启动参数 --disable-config-exec(官方推荐给系统服务)。
Q4:为什么 memswap 一直 CRITICAL?
交换分区使用率超过 [memswap] critical(默认 90%)即触发。先看 /api/4/memswap 的 percent 与 sin/sout:长期高位说明物理内存不足,应扩内存或排查内存泄漏进程(/api/4/processlist 按 memory_percent 排序);单纯偏高但 sin/sout 趋零的机器可上调阈值。
Q5:容器/虚拟机插件没数据?
[containers] 需要当前用户能读 Docker/Podman socket(默认 /var/run/docker.sock,非 root 用户需加入 docker 组或配置 podman_sock);[vms] 需 libvirt 且默认 disable=True 要先打开。本篇实测服务器未挂 socket,两个插件均无数据属正常现象。
Q6:如何关闭某个不需要的插件?
三种方式等价:配置段内 disable=True(如 [connections] 默认就是关闭的);命令行 glances --disable-plugin connections,ports(临时);glances --disable-plugin all --enable-plugin cpu,mem,load(只保留白名单)。查看当前生效插件清单:curl -s http://127.0.0.1:61208/api/4/pluginslist。
八、参考链接
- v4.5.8 官方配置模板(本篇 57 段键值的权威来源):https://github.com/nicolargo/glances/blob/v4.5.8/conf/glances.conf (查询日期 2026-10-23)
- 官方文档 Configuration 章节:https://glances.readthedocs.io/en/latest/config.html
- 官方文档 Plugins 列表:https://glances.readthedocs.io/en/latest/plugins.html
- 本站同系列实测文档:《glances 介绍与选型指南》、《Ubuntu 24.04 安装 glances 4.5.8 指南》、《glances 4.5.8 常用 CLI 命令与工具详解》