监控告警体系中,数据采集和存储解决”有什么问题”,可视化呈现解决”怎么发现问题”。Grafana作为开源数据可视化平台,支持Prometheus、Loki、InfluxDB、Elasticsearch等数十种数据源,是SRE稳定性工程中构建可观测性仪表盘的标准工具。本文从数据源接入、面板设计、告警规则配置三个维度,给出完整的Grafana监控方案。
Grafana数据源配置与多数据源管理
Grafana支持同时接入多个数据源。以Prometheus和Loki为例,通过Grafana provisioning机制实现配置即代码:
# /etc/grafana/provisioning/datasources/datasources.yaml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://localhost:9090
isDefault: true
jsonData:
httpMethod: POST
timeInterval: "15s"
- name: Loki
type: loki
access: proxy
url: http://localhost:3100
jsonData:
maxLines: 1000
- name: MySQL
type: mysql
access: proxy
url: localhost:3306
database: metrics
user: grafana_reader
secureJsonData:
password: ${GRAFANA_MYSQL_PASSWORD}
多数据源场景下,可在查询编辑器中通过数据源名称切换。Grafana支持变量化数据源,在Dashboard Settings中定义变量$datasource,面板查询中使用$datasource引用,实现同一面板切换数据源对比。
核心监控面板设计模式
一个完整的运维监控仪表盘通常包含以下面板层次:
基础设施层面板——CPU、内存、磁盘IO、网络流量,使用PromQL查询Node Exporter指标:
# CPU使用率(按核心)
100 - (avg by(instance, cpu) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
# 内存使用率
(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100
# 磁盘空间使用率
node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}
在Grafana Time Series面板中配置这些查询,设置阈值颜色:绿色(<70%)、橙色(70-85%)、红色(>85%)。Thresholds配置位于Panel设置页:
{
"thresholds": {
"mode": "absolute",
"steps": [
{"color": "green", "value": null},
{"color": "orange", "value": 70},
{"color": "red", "value": 85}
]
}
}
应用层面板——HTTP请求QPS、响应时间P99、错误率,查询Prometheus自定义指标:
# 请求QPS
rate(http_requests_total{job="api-server"}[1m])
# 响应时间P99
histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m]))
# 错误率
sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) * 100
日志关联面板——将Loki日志查询与Prometheus指标关联,点击指标异常点可下钻查看对应时间段的日志。配置方式:在面板Links中添加Dashboard Link,URL中传入时间范围变量:
/d/explore?left={"datasource":"Loki","queries":[{"expr":"{app=\"$app\"} |= \"ERROR\""}],"range":{"from":"$__from","to":"$__to"}}
Grafana Alerting告警规则配置
Grafana 9.0+内置统一告警引擎,支持PromQL和Loki查询条件。通过provisioning文件配置告警规则:
# /etc/grafana/provisioning/alerting/rules.yaml
apiVersion: 1
groups:
- name: infrastructure
interval: 30s
rules:
- uid: cpu_high
title: CPU使用率过高
condition: A
data:
- refId: A
relativeTimeRange:
from: 600s
datasourceUid: prometheus
model:
expr: |
100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 85
instant: true
noDataState: NoData
execErrState: Error
for: 5m
annotations:
summary: "CPU使用率超过85%"
description: "{{ $labels.instance }} CPU使用率已达 {{ $values.A }}%"
isPaused: false
for: 5m表示条件持续5分钟才触发告警,避免瞬时抖动误报。告警通知渠道配置:
# /etc/grafana/provisioning/alerting/contactpoints.yaml
apiVersion: 1
contactPoints:
- orgId: 1
name: ops-team
receivers:
- uid: dingtalk
type: dingtalk
settings:
url: https://oapi.dingtalk.com/robot/send?access_token=xxx
title: '{{ template "default.title" . }}'
message: '{{ template "default.message" . }}'
disableResolveMessage: false
Dashboard模板变量实现动态过滤
生产环境通常需要按主机、服务、环境等维度筛选数据。Grafana模板变量实现交互式过滤:
# 变量1: 环境选择
name: environment
type: query
query: label_values(node_uname_info, environment)
# 变量2: 主机选择(依赖环境变量)
name: instance
type: query
query: label_values(node_uname_info{environment="$environment"}, instance)
面板查询中引用变量:node_cpu_seconds_total{instance="$instance"}。用户在仪表盘顶部下拉选择环境和主机,所有面板自动过滤对应数据。
Grafana监控体系从数据源接入到面板设计再到告警配置,形成完整的可观测性闭环。实际运维中,仪表盘迭代应遵循”先覆盖核心指标,再逐步细化”的原则,避免过度追求面板数量而忽视数据质量。告警规则上线前务必在测试环境验证触发条件和通知链路,防止告警风暴和漏报。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/grafana-ke-shi-hua-jian-kong-mian-ban-da-jian-shi-zhan-shu/