#!/bin/bash set -uo pipefail textfile_dir={{ node_exporter_textfile_dir }} archive_dir={{ backup_archive_dir }} raid_mount={{ backup_raid_mount }} file=${textfile_dir}/backup_inventory.prom tmp=${file}.$$ now=$(date +%s) mkdir -p ${textfile_dir} mounted=0 if mountpoint -q ${raid_mount} then mounted=1 fi emit_kind() { local kind=$1 local newest local count=0 local age=0 local bytes=0 local total=0 if [ ${mounted} -eq 1 ] then count=$(find ${archive_dir} -maxdepth 1 -name "beeserver_${kind}_*.tar.gz.gpg" -type f 2>/dev/null | wc -l) newest=$(find ${archive_dir} -maxdepth 1 -name "beeserver_${kind}_*.tar.gz.gpg" -type f -printf '%T@ %p\n' 2>/dev/null | sort -rn | head -1 | cut -d' ' -f2-) total=$(find ${archive_dir} -maxdepth 1 -name "beeserver_${kind}_*.tar.gz.gpg" -type f -printf '%s\n' 2>/dev/null | awk '{ s += $1 } END { print s + 0 }') if [ -n "${newest:-}" ] then bytes=$(stat -c %s "${newest}") age=$((now - $(stat -c %Y "${newest}"))) fi fi echo "backup_archive_count{kind=\"${kind}\"} ${count}" echo "backup_archive_newest_age_seconds{kind=\"${kind}\"} ${age}" echo "backup_archive_newest_bytes{kind=\"${kind}\"} ${bytes}" echo "backup_archive_total_bytes{kind=\"${kind}\"} ${total}" } { echo "# HELP backup_raid_mounted Whether the backup raid is mounted." echo "# TYPE backup_raid_mounted gauge" echo "backup_raid_mounted ${mounted}" echo "# HELP backup_archive_count Number of retained archives." echo "# TYPE backup_archive_count gauge" echo "# HELP backup_archive_newest_age_seconds Age of the newest archive." echo "# TYPE backup_archive_newest_age_seconds gauge" echo "# HELP backup_archive_newest_bytes Size of the newest archive." echo "# TYPE backup_archive_newest_bytes gauge" echo "# HELP backup_archive_total_bytes Size of all retained archives." echo "# TYPE backup_archive_total_bytes gauge" emit_kind daily emit_kind weekly } > "${tmp}" chmod 0644 "${tmp}" mv "${tmp}" "${file}"