diff --git a/files/prometheus-borg-exporter.service b/files/prometheus-borg-exporter.service new file mode 100644 index 0000000..98505de --- /dev/null +++ b/files/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 \ No newline at end of file diff --git a/files/prometheus-borg-exporter.timer b/files/prometheus-borg-exporter.timer new file mode 100644 index 0000000..743d8b3 --- /dev/null +++ b/files/prometheus-borg-exporter.timer @@ -0,0 +1,8 @@ +[Unit] +Description=Prometheus Borg Exporter Timer + +[Timer] +OnCalendar=*:0/30 + +[Install] +WantedBy=timers.target \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..78c6b15 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,14 @@ +--- +- name: Restart exporter service + ansible.builtin.systemd: + name: prometheus-borg-exporter.service + daemon_reload: true + state: restarted + enabled: true + +- name: Restart exporter timer + ansible.builtin.systemd: + name: prometheus-borg-exporter.timer + daemon_reload: true + state: restarted + enabled: true diff --git a/tasks/exporter.yml b/tasks/exporter.yml new file mode 100644 index 0000000..4080d6b --- /dev/null +++ b/tasks/exporter.yml @@ -0,0 +1,20 @@ +--- +- name: "Exporter | Copy borg_exporter" + ansible.builtin.template: + src: borg_exporter.j2 + dest: /usr/local/bin/borg_exporter + mode: "0755" + +- name: "Exporter | Copy systemd unit" + ansible.builtin.copy: + src: prometheus-borg-exporter.service + dest: /etc/systemd/system/prometheus-borg-exporter.service + mode: "0644" + notify: Restart exporter service + +- name: "Exporter | Copy systemd timer" + ansible.builtin.copy: + src: prometheus-borg-exporter.timer + dest: /etc/systemd/system/prometheus-borg-exporter.timer + mode: "0644" + notify: Restart exporter timer diff --git a/tasks/main.yml b/tasks/main.yml index 69bad4a..1f83d09 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -20,3 +20,13 @@ - name: Setup borg ansible.builtin.import_tasks: borg.yml tags: borg + +- name: Get installed packages + ansible.builtin.package_facts: + manager: apt + tags: exporter + +- name: Setup Prometheus exporter + ansible.builtin.import_tasks: exporter.yml + tags: exporter + when: "'prometheus-node-exporter' in ansible_facts.packages" diff --git a/templates/borg_exporter.j2 b/templates/borg_exporter.j2 new file mode 100644 index 0000000..04143b8 --- /dev/null +++ b/templates/borg_exporter.j2 @@ -0,0 +1,78 @@ +#!/bin/bash +{{ ansible_managed | comment }} + +set -eu + +LOGFILE=/var/log/borg +TEXTFILE_COLLECTOR_DIR=/var/lib/node_exporter +PROM_FILE=$TEXTFILE_COLLECTOR_DIR/backup.prom +TMP_FILE=$PROM_FILE.$$ +HOSTNAME=$(hostname) +COUNTER=0 + +mkdir -p $TEXTFILE_COLLECTOR_DIR + +IFS=$'\n' +#for i in $LIST; do +# COUNTER=$((COUNTER+1)) +#done + +if [ -f "$LOGFILE" ] && [ ! $(find "$LOGFILE" -mtime +2 -print) ] ; then + RESULT_B=$(cat $LOGFILE | grep "terminating" | awk '{print $3}' | head -1 ) + if [ "$RESULT_B" == 'success' ]; then RESULT_BACKUP=0; elif [ "$RESULT_B" == 'warning' ]; then RESULT_BACKUP=1; elif [ "$RESULT_B" == 'error' ]; then RESULT_BACKUP=2; else RESULT_BACKUP=3; fi + RESULT_P=$(cat $LOGFILE | grep "terminating" | awk '{print $3}' | tail -1) + if [ "$RESULT_P" == 'success' ]; then RESULT_PRUNE=0; elif [ "$RESULT_P" == 'warning' ]; then RESULT_PRUNE=1; elif [ "$RESULT_P" == 'error' ]; then RESULT_PRUNE=2; else RESULT_PRUNE=3; fi +else + RESULT_BACKUP=3 + RESULT_PRUNE=3 +fi + +echo "backup_result{host=\"${HOSTNAME}\"} $RESULT_BACKUP" +echo "prune_result{host=\"${HOSTNAME}\"} $RESULT_PRUNE" +echo "backup_result{host=\"${HOSTNAME}\"} $RESULT_BACKUP" >> $TMP_FILE +echo "prune_result{host=\"${HOSTNAME}\"} $RESULT_PRUNE" >> $TMP_FILE + + +BORG_INFO=$(borgmatic info --last 1 ) || mv $TMP_FILE $PROM_FILE +#echo "backup_count{host=\"${HOSTNAME}\"} $COUNTER" > $TMP_FILE +echo "backup_files{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | grep "Number of files" | awk '{print $4}')" >> $TMP_FILE +echo "backup_chunks_unique{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | grep "Chunk index" | awk '{print $3}')" >> $TMP_FILE +echo "backup_chunks_total{host=\"${HOSTNAME}\"} $(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 }' + ;; + TB) + echo $NUM | awk '{ print $1 * 1024 * 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{host=\"${HOSTNAME}\"} $LAST_SIZE" >> $TMP_FILE +echo "backup_last_size_compressed{host=\"${HOSTNAME}\"} $LAST_SIZE_COMPRESSED" >> $TMP_FILE +echo "backup_last_size_dedup{host=\"${HOSTNAME}\"} $LAST_SIZE_DEDUP" >> $TMP_FILE +echo "backup_total_size{host=\"${HOSTNAME}\"} $TOTAL_SIZE" >> $TMP_FILE +echo "backup_total_size_compressed{host=\"${HOSTNAME}\"} $TOTAL_SIZE_COMPRESSED" >> $TMP_FILE +echo "backup_total_size_dedup{host=\"${HOSTNAME}\"} $TOTAL_SIZE_DEDUP" >> $TMP_FILE + +mv $TMP_FILE $PROM_FILE