Add Prometheus metrics to restic-job script

This commit is contained in:
Ryan Cavicchioni 2020-10-10 10:55:23 -05:00
parent 6a55359d28
commit c87dd6cb3e
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D

View File

@ -65,6 +65,8 @@ if [ -z "${PATHS+x}" ]; then
error_exit "\$PATHS is not set" error_exit "\$PATHS is not set"
fi fi
START="$(date +%s)"
if [ -f "$LOCK" ]; then if [ -f "$LOCK" ]; then
pid=$(cat "$LOCK") pid=$(cat "$LOCK")
if ! kill -0 "$pid" 2> /dev/null; then if ! kill -0 "$pid" 2> /dev/null; then
@ -86,6 +88,8 @@ fi
echo $$ > "$LOCK" echo $$ > "$LOCK"
LOCKED=$(($(date +%s) - START))
printf "job '%s' started\n" "$JOB" printf "job '%s' started\n" "$JOB"
if [ -d "${HOOKS_PATH}" ]; then if [ -d "${HOOKS_PATH}" ]; then
@ -133,3 +137,25 @@ if [ -d "${HOOKS_PATH}" ]; then
printf "running '%s' job post-hooks\n" "$JOB" printf "running '%s' job post-hooks\n" "$JOB"
run-parts --exit-on-error -v -a post "${HOOKS_PATH}" run-parts --exit-on-error -v -a post "${HOOKS_PATH}"
fi fi
END=$(date +%s)
if [ -d /var/spool/node_exporter/textfile_collector ]; then
cat << EOF > "/var/spool/node_exporter/textfile_collector/restic.prom.$$"
node_restic_duration_seconds{restic_job="${JOB}"} $((END - START))
node_restic_lock_duration_seconds{restic_job="${JOB}"} $LOCKED
node_restic_last_run_time{restic_job="${JOB}"} $END
node_restic_retries{restic_job="${JOB}"} $counter
EOF
if [ -f /var/spool/node_exporter/textfile_collector/restic.prom ]; then
cat /var/spool/node_exporter/textfile_collector/restic.prom "/var/spool/node_exporter/textfile_collector/restic.prom.$$" |
tac |
awk '!seen[$1]++' |
tac |
sponge "/var/spool/node_exporter/textfile_collector/restic.prom.$$"
fi
mv "/var/spool/node_exporter/textfile_collector/restic.prom.$$" \
/var/spool/node_exporter/textfile_collector/restic.prom
fi