Skip to content

Instantly share code, notes, and snippets.

@sazeygit
Last active April 10, 2025 05:38
Show Gist options
  • Save sazeygit/12ebe0bfed41c3b521a59e664275108c to your computer and use it in GitHub Desktop.
Save sazeygit/12ebe0bfed41c3b521a59e664275108c to your computer and use it in GitHub Desktop.
Get Prometheus-friendly borgbase stats for your backup repos
## This script echoes metrics pulled from borgbase API into a Prometheus agent friendly format
## jq is assumed installed
## Replace $API_KEY with a read only API key from your BrogBase account
## To use, set a cron job to write output of this script to a .prom file to be picked up by Prometheus agent
## Output format is as follows, repeated for each repo in account
## borg_repo_usage_mb_total{repo_name="<name>",repo_id="<id>"} <used space (mb):float>
## borg_repo_last_modified{repo_name="<name>",repo_id="<id>"} <timestamp (s):int)
## borg_repo_quota{repo_name="<name>",repo_id="<id>"} <quota size (mb):int>
# set vars
API_KEY="ey...zA"
HEADER="Authorization: Bearer $API_KEY"
REPO_QUERY='{"query": "{ repoList { name,id,quota,currentUsage,lastModified }}"}'
# get raw response
RESPONSE=$(curl --silent -X POST \
-H 'Content-Type: application/json' \
-H "$HEADER" \
-d "$REPO_QUERY" \
https://api.borgbase.com/graphql)
# get repo usage info from response
i=0; jq -r '.data.repoList[] | .id' <<< $RESPONSE | \
while read id; do
name=$(jq --arg i $i -r '.data.repoList[$i | tonumber] | .name' <<< $RESPONSE)
usage=$(jq --arg i $i -r '.data.repoList[$i | tonumber] | .currentUsage' <<< $RESPONSE)
echo "borg_repo_usage_mb_total{repo_name=\"$name\",repo_id=\"$id\"} $usage"
i=$((i + 1))
done
# get last modified info from response
i=0; jq -r '.data.repoList[] | .id' <<< $RESPONSE | \
while read id; do
name=$(jq --arg i $i -r '.data.repoList[$i | tonumber] | .name' <<< $RESPONSE)
modified=$(jq --arg i $i -r '.data.repoList[$i | tonumber] | .lastModified' <<< $RESPONSE)
modified=$(date -d $modified +"%s")
echo "borg_repo_last_modified{repo_name=\"$name\",repo_id=\"$id\"} $modified"
i=$((i + 1))
done
# get quota info from response
i=0; jq -r '.data.repoList[] | .id' <<< $RESPONSE | \
while read id; do
name=$(jq --arg i $i -r '.data.repoList[$i | tonumber] | .name' <<< $RESPONSE)
quota=$(jq --arg i $i -r '.data.repoList[$i | tonumber] | .quota' <<< $RESPONSE)
echo "borg_repo_quota{repo_name=\"$name\",repo_id=\"$id\"} $quota"
i=$((i + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment