Resource icon

Cloud Sync monitoring script

Here's a small shell script that runs on one of my other hosts to monitor that my one cloudsync job works.

To use it, you'll need to change the `storage.realm` URL to something that can reach your NAS, and you'll need to change the thing after "Bearer" to your API token.

This script definitely assumes you have single cloudsync job, and will likely give false successes if you have more than one and one is failing.

Feel free to bug me if you need it tweaked; robinleepowell [at] gmail

Code:
#!/bin/bash

# Error trapping from https://gist.github.com/oldratlee/902ad9a398affca37bfcfab64612e7d1
__error_trapper() {
  local parent_lineno="$1"
  local code="$2"
  local commands="$3"
  echo "error exit status $code, at file $0 on or near line $parent_lineno: $commands"
}
trap '__error_trapper "${LINENO}/${BASH_LINENO}" "$?" "$BASH_COMMAND"' ERR

set -euE -o pipefail

# Cron's PATH often sucks
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# Check success
if [[ $(curl -s -X GET "http://storage.realm/api/v2.0/core/get_jobs" -H  "accept: */*" -H  "Authorization: Bearer 1-l[SNIP]Fhn" | jq -r '[.[] | select(.method=="cloudsync.sync")] | last | .state') != "SUCCESS" ]]
then
  echo "ERROR: Not successful!"
  exit 1
fi

# Twice
if [[ $(curl -s -X GET "http://storage.realm/api/v2.0/core/get_jobs" -H  "accept: */*" -H  "Authorization: Bearer 1-l[SNIP]Fhn" | jq -r '[.[] | select(.method=="cloudsync.sync")] | last | .progress.percent') != "100" ]]
then
  echo "ERROR: Not 100% complete!"
  exit 1
fi

# Check that the number of items checked has a sensible number
num=$(curl -s -X GET "http://storage.realm/api/v2.0/core/get_jobs" -H  "accept: */*" -H  "Authorization: Bearer 1-l[SNIP]Fhn" | jq -r '[.[] | select(.method=="cloudsync.sync")] | last | .progress.description' | sed 's/.* //')
if [[ $num -lt 2000 ]]
then
  echo "ERROR: Not enough items!"
  exit 1
fi

# Check the date
jobdate=$(curl -s -X GET "http://storage.realm/api/v2.0/core/get_jobs" -H  "accept: */*" -H  "Authorization: Bearer 1-l[SNIP]Fhn" | jq -r '[.[] | select(.method=="cloudsync.sync")] | last | .time_finished."$date"' | sed 's/...$//')
tdadate=$(date +%s -d '2 days ago')
if [[ $jobdate -lt $tdadate ]]
then
  echo "ERROR: Too old!"
  exit 1
fi

echo "cloudsync job completed successfully within the past 2 days"
  • Like
Reactions: Lizard1204
Author
rlpowell
Views
818
First release
Last update
Rating
0.00 star(s) 0 ratings
Top