Add functions, help text and metric type

This commit is contained in:
phil 2023-12-02 16:03:29 +01:00
parent f1972140ff
commit 82bc88629d

View file

@ -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 <<EOF
# HELP backup_age Age of last backup ind days.
# TYPE backup_age gauge
backup_age $borg_backup_age
# HELP backup_files Number of files in backup.
# TYPE backup_files gauge
backup_files $(get_field 'archives | .[] | .stats.nfiles')
# HELP backup_chunks_unique Number of unique chunks of recent backup.
# TYPE backup_chunks_unique gauge
backup_chunks_unique $(get_field 'cache.stats.total_unique_chunks')
# HELP backup_chunks_total Number of chunks in all backups.
# TYPE backup_chunks_total gauge
backup_chunks_total $(get_field 'cache.stats.total_chunks')
# HELP backup_last_size Size of last backup in bytes.
# TYPE backup_last_size gauge
backup_last_size $(get_field 'archives | .[] | .stats.original_size')
# HELP backup_last_size_compressed Compressed site of last backup in bytes.
# TYPE backup_last_size_compressed gauge
backup_last_size_compressed $(get_field 'archives | .[] | .stats.compressed_size')
# HELP backup_last_size_dedup Depluplicated size of last backup in bytes.
# TYPE backup_last_size_dedup gauge
backup_last_size_dedup $(get_field 'archives | .[] | .stats.deduplicated_size')
# HELP backup_total_size Size of all backups in bytes.
# TYPE backup_total_size gauge
backup_total_size $(get_field 'cache.stats.total_size')
# HELP backup_total_size_compressed Compressed size of all backups in bytes.
# TYPE backup_total_size_compressed gauge
backup_total_size_compressed $(get_field 'cache.stats.total_csize')
# HELP backup_total_size_dedup Depluplicated size of all backups in bytes.
# TYPE backup_total_size_dedup gauge
backup_total_size_dedup $(get_field 'cache.stats.unique_csize')
EOF
}
status=$(get_status_data)
printf '%s' "$status" >"$TMP_FILE"
mv "$TMP_FILE" "$PROM_FILE"