Grafana可视化仪表板
Grafana可视化仪表板
Grafana简介
Grafana是一个开源的数据可视化和监控平台,支持多种数据源。
安装Grafana
# Docker
docker run -d --name grafana \
-p 3000:3000 \
-v grafana_data:/var/lib/grafana \
-e GF_SECURITY_ADMIN_PASSWORD=admin \
grafana/grafana
# 默认登录:admin/admin
数据源配置
Prometheus数据源
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://prometheus:9090
isDefault: true
其他数据源
# InfluxDB
- name: InfluxDB
type: influxdb
url: http://influxdb:8086
database: mydb
# Elasticsearch
- name: Elasticsearch
type: elasticsearch
url: http://elasticsearch:9200
index: logs-*
面板类型
Graph面板
{
"type": "graph",
"title": "Request Rate",
"targets": [
{
"expr": "sum(rate(http_requests_total[5m])) by (method)",
"legendFormat": "{{method}}"
}
],
"options": {
"legend": {
"show": true
}
}
}
Singlestat面板
{
"type": "singlestat",
"title": "Current Requests/s",
"targets": [
{
"expr": "sum(rate(http_requests_total[5m]))"
}
],
"options": {
"valueName": "current",
"format": "short"
}
}
Table面板
{
"type": "table",
"title": "Top Endpoints",
"targets": [
{
"expr": "topk(10, sum(rate(http_requests_total[5m])) by (endpoint))",
"format": "table",
"instant": true
}
]
}
变量和模板
{
"templating": {
"list": [
{
"name": "instance",
"type": "query",
"query": "label_values(http_requests_total, instance)",
"refresh": 2
},
{
"name": "interval",
"type": "interval",
"options": ["1m", "5m", "15m", "1h"],
"current": {
"text": "5m",
"value": "5m"
}
}
]
}
}
实践:创建监控仪表板
系统监控仪表板
{
"dashboard": {
"title": "System Monitoring",
"panels": [
{
"title": "CPU Usage",
"type": "graph",
"targets": [
{
"expr": "100 - (avg by(instance) (irate(node_cpu_seconds_total{mode=\"idle\"}[5m])) * 100)",
"legendFormat": "{{instance}}"
}
],
"yaxes": [
{"format": "percent", "max": 100}
]
},
{
"title": "Memory Usage",
"type": "graph",
"targets": [
{
"expr": "(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100",
"legendFormat": "{{instance}}"
}
],
"yaxes": [
{"format": "percent", "max": 100}
]
},
{
"title": "Disk Usage",
"type": "graph",
"targets": [
{
"expr": "(1 - node_filesystem_avail_bytes{mountpoint=\"/\"} / node_filesystem_size_bytes{mountpoint=\"/\"}) * 100",
"legendFormat": "{{instance}}"
}
],
"yaxes": [
{"format": "percent", "max": 100}
]
}
]
}
}
应用监控仪表板
{
"dashboard": {
"title": "Application Monitoring",
"panels": [
{
"title": "Request Rate",
"type": "graph",
"targets": [
{
"expr": "sum(rate(http_requests_total[5m])) by (method, status)"
}
]
},
{
"title": "Response Time",
"type": "graph",
"targets": [
{
"expr": "histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket[5m])) by (le, endpoint))"
}
]
},
{
"title": "Error Rate",
"type": "singlestat",
"targets": [
{
"expr": "sum(rate(http_requests_total{status=~'5..'}[5m])) / sum(rate(http_requests_total[5m])) * 100"
}
],
"format": "percent"
}
]
}
}
告警配置
{
"alert": {
"name": "High Error Rate",
"conditions": [
{
"type": "query",
"query": {"params": ["A", "5m", "now"]},
"reducer": {"type": "avg"},
"evaluator": {"type": "gt", "params": [5]}
}
],
"frequency": "1m",
"notifications": [
{"type": "email", "addresses": ["admin@example.com"]}
]
}
}
导入仪表板
# 导入JSON
grafana-cli plugins import /path/to/dashboard.json
# 常用仪表板ID
# Node Exporter: 1860
# Docker: 893
# Kubernetes: 315
# MySQL: 7362
总结
Grafana是功能强大的可视化工具。通过创建专业的仪表板,可以直观地监控系统和应用的各项指标。