Skip to content

Instantly share code, notes, and snippets.

@bjeanes
Created March 19, 2015 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjeanes/db723d1951153b91dffe to your computer and use it in GitHub Desktop.
Save bjeanes/db723d1951153b91dffe to your computer and use it in GitHub Desktop.
A scheduler script for NZBGet which pauses the queue if using too much of my allocated monthly quota from Internode. E.g. if 30% through the month but used more than 30% of quota, pause, otherwise unpause.
#!/usr/bin/env sh
##############################################################################
### NZBGET SCHEDULER SCRIPT ###
# Pause queue when using too much of Internode monthly quota.
#
# It will also unpause the queue once we're behind expected usage.
#
# Note: this script requires the `xmllint` binary (e.g.: from xmllib2-utils)
# and `curl` and `awk` and `perl`.
##############################################################################
### OPTIONS ###
# Internode Username
#Username=
# Internode Password
#Password=
# Service Identifier (optional if you only have one account with Internode)
#Service=
### NZBGET SCHEDULER SCRIPT ###
##############################################################################
INTERNODE_API="https://customer-webtools-api.internode.on.net/api/v1.5"
INTERNODE_USER="$NZBPO_USERNAME"
INTERNODE_PASS="$NZBPO_PASSWORD"
INTERNODE_SERVICE="$NZBPO_SERVICE"
if [ x = "x$INTERNODE_SERVICE" ]; then
INTERNODE_SERVICE="[1]"
else
INTERNODE_SERVICE="[text()=$INTERNODE_SERVICE]"
fi
PATH="/usr/bin:/opt/bin:$PATH"
XML_LINT="`which xmllint`"
CURL="`which curl`"
nzbRun() {
if [ -z "$NZBOP_APPBIN" ]; then
echo "[DEBUG] would run: nzbget -c nzbget.conf $*"
else
"$NZBOP_APPBIN" -c "$NZBOP_CONFIGFILE" $* >/dev/null 2>/dev/null
fi
}
pause() {
nzbRun -P D
}
unpause() {
nzbRun -U D
}
api() {
ENDPOINT=$1
$CURL -k --silent -u "$INTERNODE_USER:$INTERNODE_PASS" "${INTERNODE_API}${ENDPOINT}"
}
path() {
result="`$XML_LINT --xpath $1 -`"
echo $result
}
service() {
services="`api /`"
service_endpoint="`echo $services | path "string(//service$INTERNODE_SERVICE/@href)"`"
api $service_endpoint
}
quota() {
echo $1 | path 'string(//traffic/@quota)'
}
usage() {
echo $1 | path '//traffic/text()'
}
rollover() {
echo $1 | path 'string(//traffic/@rollover)'
}
# actual calc command not on my NAS
calc() { awk "BEGIN { print $*}"; }
is() {
test 1 -eq "`calc \( $* \)`"
}
# Thanks to http://stackoverflow.com/a/4948548/56690. The cleaner solution on that page
# works but the Perl DateTime module isn't on my Synology NAS so I used the uglier version.
daysBetween() {
perl -le '
use Time::Local;
sub to_epoch {
my ($t) = @_;
my ($y, $m, $d) = ($t =~ /(\d{4})-(\d{2})-(\d{2})/);
return timelocal(0, 0, 0, $d+0, $m-1, $y-1900);
}
sub diff_days {
my ($t1, $t2) = @_;
return int((abs(to_epoch($t2) - to_epoch($t1))) / 86400);
}
print diff_days(shift, shift);
' $1 $2
}
main() {
service="`service`"
usage_endpoint="`echo $service | path 'string(//resource[@type="usage"]/@href)'`"
history_endpoint="`echo $service | path 'string(//resource[@type="history"]/@href)'`"
usage="`api $usage_endpoint`"
first_day="`api $history_endpoint | path 'string(//usage[1]/@day)'`"
today="`date +"%Y-%m-%d"`"
rollover="`rollover "$usage"`"
quota="`quota "$usage"`"
used="`usage "$usage"`"
days_in_month="`daysBetween $first_day $rollover`"
days_into_month="`daysBetween $first_day $today`"
days_into_month="`calc 1 + $days_into_month`" # count today
if [ $days_into_month -ge $days_in_month ]; then
month_perc=100
else
month_perc="`calc 100 \* $days_into_month / $days_in_month`"
fi
usage_perc="`calc 100 \* $used / $quota`"
echo "[DETAIL] Today: $today ($days_into_month day(s) or $month_perc% into month)"
echo "[DETAIL] Month: $first_day to $rollover ($days_in_month day(s))"
echo "[DETAIL] Usage: $used / $quota bytes ($usage_perc%)"
# The cut-off for downloading is the % of quota corresponding to how much
# through our current month we are, unless it's over 90% of the total quota,
# in which case we should just stop downloading for the month.
if is $month_perc \>= 90; then
perc_cap=90
else
perc_cap=$month_perc
fi
if is $usage_perc \>= $perc_cap; then
echo "[INFO] Using quota to quickly; pausing"
pause
else
echo "[INFO] Quota to spare; unpausing"
unpause
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment