Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Read from prometheus and write to influxdb
#!/bin/bash
set -euo pipefail
# connecting to prometheus
readonly prometheus_uri='localhost:9090'
# prometheus query
readonly query='sum(irate(metric_total[20s])) without (job)'
readonly ts_start=1612280785
readonly ts_end=1612390785
readonly step=10
# connecting to influxdb
readonly influxdb_uri='localhost:8086'
readonly influxdb_db='db'
readonly influxdb_user='writer'
readonly influxdb_pw='writer'
# influxdb format
readonly influxdb_measurement='metric_rate'
# 1. read from prometheus (add -v to debug)
# 2. convert format
# 3. write to influxdb
curl --get -fSsL "${prometheus_uri}/api/v1/query_range?start=${ts_start}&end=${ts_end}&step=${step}" --data-urlencode "query=${query}" \
| jq --raw-output ".data.result[] as \$x|\$x.values[]|\"${influxdb_measurement},\" + (\$x.metric|to_entries|sort|map(\"\(.key)=\(.value)\")|join(\",\")) + \" value=\(.[1]) \(.[0])\"" \
| parallel --plain --pipe \
curl -fSsL "${influxdb_uri}/write?db=${influxdb_db}\&u=${influxdb_user}\&p=${influxdb_pw}\&precision=s" --data-binary '@-'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment