快速检查在Grafana设定的图表查询表达式
Grafana 打印表达式.sh
#!/bin/bash
set -eu
for dashboard in "$@"; do
curl -s \
-H "Authorization: Bearer $GRAFANA_KEY" \
-H "Content-type: application/json" \
"http://$GRAFANA_HOST/api/dashboards/db/$dashboard" |
jq -r --arg dashboard "$dashboard" '
.dashboard.rows[].panels[] |
select(has("targets")) |
["[" + $dashboard + "::" + .title + "]", (.targets[].expr | . + "\n")][]
' |
sed 's/^\[/\x1b[0;32m[/' |
sed 's/\]$/]\x1b[m/'
done
举个例子,可以得到类似这样的输出。
./grafana-print-expr.sh prometheus-stats
[prometheus-stats::Uptime]
(time() - process_start_time_seconds{job="prometheus"})
[prometheus-stats::Local Storage Memory Series]
prometheus_local_storage_memory_series
[prometheus-stats::Interal Storage Queue Length]
prometheus_local_storage_indexing_queue_length
[prometheus-stats::Samples ingested (rate-5m)]
rate(prometheus_local_storage_ingested_samples_total[5m])
[prometheus-stats::Target Scrapes (last 5m)]
rate(prometheus_target_interval_length_seconds_count[5m])
[prometheus-stats::Scrape Duration]
prometheus_target_interval_length_seconds{quantile!="0.01", quantile!="0.05"}
[prometheus-stats::Rule Eval Duration]
prometheus_evaluator_duration_seconds{quantile!="0.01", quantile!="0.05"}