From 82bc88629dd1e87eba7766c4b0d330217afaf68c Mon Sep 17 00:00:00 2001 From: phil Date: Sat, 2 Dec 2023 16:03:29 +0100 Subject: [PATCH] Add functions, help text and metric type --- templates/borg_exporter.j2 | 62 +++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/templates/borg_exporter.j2 b/templates/borg_exporter.j2 index b2d0945..304da90 100644 --- a/templates/borg_exporter.j2 +++ b/templates/borg_exporter.j2 @@ -3,23 +3,57 @@ set -eu -LOGFILE=/var/log/borg TEXTFILE_COLLECTOR_DIR=/var/lib/prometheus/node-exporter PROM_FILE=$TEXTFILE_COLLECTOR_DIR/backup.prom TMP_FILE=$PROM_FILE.$$ -HOSTNAME=$(hostname) +NOW=$(date +%s) +BORG_INFO=$(borgmatic --no-color info --last 1 --json) -BORG_INFO=$(borgmatic --no-color info --last 1 --json ) || mv $TMP_FILE $PROM_FILE -echo "backup_last_run{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r .[].repository.last_modified)" >> $TMP_FILE -echo "backup_files{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[].archives | .[] | .stats.nfiles')" >> $TMP_FILE -echo "backup_chunks_unique{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[] | .cache.stats.total_unique_chunks')" >> $TMP_FILE -echo "backup_chunks_total{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[] | .cache.stats.total_chunks')" >> $TMP_FILE +get_field() { + local query="$1" + printf '%s' "$BORG_INFO" | jq -r ".[].$query" +} -echo "backup_last_size{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[].archives | .[] | .stats.original_size')" >> $TMP_FILE -echo "backup_last_size_compressed{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[].archives | .[] | .stats.compressed_size')" >> $TMP_FILE -echo "backup_last_size_dedup{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[].archives | .[] | .stats.deduplicated_size')" >> $TMP_FILE -echo "backup_total_size{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" |j q -r '.[] | .cache.stats.total_size')" >> $TMP_FILE -echo "backup_total_size_compressed{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[] | .cache.stats.total_csize')" >> $TMP_FILE -echo "backup_total_size_dedup{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[] | .cache.stats.unique_csize')" >> $TMP_FILE +borg_last_modified=$(get_field 'repository.last_modified' | xargs -I{} date -d "{}" +"%s") +echo $borg_last_modified +borg_backup_age=$(echo "$NOW" "$borg_last_modified" | awk '{print(($1 - $2) / (24 * 60 * 60))}') -mv $TMP_FILE $PROM_FILE +get_status_data() { + cat <"$TMP_FILE" +mv "$TMP_FILE" "$PROM_FILE"