Skip to content

Instantly share code, notes, and snippets.

@Generator
Last active June 2, 2023 00:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Generator/67da7dc859634046165320ef061769e0 to your computer and use it in GitHub Desktop.
Save Generator/67da7dc859634046165320ef061769e0 to your computer and use it in GitHub Desktop.
Throttle Deluge on Plex stream Start/Stop

Dependencies

  • deluge-console

Install

$ wget -O deluge_throttle.sh https://gist.githubusercontent.com/Generator/67da7dc859634046165320ef061769e0/raw/deluge_throttle.sh
$ chmod +x deluge_throttle.sh

Script Setup

Edit deluge_throttle.sh and set deluged username password .

If want's to use different username, password and level see Deluge documentation
https://dev.deluge-torrent.org/wiki/UserGuide/Authentication

Tautulli Setup

Commum Scripts Settings in Tautulli:
Taultulli > Settings > Notification Agents > Add a Notification Agent > Script
Set Script Folder
Select deluge_throttle.sh
Script Timeout 0

Throttle Download/Upload Speed

Triggers:

  • Playback Start
  • Playback Stop

Arguments: Set Download/Upload limit in KBps. Set -1 for unlimited

  • Playback Start: -D 100 -U 10
  • Playback Stop: -D -1 -U -1

Throttle Pause

Triggers:

  • Playback Start
  • Playback Stop

Arguments:

  • Playback Start: -p
  • Playback Stop: -r

Usage

-U		- Set max upload speed [KBs], use "-1" to set unlimited
-D		- Set max download speed [KBs], use "-1" to set unlimited
-p		- Pause active torrent
-r		- Restore paused torrents (previously active only)
-i		- Shows torrent information status
-h | --help	- Help
#!/bin/bash
# Options
D_USERNAME= # deluged username
D_PASSWORD= # deluged password
D_HOST=localhost # deluged host
D_PORT=58846 # deluged port
## CODE ##
# List active (non-paused) torrents
list_active() {
deluge-console "connect $D_HOST:$D_PORT $D_USERNAME $D_PASSWORD ; info --sort=progress" | \
grep -B1 -E "State: Downloading|State: Seeding|State: Queued" | \
awk '/ID:/{ print $2 }' ORS=' '
}
if [ -f /tmp/deluge_active ]; then
get_active=$(cat /tmp/deluge_active)
fi
# Pause (active) torrents
pause_active() {
list_active > /tmp/deluge_active
deluge-console "connect $D_HOST:$D_PORT $D_USERNAME $D_PASSWORD ; pause ${get_active}"
}
# Resume torrents
resume_active() {
if [ -f /tmp/deluge_active ]; then
deluge-console "connect $D_HOST:$D_PORT $D_USERNAME $D_PASSWORD ; resume ${get_active}"
rm /tmp/deluge_active
else
deluge-console "connect $D_HOST:$D_PORT $D_USERNAME $D_PASSWORD ; resume '*'"
fi
}
# Set maximum speed
max_speed() {
[ -n "${U}" ] && U_Speed="; config -s max_upload_speed ${U} "
[ -n "${D}" ] && D_Speed="; config -s max_download_speed ${D} "
deluge-console "connect $D_HOST:$D_PORT $D_USERNAME $D_PASSWORD ${U_Speed}${D_Speed}"
echo "${U_Speed}${D_Speed}"
}
# Show torrent status
info() {
deluge-console "connect $D_HOST:$D_PORT $D_USERNAME $D_PASSWORD ; info --sort=progress" | grep -e 'Name:' -A7 | grep -e '--' -e 'Name' -e 'Progress' -e 'State'
}
# Usage
usage() {
echo "[-U <KBs|-1>] [-D <KBs|-1>] [-p] [-r] [-h] "
}
# Help
help() {
cat <<EOF
-U - Set max upload speed [KBs], use "-1" to set unlimited
-D - Set max download speed [KBs], use "-1" to set unlimited
-p - Pause active torrent
-r - Restore paused torrents (previously active only)
-i - Shows torrent information status
-h | --help - Help
EOF
exit 0; }
# CLI options
while getopts ":U:D:prhi" arg; do
case "${arg}" in
D)
D="${OPTARG}"
max_speed
;;
U)
U="${OPTARG}"
max_speed
;;
p)
pause_deluge
;;
r)
resume_stream
;;
i)
info
;;
h|-help)
help
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
@schwarld
Copy link

Hi,
I got these error trying to test your script. Any thoughts?

2019-03-26 01:08:06 - INFO :: CP Server Thread-11 : Tautulli Notifiers :: Updated notification agent: Script (notifier_id 1).
2019-03-26 01:08:06 - DEBUG :: CP Server Thread-11 : Sending test Script notification.
2019-03-26 01:08:06 - INFO :: Thread-2 : Tautulli NotificationHandler :: Preparing notification for notifier_id 1.
2019-03-26 01:08:06 - DEBUG :: Thread-2 : Tautulli Notifiers :: Trying to run notify script, action: test, arguments: [u'-p']
2019-03-26 01:08:06 - DEBUG :: Thread-2 : Tautulli Notifiers :: Full script is: [u'/scripts/deluge_throttle.sh', '-p']
2019-03-26 01:08:06 - DEBUG :: Thread-2 : Tautulli Notifiers :: Executing script in a new thread.
2019-03-26 01:08:08 - ERROR :: Thread-31 : Tautulli Notifiers :: Script error:
/scripts/deluge_throttle.sh: line 80: pause_deluge: command not found
2019-03-26 01:08:08 - INFO :: Thread-31 : Tautulli Notifiers :: Script notification sent.
2019-03-26 01:08:14 - INFO :: CP Server Thread-11 : Tautulli Notifiers :: Updated notification agent: Script (notifier_id 1).
2019-03-26 01:08:14 - DEBUG :: CP Server Thread-11 : Sending test Script notification.
2019-03-26 01:08:14 - INFO :: Thread-1 : Tautulli NotificationHandler :: Preparing notification for notifier_id 1.
2019-03-26 01:08:14 - DEBUG :: Thread-1 : Tautulli Notifiers :: Trying to run notify script, action: test, arguments: [u'-r']
2019-03-26 01:08:14 - DEBUG :: Thread-1 : Tautulli Notifiers :: Full script is: [u'/scripts/deluge_throttle.sh', '-r']
2019-03-26 01:08:14 - DEBUG :: Thread-1 : Tautulli Notifiers :: Executing script in a new thread.
2019-03-26 01:08:17 - ERROR :: Thread-32 : Tautulli Notifiers :: Script error:
/scripts/deluge_throttle.sh: line 83: resume_stream: command not found
2019-03-26 01:08:17 - INFO :: Thread-32 : Tautulli Notifiers :: Script notification sent.

@sandwormusmc
Copy link

Looks like resume_stream and pause_deluge in the case statement (lines 80 and 83) should be resume_active and pause_active instead...

@Tinynja
Copy link

Tinynja commented Sep 12, 2020

I modified this script a little bit to work with the new version of deluge-console (added --verbose parameter), and adapted it to be used with a docker setup, such as in Unraid. I'm a noob when it comes to GitHub, so I put my updated file as a fork here.

Keep in mind that, if you are using this in an unraid setup like I am, you have to give access to the docker command from the tautulli container. To do that, you can follow these procedures:

  1. SSH into unraid
  2. Take note of the gid of the docker group: cat /etc/group | grep docker
  3. Create a new user called tautulli: useradd -MN -G docker --shell=/bin/false -g DOCKER_GID tautulli
  4. Take note of the uid of the tautulli user: cat /etc/passwd | grep tautulli
  5. Edit the tautulli container settings as follows...
  6. Change the PUID to the tautulli uid (from step 4)
  7. Change the PGID to the tautulli gid (from step 4)
  8. Add a bind mounts /var/run/docker.sock to /var/run/docker.sock
  9. Add a bind mounts /usr/bin/docker to /usr/bin/docker
  10. Modify the variables in deluge_throttle.sh according to your setup

Additionally, you can set the following tautulli conditions to prevent throttling if you are using your plex server locally:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment