Browse Source

added prometheus pushgateway exporter

toastie89 1 year ago
parent
commit
7ef105afce

+ 15 - 0
restic/data/restic/notify-prom-stats.sh

@@ -0,0 +1,15 @@
+#!/bin/sh -xe
+
+json=`restic stats --json`
+
+[ ! -z $PROM_GW_URL ] && cat <<EOF | curl --data-binary @- $PROM_GW_URL 
+# TYPE restic_repository_total_size_bytes gauge
+# HELP restic_repository_total_size_bytes total size of the repository
+`echo -n 'restic_repository_total_size_bytes ' && \
+echo $json | grep -o '"total_size":[0-9]*' | grep -o '[0-9]*'`
+
+# TYPE restic_repository_total_files_count gauge
+# HELP restic_repository_total_files_count total count of files in the repository
+`echo -n 'restic_repository_total_files_count ' && \
+echo $json | grep -o '"total_file_count":[0-9]*' | grep -o '[0-9]*'`
+EOF

+ 16 - 0
restic/data/restic/notify-prom-status.sh

@@ -0,0 +1,16 @@
+#!/bin/sh -xe
+
+case $1 in
+  start)      state='1';  status='started'    ;;
+  stop)       state='0';  status='stopped'    ;;
+  incomplete) state='-3'; status='incomplete' ;;
+  fail|*)     state='-1'; status='failed'     ;;
+esac
+
+[ ! -z $PROM_GW_URL ] && cat <<EOF | curl --data-binary @- $PROM_GW_URL 
+# TYPE restic_hook_state gauge
+# HELP restic_hook_state 1=started 0=stopped -1=failed -3=incomplete
+restic_hook_state $state
+# TYPE restic_hook_state_timestamp gauge
+restic_hook_state_timestamp{label="$status"} `date +%s`
+EOF

+ 10 - 0
restic/data/restic/run.sh

@@ -3,9 +3,19 @@
 # Init restic repository if neccessary
 restic snapshots &>/dev/null || restic init
 
+# Notify prometheus on backup start if $PROM_GW_URL is set
+/root/restic/notify-prom-status.sh start
+
 # Run backup job, using parameters from AWS_ACCESS_KEY_ID,
 # AWS_SECRET_ACCESS_KEY, RESTIC_PASSWORD, RESTIC_REPOSITORY
 restic backup $RESTIC_BACKUP_ARGS $RESTIC_BACKUP_FOLDERS
 
+case $? in
+  0)   /root/restic/notify-prom-status.sh stop
+       /root/restic/notify-prom-stats.sh             ;;
+  3)   /root/restic/notify-prom-status.sh incomplete ;;
+  1|*) /root/restic/notify-prom-status.sh fail       ;;
+esac
+  
 # Cleanup old backups
 restic forget $RESTIC_FORGET_ARGS