
监控服务器
linux环境下载:https://github.com/prometheus/node_exporter/releases
window环境下载:https://github.com/prometheus-community/windows_exporter/releases
本质上就是需要在服务器上下载一个node_exporter,有现成的指标暴露。
配置Prometheus
prometheus.yml
yml
scrape_configs:
# 3. 主机服务器
- job_name: 'host-server'
static_configs:
- targets: ['localhost:9182']
metrics_path: '/metrics'
scrape_interval: 5s模版
模版ID:8919 或者 1860
指标
查看cpu的使用率
我来解释下下面的公式,就是将近5分钟指标为windows_cpu_time_total{mode="idle"}的值求个平均,就能得到该服务器cpu休眠的比例。 但是可能你是有多个服务器,所以还要group by 一下,再求个平均即可。然后100-休眠的,就是真正使用率。
假设你的 Windows 机器有 1 个逻辑核心(简化分析),Prometheus 每 15 秒采集一次。
| 采集时间(相对) | 指标值(秒) |
|---|---|
| T - 5m (0s) | 10000.0 |
| T - 4m45s | 10014.2 |
| T - 4m30s | 10028.5 |
| ... | ... |
| T - 15s | 10138.0 |
| T (now) | 10152.3 |
总增量 = 10152.3 - 10000.0 = 152.3 秒
时间跨度 = 300 秒(5 分钟)
👉 粗略估算速率 = 152.3 / 300 ≈ 0.5077 秒/秒
但 rate() 更智能,会排除异常点、处理 counter 重置,并使用更稳健的算法。注意,知道大概意思就行,rate有它自己的算法!!!
shell
## linux环境
100 - (avg by (instance) (rate(windows_cpu_time_total{mode="idle"}[5m])) * 100)
## windows 环境
100 - (avg by (instance) (rate(windows_cpu_time_total{mode="idle"}[5m])) * 100)查看内存使用率
内存使用率 = 100% × (1 - 可用内存 / 总内存)
shell
## linux环境
100 * (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))
## windows环境
100 * (1 - (windows_memory_physical_free_bytes / windows_memory_physical_total_bytes))查看磁盘使用率
📌 核心指标:
node_filesystem_size_bytes:文件系统总大小node_filesystem_free_bytes:文件系统空闲空间node_filesystem_avail_bytes:实际可用空间(考虑 root 保留空间)node_filesystem_device/mountpoint:设备和挂载点
💡 推荐使用
avail而非free,因为 Linux 默认为 root 保留 5% 空间,普通用户无法写满。
shell
## linux环境
### 🔹 推荐公式(按挂载点计算使用率):
100 * (1 - (node_filesystem_avail_bytes{mountpoint!=""} / node_filesystem_size_bytes{mountpoint!=""}))
### 🔸 如果你只想监控根分区 `/`:
100 * (1 - (node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}))
## windows环境
100 * (1 - (windows_logical_disk_free_bytes / windows_logical_disk_size_bytes))
