Last active
September 1, 2021 20:33
-
-
Save hickinbottoms/6612de667baa1169bf05ef50029f2205 to your computer and use it in GitHub Desktop.
Prometheus exporter for restic snapshot age
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# get current time to be used to compute age | |
NOW_SECONDS=$(date '+%s') | |
JSONDIR="/var/lib/restic-snapshot-dump" | |
PROMDIR="/var/lib/node_exporter" | |
# following will be replaced with actual restic snapshots command | |
for JSONFILE in "${JSONDIR}"/*.json; do | |
# loop over each snapshot | |
PROMFILE="${JSONFILE##*/}" # strip dirname | |
PROMFILE="${PROMDIR}/${PROMFILE%.json}.prom" # replace extension | |
echo "# HELP restic_snapshot_age_seconds Time since the last successful restic snapshot" > "${PROMFILE}.$$" | |
echo "# TYPE restic_snapshot_age_seconds gauge" >> "${PROMFILE}.$$" | |
jq -c '.[]' "${JSONFILE}" | while read SNAPSHOT_LINE; do | |
S_HOSTNAME=$(jq -r '.hostname' <<< "${SNAPSHOT_LINE}") | |
S_TAG=$(jq -r '.tags[0]' <<< "${SNAPSHOT_LINE}") # assume one tag | |
S_TIME=$(date --date="$(jq -r '.time' <<< "${SNAPSHOT_LINE}")" '+%s') | |
let S_AGE=NOW_SECONDS-S_TIME | |
# multiple paths (potentially) | |
jq -c '.paths' <<< "${SNAPSHOT_LINE}" | while read PATH_LINE; do | |
S_PATH=$(jq -r '.[]' <<< "${PATH_LINE}") | |
echo "restic_snapshot_age_seconds{tag=\"${S_TAG}\",hostname=\"${S_HOSTNAME}\",path=\"${S_PATH}\"} ${S_AGE}" | |
done | |
done >> "${PROMFILE}.$$" | |
mv "${PROMFILE}.$$" "${PROMFILE}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment