helper/borg_exporter.sh
2024-10-05 00:06:41 +02:00

43 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env bash
ENVFIILE=/etc/prometheus/node_exporter/borg_exporter.env
[[ -f "$ENVFIILE" ]] && source "$ENVFIILE"
TEXTFILE_COLLECTOR_DIR=${TEXTFILE_COLLECTOR_DIR:-/var/lib/node_exporter/}
PROM_FILE=$TEXTFILE_COLLECTOR_DIR/backup.prom
TMP_FILE=$PROM_FILE.$$
if [[ -n "$REPO_BASE" ]] ; then
REPO_BASES+=("$REPO_BASE")
fi
for repo_base in "${REPO_BASES[@]}"; do
mapfile -t TMP < <(find "$repo_base" -type d -name data -print0 | xargs --null -n1 dirname)
REPOS+=( "${TMP[@]}" )
unset TMP
done
if [[ -n "${REPOS[*]}" ]] ; then
echo "# TYPE backup_age_seconds gauge"
echo "# TYPE backup_total_size_bytes gauge"
fi >> "$TMP_FILE"
for repository in "${REPOS[@]}" ; do
hostname="$repository"
if [[ -n "${HOSTNAMES[@]}" ]] ; then
hostname=${HOSTNAMES[$repository]:-$repository}
fi
if [[ "$hostname" == "$repository" ]] ; then
hostname=${hostname/"${REPO_BASE}/"//}
hostname=${hostname/"/backup"//}
hostname=${hostname//\//}
fi
index_base="$repository/index"
age_seconds="$(( $(date +%s) - $(date +%s -r "$index_base".*) ))"
size="$(du -s "$repository" | cut -f1)"
echo backup_age_seconds\{host=\""$hostname"\",repository=\""$repository"\"\} "$age_seconds"
echo backup_total_size_bytes\{host=\""$hostname"\",repository=\""$repository"\"\} "$size"
done >> "$TMP_FILE"
mv "$TMP_FILE" "$PROM_FILE"