log_to_influx.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/bin/bash
  2. # Load environment variables from .env file
  3. source .env
  4. # Export Borg passphrase
  5. export BORG_PASSPHRASE=$PASSPHRASE
  6. export BORG_RSH="ssh -i $SSH_KEY -p $SSH_PORT"
  7. json=`sudo -E borg info "$SERVER_USER@$SERVER_IP:$REPOSITORY" --last 1 --json` || exit 1
  8. count=`echo "$json" | jq '.archives | length'`
  9. data=""
  10. i=0
  11. while [ $i -lt $count ]; do
  12. name=`echo "$json" | jq ".archives[$i].name"`
  13. comment=`echo "$json" | jq ".archives[$i].comment"`
  14. duration=`echo "$json" | jq ".archives[$i].duration"`
  15. start=`echo "$json" | jq -r ".archives[$i].start"`
  16. end=`echo "$json" | jq -r ".archives[$i].end"`
  17. hostname=`echo "$json" | jq -r ".archives[$i].hostname"`
  18. compressed_size=`echo "$json" | jq ".archives[$i].stats.compressed_size"`
  19. deduplicated_size=`echo "$json" | jq ".archives[$i].stats.deduplicated_size"`
  20. nfiles=`echo "$json" | jq ".archives[$i].stats.nfiles"`
  21. original_size=`echo "$json" | jq ".archives[$i].stats.original_size"`
  22. repo_location=`echo "$json" | jq -r '.repository.location'`
  23. timestamp=`date --date="$start" +"%s"`
  24. data="info,repo_location=$repo_location,hostname=$hostname name=$name,comment=$comment,duration=$duration,compressed_size=$compressed_size,deduplicated_size=$deduplicated_size,nfiles=$nfiles,original_size=$original_size $timestamp"
  25. curl --request POST \
  26. "$INFLUXDB_URL/api/v2/write?org=$INFLUXDB_ORG&bucket=$INFLUXDB_BUCKET&precision=s" \
  27. --header "Authorization: Token $INFLUXDB_TOKEN" \
  28. --header "Content-Type: text/plain; charset=utf-8" \
  29. --header "Accept: application/json" \
  30. --data-binary "$data"
  31. i=$(($i+1))
  32. done