borg-textcollector.sh 2.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/bash

set -o errexit
set -o nounset
set -o pipefail

if (( $# != 1 )); then
  echo "Missing textcollector directory argument"
  exit 1
fi

TEXTFILE_COLLECTOR_DIR=${1}
PROM_FILE=$TEXTFILE_COLLECTOR_DIR/borg.prom


TMP_FILE=$PROM_FILE.$$
[ -e $TMP_FILE ] && rm -f $TMP_FILE

trap "rm -f $TMP_FILE" EXIT

# Hetzner borg
if [[ -f /usr/local/bin/borg ]]; then
  LAST_ARCHIVE=$(/usr/local/bin/borg list --last 1)
  LAST_ARCHIVE_NAME=$(echo $LAST_ARCHIVE | awk '{print $1}')
  LAST_ARCHIVE_DATE=$(echo $LAST_ARCHIVE | awk '{print $3" "$4}')
  LAST_ARCHIVE_TIMESTAMP=$(date -d "$LAST_ARCHIVE_DATE" +"%s")

  echo "# HELP borg_hetzner_last_archive_timestamp timestamp of last backup in UTC" >> $TMP_FILE
  echo "# TYPE borg_hetzner_last_archive_timestamp counter" >> $TMP_FILE
30
  echo "borg_hetzner_last_archive_timestamp $LAST_ARCHIVE_TIMESTAMP" >> $TMP_FILE;
Evangelos Foutras's avatar
Evangelos Foutras committed
31
32
33
34
35
36

  REPO_SIZE=$(/usr/local/bin/borg info --json | jq '.cache.stats.unique_csize')

  echo "# HELP borg_hetzner_repo_size_bytes amount of data stored in the repo in bytes" >> $TMP_FILE
  echo "# TYPE borg_hetzner_repo_size_bytes gauge" >> $TMP_FILE
  echo "borg_hetzner_repo_size_bytes $REPO_SIZE" >> $TMP_FILE
37
38
39
40
41
42
43
44
45
46
47
fi

# rsync.net borg
if [[ -f /usr/local/bin/borg-offsite ]]; then
  LAST_ARCHIVE=$(/usr/local/bin/borg-offsite list --last 1)
  LAST_ARCHIVE_NAME=$(echo $LAST_ARCHIVE | awk '{print $1}')
  LAST_ARCHIVE_DATE=$(echo $LAST_ARCHIVE | awk '{print $3" "$4}')
  LAST_ARCHIVE_TIMESTAMP=$(date -d "$LAST_ARCHIVE_DATE" +"%s")

  echo "# HELP borg_offsite_last_archive_timestamp timestamp of last backup in UTC" >> $TMP_FILE
  echo "# TYPE borg_offsite_last_archive_timestamp counter" >> $TMP_FILE
48
  echo "borg_offsite_last_archive_timestamp $LAST_ARCHIVE_TIMESTAMP" >> $TMP_FILE;
Evangelos Foutras's avatar
Evangelos Foutras committed
49
50
51
52
53
54

  REPO_SIZE=$(/usr/local/bin/borg-offsite info --json | jq '.cache.stats.unique_csize')

  echo "# HELP borg_offsite_repo_size_bytes amount of data stored in the repo in bytes" >> $TMP_FILE
  echo "# TYPE borg_offsite_repo_size_bytes gauge" >> $TMP_FILE
  echo "borg_offsite_repo_size_bytes $REPO_SIZE" >> $TMP_FILE
55
56
57
fi

mv -f $TMP_FILE $PROM_FILE