Merge branch 'sensu' into 'master'

duplicity nagios check

See merge request !3
diff --git a/backupninja/files/duplicity_salt.sh b/backupninja/files/duplicity_salt.sh
index f32f270..0317847 100644
--- a/backupninja/files/duplicity_salt.sh
+++ b/backupninja/files/duplicity_salt.sh
@@ -1,6 +1,10 @@
 {%- from "backupninja/map.jinja" import client, service_grains with context -%}
 #!/bin/bash
 
+if [ $EUID -ne 0 ]; then
+    exec /usr/bin/sudo $0 $*
+fi
+
 DUPLICITY_ARGS="--no-encryption --progress --ssl-cacert-file={{ client.cacert_file }}"
 BACKUP_URL="{{ client.target.url }}"
 
@@ -14,6 +18,7 @@
     esac
 done
 shift $((OPTIND-1))
+TOL=$2
 
 action_prepare() {
     trap action_cleanup INT TERM EXIT
@@ -43,6 +48,43 @@
     duplicity ${DUPLICITY_ARGS} collection-status ${BACKUP_URL}
 }
 
+nagios() {
+    EXITVAL=2
+    read -ra dup_status <<< $(status)
+
+    if [[ -n ${dup_status[@]} ]]; then
+        read -ra err_msg <<< $(echo ${dup_status[@]} | sed -e 's/\<No orphaned or incomplete backup sets found\>//g')
+
+        if [ ${#dup_status[@]} -ne ${#err_msg[@]} ]; then
+            EXITVAL=0
+        else
+            exit_critical "incomplete backup found"
+        fi
+    else
+        exit_critical "duplicity not working correctly"
+    fi
+
+    LAST=$(date -d "$(echo ${dup_status[@]} | sed -n -e 's/^.*Chain end time: //p' | awk '{print $1,$2,$3,$4,$5}')" +"%Y%m%d%H")
+    TODATE=$(date +"%Y%m%d%H")
+
+    if [ $[TODATE-$TOL] -gt $LAST ]; then
+        exit_critical "Last backup $[TODATE-$LAST] hours ago."
+    else
+        exit_ok $EXITVAL
+    fi
+}
+
+exit_critical() {
+    echo "CRITICAL: $*"
+    exit 2
+}
+
+exit_ok() {
+    echo "OK: $1"
+    exit $1
+}
+
+
 list_files() {
     duplicity ${DUPLICITY_ARGS} list-current-files ${BACKUP_URL}
 }
@@ -53,12 +95,15 @@
     echo "               Usage: $0 restore -f [file_to_restore] -d [destination]"
     echo "  list-files - list last backup files"
     echo "  status     - show collection status"
+    echo "  nagios      - nagios check"
+    echo "               Usage: duplicity_salt.sh nagios x_hours_back"
     exit 1
 }
 
 case $1 in
     restore) action_prepare && restore ;;
     status) action_prepare && status ;;
+    nagios) action_prepare && nagios ;;
     list-files) action_prepare && list_files ;;
     *) usage ;;
 esac
diff --git a/backupninja/meta/sensu.yml b/backupninja/meta/sensu.yml
new file mode 100644
index 0000000..646a7fa
--- /dev/null
+++ b/backupninja/meta/sensu.yml
@@ -0,0 +1,8 @@
+check:
+  local_backup_{{ grains['fqdn'] }}:
+    command: "PATH=$PATH:/usr/local/sbin duplicity_salt.sh nagios 24"
+    interval: 3600
+    timeout: 300
+    occurrences: 1
+    subscribers:
+    - {{ grains['fqdn']|replace('.', '-') }}
\ No newline at end of file