Skip to content

Instantly share code, notes, and snippets.

@lachesis
Created June 11, 2019 02:42
Show Gist options
  • Save lachesis/10cc3775cec161846866c32c8870681f to your computer and use it in GitHub Desktop.
Save lachesis/10cc3775cec161846866c32c8870681f to your computer and use it in GitHub Desktop.
Get last value of statsd gauge from grafana server
#!/bin/bash
# Get last value of statsd gauge from grafana server
# Set env vars:
# GRAFANA_BASE="https://www.example.com" <-- your grafana base URL
# GRAFANA_API_KEY="eyJryJzd.....IxMj0F9" <-- your grafana API key
curl --compressed --silent \
"$GRAFANA_BASE/api/datasources/proxy/1/render" \
-H "Authorization: Bearer $GRAFANA_API_KEY" \
--data-urlencode "target=summarize($1, "'"1h", "last")' \
--data-urlencode "from=-30min" \
--data-urlencode "to=now" \
--data-urlencode "format=json" \
--data-urlencode "maxDataPoints=4" \
| jq -r '.[0].datapoints[-1]|map(tostring)|join("\t")' \
| cut -f1 # remove this line to return "value<TAB>time"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment