From 932960c3308fa15dbf27a1a8a3bea6693d3ea208 Mon Sep 17 00:00:00 2001 From: Timo Derstappen Date: Sat, 23 Apr 2016 22:31:00 +0200 Subject: [PATCH] first commit --- README.md | 36 +++++++++++++++++++ VERSION | 1 + borg.env | 2 ++ borg_exporter | 59 ++++++++++++++++++++++++++++++++ prometheus-borg-exporter.service | 7 ++++ prometheus-borg-exporter.timer | 8 +++++ 6 files changed, 113 insertions(+) create mode 100644 README.md create mode 100644 VERSION create mode 100644 borg.env create mode 100755 borg_exporter create mode 100644 prometheus-borg-exporter.service create mode 100644 prometheus-borg-exporter.timer diff --git a/README.md b/README.md new file mode 100644 index 0000000..3ab0fd4 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# Borg exporter + +Export borg information to prometheus. + +## Dependencies + + * Prometheus (obviously) + * Node Exporter with textfile collector + * Borg (https://github.com/borgbackup/borg) + +## Install + +Copy `borg_exporter` to `/usr/local/bin`. + +Copy `borg.env` to `/etc/borg` and replace your repokey and repository in it. + +Copy the systemd unit to `/etc/systemd/system` and run + +``` +systemctl enable prometheus-borg-exporter.timer +systemctl start prometheus-borg-exporter.timer +``` + +Alternative: Use `ExecStartPost` in your borg backupt timer itself to write our the metrics. + +## Configure your node exporter + +Make sure your node exporter uses `textfile` in `--collectors.enabled` and add the following parameter: `--collector.textfile.directory=/var/lib/node_exporter/textfile_collector` + +## Example queries + +``` +backup_total_size_dedup{job='node'} +backup_last_size_dedup{job='node'} +backup_chunks_total{job='node'} +``` diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..9c1b85d --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.0.0+git diff --git a/borg.env b/borg.env new file mode 100644 index 0000000..b27d353 --- /dev/null +++ b/borg.env @@ -0,0 +1,2 @@ +BORG_PASSPHRASE= +REPOSITORY= diff --git a/borg_exporter b/borg_exporter new file mode 100755 index 0000000..6fafa93 --- /dev/null +++ b/borg_exporter @@ -0,0 +1,59 @@ +#!/bin/bash + +set -eu + +source /etc/borg + +TEXTFILE_COLLECTOR_DIR=/var/lib/node_exporter/textfile_collector +PROM_FILE=$TEXTFILE_COLLECTOR_DIR/backup.prom +TMP_FILE=$PROM_FILE.$$ +LIST=$(BORG_PASSPHRASE=$BORG_PASSPHRASE borg list $REPOSITORY |awk '{print $1}') +COUNTER=0 + +mkdir -p $TEXTFILE_COLLECTOR_DIR + +for i in $LIST; do + COUNTER=$((COUNTER+1)) +done + +BORG_INFO=$(BORG_PASSPHRASE=$BORG_PASSPHRASE borg info "$REPOSITORY::$i") +echo "backup_count $COUNTER" > $TMP_FILE +echo "backup_files $(echo "$BORG_INFO" |grep "Number of files" |awk '{print $4}')" >> $TMP_FILE +echo "backup_chunks_unique $(echo "$BORG_INFO" |grep "Chunk index" |awk '{print $3}')" >> $TMP_FILE +echo "backup_chunks_total $(echo "$BORG_INFO" |grep "Chunk index" |awk '{print $4}')" >> $TMP_FILE + +function calc_bytes { + NUM=$1 + UNIT=$2 + + case "$UNIT" in + KB) + echo $NUM | awk '{ print $1 * 1024 }' + ;; + MB) + echo $NUM | awk '{ print $1 * 1024 * 1024 }' + ;; + GB) + echo $NUM | awk '{ print $1 * 1024 * 1024 * 1024 }' + ;; + esac +} + +# byte size +LAST_SIZE=$(calc_bytes $(echo "$BORG_INFO" |grep "This archive" |awk '{print $3}') $(echo "$BORG_INFO" |grep "This archive" |awk '{print $4}')) +LAST_SIZE_COMPRESSED=$(calc_bytes $(echo "$BORG_INFO" |grep "This archive" |awk '{print $5}') $(echo "$BORG_INFO" |grep "This archive" |awk '{print $6}')) +LAST_SIZE_DEDUP=$(calc_bytes $(echo "$BORG_INFO" |grep "This archive" |awk '{print $7}') $(echo "$BORG_INFO" |grep "This archive" |awk '{print $8}')) +TOTAL_SIZE=$(calc_bytes $(echo "$BORG_INFO" |grep "All archives" |awk '{print $3}') $(echo "$BORG_INFO" |grep "All archives" |awk '{print $4}')) +TOTAL_SIZE_COMPRESSED=$(calc_bytes $(echo "$BORG_INFO" |grep "All archives" |awk '{print $5}') $(echo "$BORG_INFO" |grep "All archives" |awk '{print $6}')) +TOTAL_SIZE_DEDUP=$(calc_bytes $(echo "$BORG_INFO" |grep "All archives" |awk '{print $7}') $(echo "$BORG_INFO" |grep "All archives" |awk '{print $8}')) + + +echo "backup_last_size $LAST_SIZE" >> $TMP_FILE +echo "backup_last_size_compressed $LAST_SIZE_COMPRESSED" >> $TMP_FILE +echo "backup_last_size_dedup $LAST_SIZE_DEDUP" >> $TMP_FILE +echo "backup_total_size $TOTAL_SIZE" >> $TMP_FILE +echo "backup_total_size_compressed $TOTAL_SIZE_COMPRESSED" >> $TMP_FILE +echo "backup_total_size_dedup $TOTAL_SIZE_DEDUP" >> $TMP_FILE + + +mv $TMP_FILE $PROM_FILE diff --git a/prometheus-borg-exporter.service b/prometheus-borg-exporter.service new file mode 100644 index 0000000..8eb5648 --- /dev/null +++ b/prometheus-borg-exporter.service @@ -0,0 +1,7 @@ +[Unit] +Description=Prometheus Borg Exporter +After=network-online.target + +[Service] +ExecStart=/usr/local/bin/borg_exporter +Type=oneshot diff --git a/prometheus-borg-exporter.timer b/prometheus-borg-exporter.timer new file mode 100644 index 0000000..f7bd688 --- /dev/null +++ b/prometheus-borg-exporter.timer @@ -0,0 +1,8 @@ +[Unit] +Description=Prometheus Borg Exporter Timer + +[Timer] +OnCalendar=daily + +[Install] +WantedBy=timers.target