Last active
May 25, 2021 18:05
-
-
Save zoispag/074688c9a717f2c87581ff9755f62705 to your computer and use it in GitHub Desktop.
Resticker metrics (Medium Gist 1)
This file contains 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 | |
################################################################################# | |
# MIT License | |
# | |
# Copyright (c) 2020 Zois Pagoulatos | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
################################################################################# | |
# 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`} | |
# Ensure jq package is installed, as it is needed to format | |
# the restic json response to valid prometheus metric. | |
dpkg -l jq &> /dev/null | |
if [ $? -ne 0 ]; then | |
sudo apt install jq -y | |
fi | |
TEXTFILE_COLLECTOR_DIR=/metrics | |
# 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 | |
docker exec -i restic-backups 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 | |
docker exec -i restic-backups 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. | |
mv "${TEMP_FILE}" "${PERM_FILE}" || rm "${PERM_FILE}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment