Skip to content

Instantly share code, notes, and snippets.

@varac
Created February 6, 2021 13:39
Show Gist options
  • Save varac/0e16de606828968b30f0cd247ed79cbc to your computer and use it in GitHub Desktop.
Save varac/0e16de606828968b30f0cd247ed79cbc to your computer and use it in GitHub Desktop.
Export restic prometheus metrics
#!/bin/bash
# based on https://medium.com/@zoispag/collect-restic-metrics-when-using-resticker-cf862f87fc41
# This script will execute restic commands and will pipe the output to jq,
# in order to format it in a prometheus compatible metric.
# Add this script in the host crontab, to be executed every few hours.
HOST=${HOST:-`hostname -f`}
# Ensure jq package is installed, as it is needed to format
# the restic json response to valid prometheus metric.
which jq &> /dev/null
if [ $? -ne 0 ]; then
apk add jq
fi
# TEXTFILE_COLLECTOR_DIR=/var/lib/prometheus/node-exporter
TEXTFILE_COLLECTOR_DIR=/tmp
# Create directory.
[ -d ${TEXTFILE_COLLECTOR_DIR} ] || mkdir -p ${TEXTFILE_COLLECTOR_DIR}
# Create a temp unique file, that will not be parsed by node exporter.
TEMP_FILE="${TEXTFILE_COLLECTOR_DIR}/restic.prom.$$"
PERM_FILE="${TEXTFILE_COLLECTOR_DIR}/restic.prom"
touch ${TEMP_FILE}
# Note the start time of the script.
START="$(date +%s)"
# Get last backup timestamp
restic snapshots --host ${HOST} latest --json | jq -r 'max_by(.time) | .time | sub("[.][0-9]+"; "") | sub("Z"; "+00:00") | def parseDate(date): date | capture("(?<no_tz>.*)(?<tz_sgn>[-+])(?<tz_hr>\\d{2}):(?<tz_min>\\d{2})$") | (.no_tz + "Z" | fromdateiso8601) - (.tz_sgn + "60" | tonumber) * ((.tz_hr | tonumber) * 60 + (.tz_min | tonumber)); parseDate(.) | "restic_last_snapshot_ts \(.)"' >> ${TEMP_FILE}
# Get last backup size in bytes and files count
restic stats --host ${HOST} latest --json | jq -r '"restic_stats_total_size_bytes \(.total_size)\nrestic_stats_total_file_count \(.total_file_count)"' >> ${TEMP_FILE}
# Write out metrics to a temporary file.
END="$(date +%s)"
echo "restic_collector_duration_seconds $(($END - $START))" >> ${TEMP_FILE}
echo "restic_collector_last_run_ts ${END}" >> ${TEMP_FILE}
# Rename the temporary file atomically.
# This avoids the node exporter seeing half a file.
# In case a temp file was not created, delete the permanent file,
# to avoid outdated metrics.
cat "${TEMP_FILE}" > "${PERM_FILE}" || rm "${PERM_FILE}"
echo "$(date): restic metrics file update with following values:"
echo
cat ${PERM_FILE}
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment