文档
Grafana 连接 Prometheus + 主机监控 Dashboard
目标
配置 Prometheus 数据源,导入 Node Exporter Full 仪表盘,实现一键监控。
步骤一:添加数据源
- 登录 Grafana
http://localhost:3000 - 左侧菜单 → Connections → Data Sources → Add data source
- 选择 Prometheus
- 填写 URL:
- Docker 环境:
http://host.docker.internal:9090(macOS/Windows) - Docker 环境:
http://172.17.0.1:9090(Linux) - 同主机:
http://localhost:9090
- Docker 环境:
- 点击 Save & Test,应显示绿色 "Data source is working"
步骤二:创建自定义 Dashboard(JSON 配置)
{
"dashboard": {
"title": "主机监控总览",
"tags": ["prometheus", "node"],
"timezone": "browser",
"panels": [
{
"title": "CPU 使用率",
"type": "timeseries",
"targets": [
{
"expr": "100 - (avg by(instance)(rate(node_cpu_seconds_total{mode=\"idle\"}[5m])) * 100)",
"legendFormat": "{{instance}}",
"refId": "A"
}
],
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 0}
},
{
"title": "内存使用率",
"type": "gauge",
"targets": [
{
"expr": "100 * (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)",
"refId": "A"
}
],
"fieldConfig": {
"defaults": {
"unit": "percent",
"thresholds": {
"steps": [
{"color": "green", "value": null},
{"color": "yellow", "value": 70},
{"color": "red", "value": 90}
]
}
}
},
"gridPos": {"h": 8, "w": 6, "x": 12, "y": 0}
},
{
"title": "磁盘使用率",
"type": "timeseries",
"targets": [
{
"expr": "100 * (1 - node_filesystem_avail_bytes{mountpoint=\"/\"} / node_filesystem_size_bytes{mountpoint=\"/\"})",
"legendFormat": "{{instance}}",
"refId": "A"
}
],
"gridPos": {"h": 8, "w": 6, "x": 18, "y": 0}
}
]
}
}
步骤三:导入社区 Dashboard
Grafana 社区有大量现成 Dashboard:
- 左侧菜单 → Dashboards → New → Import
- 输入 Dashboard ID(如 Node Exporter Full:
1860) - 选择 Prometheus 数据源
- 导入
常用 Dashboard ID:
1860- Node Exporter Full11074- Node Exporter for Prometheus6417- Kubernetes 集群监控
步骤四:配置告警通知
# grafana.ini(或环境变量)
[smtp]
enabled = true
host = smtp.example.com:587
user = alerts@example.com
password = your-password
from_address = grafana@example.com
# 环境变量方式:
# GF_SMTP_ENABLED=true
# GF_SMTP_HOST=smtp.example.com:587
然后在 Dashboard 面板的 Alert 标签页中创建告警规则。
预期效果
Dashboard 显示主机 CPU/内存/磁盘实时曲线,阈值变色,告警通道配置完成后可收到通知。