From c87dd6cb3e492287f7e0fe8cd6e49954b78aef05 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sat, 10 Oct 2020 10:55:23 -0500 Subject: [PATCH] Add Prometheus metrics to restic-job script --- roles/restic/files/restic-job.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/roles/restic/files/restic-job.sh b/roles/restic/files/restic-job.sh index fd82011..eb12d72 100755 --- a/roles/restic/files/restic-job.sh +++ b/roles/restic/files/restic-job.sh @@ -65,6 +65,8 @@ if [ -z "${PATHS+x}" ]; then error_exit "\$PATHS is not set" fi +START="$(date +%s)" + if [ -f "$LOCK" ]; then pid=$(cat "$LOCK") if ! kill -0 "$pid" 2> /dev/null; then @@ -86,6 +88,8 @@ fi echo $$ > "$LOCK" +LOCKED=$(($(date +%s) - START)) + printf "job '%s' started\n" "$JOB" if [ -d "${HOOKS_PATH}" ]; then @@ -133,3 +137,25 @@ if [ -d "${HOOKS_PATH}" ]; then printf "running '%s' job post-hooks\n" "$JOB" run-parts --exit-on-error -v -a post "${HOOKS_PATH}" 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