push-metric.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/sh -xe
  2. if [ $# -eq 0 ]; then
  3. echo 'Script to update a Prometheus Pushgateway with restic metrics'
  4. echo 'Syntax: push-metric.sh [start|stop|incomplete|fail]'
  5. echo 'Gateway-URL must be set as environment variable in $PROM_GW_URL'
  6. exit 0
  7. fi
  8. if [ -z $PROM_GW_URL ]; then
  9. echo 'No Prometheus Push Gateway set in $PROM_GW_URL'
  10. exit 0
  11. fi
  12. ## Set numeric state value and verbal status based on the received event
  13. case $1 in
  14. start) state='1'; status='started' ;;
  15. stop) state='0'; status='stopped' ;;
  16. incomplete) state='-3'; status='incomplete' ;;
  17. fail|*) state='-1'; status='failed' ;;
  18. esac
  19. ## Update the Prometheus Pushgateway about the state/status
  20. cat <<EOF | curl --data-binary @- $PROM_GW_URL
  21. # TYPE restic_hook_state gauge
  22. # HELP restic_hook_state 1=started 0=stopped -1=failed -3=incomplete
  23. restic_hook_state $state
  24. # TYPE restic_hook_state_timestamp gauge
  25. restic_hook_state_timestamp{label="$status"} `date +%s`
  26. EOF
  27. ## Update the Prometheus Pushgateway about statistics if status is stopped
  28. if [ $status = 'stopped' ]; then
  29. json=`restic stats --json`
  30. cat <<EOF | curl --data-binary @- $PROM_GW_URL
  31. # TYPE restic_repository_total_size_bytes gauge
  32. # HELP restic_repository_total_size_bytes total size of the repository
  33. `echo -n 'restic_repository_total_size_bytes ' && \
  34. echo $json | grep -o '"total_size":[0-9]*' | grep -o '[0-9]*'`
  35. # TYPE restic_repository_total_files_count gauge
  36. # HELP restic_repository_total_files_count total count of files in the repository
  37. `echo -n 'restic_repository_total_files_count ' && \
  38. echo $json | grep -o '"total_file_count":[0-9]*' | grep -o '[0-9]*'`
  39. EOF
  40. fi