Skip to content

Instantly share code, notes, and snippets.

@Tinynja
Forked from Generator/Deluge_Throttle.md
Last active June 2, 2023 00:30
Show Gist options
  • Save Tinynja/2169be3f20b8656f67dbc89129d57598 to your computer and use it in GitHub Desktop.
Save Tinynja/2169be3f20b8656f67dbc89129d57598 to your computer and use it in GitHub Desktop.
Throttle Deluge on Plex stream Start/Stop

Dependencies

  • deluge-console (or a deluge docker container)

Install

$ wget -O deluge_throttle.sh https://gist.github.com/Tinynja/2169be3f20b8656f67dbc89129d57598/raw/66409a4ebdec1d2880d288c26e9f8bc75b4f900f/deluge_throttle.sh
$ chmod +x deluge_throttle.sh

Script Setup

Edit deluge_throttle.sh and set deluged username and password.

If you are running deluge and tautulli as docker containers, set host to 127.0.0.1 and docker_container to the name of your deluge docker container.

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

Unraid Setup

If you are using this in an unraid setup, you have to give access to the docker command from the tautulli container. This is one way to do it:

  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

Tautulli Setup

Commum Scripts Settings in Tautulli

  1. Taultulli > Settings > Notification Agents > Add a Notification Agent > Script
  2. Set Script Folder
  3. Select deluge_throttle.sh
  4. Script Timeout 0

Throttle only on wan connections

Notification conditions:

  • Condition {1}: Stream Local is 0
  • Condition {2}: Action is play
  • Condition {3}: Streams is 0

Condition logic: {1} and {2} or {3}

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

-D		- Set max download speed [KBs], use "-1" to set unlimited
-U		- Set max upload 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= # deluged host (127.0.0.1 if running with docker)
D_PORT=58846 # deluged port
docker_container= # name of the deluge docker container (leave empty if not running with docker)
## CODE ##
if [ -z "$docker_container" ]; then
deluge_console="deluge-console -d $D_HOST -p $D_PORT -U $D_USERNAME -P $D_PASSWORD"
else
deluge_console="docker exec $docker_container deluge-console -d $D_HOST -p $D_PORT -U $D_USERNAME -P $D_PASSWORD"
fi
# List active (non-paused) torrents
list_active() {
$deluge_console "info --verbose --sort=progress" | \
grep -B1 -E "State: Downloading|State: Seeding|State: Queued" | \
awk '/ID:/{ print $2 }' ORS=' '
}
# Pause (active) torrents
pause_active() {
list_active > /tmp/deluge_active
if [ -s /tmp/deluge_active ]; then
$deluge_console "pause $(cat /tmp/deluge_active)"
else
echo "No active torrents to pause."
fi
}
# Resume torrents
resume_active() {
if [ -s /tmp/deluge_active ]; then
$deluge_console "resume $(cat /tmp/deluge_active)"
rm /tmp/deluge_active
else
echo "No active torrents to resume."
fi
}
# Set maximum download speed
max_download_speed() {
$deluge_console "config -s max_download_speed ${D}"
}
# Set maximum upload speed
max_upload_speed() {
$deluge_console "config -s max_upload_speed ${U}"
}
# Show torrent status
info() {
$deluge_console "info --verbose --sort=upload_payload_rate" | grep -e 'Name:' -A2 | grep -e '--' -e 'Name' -e 'State'
}
# Usage
usage() {
echo "[-U <KBs|-1>] [-D <KBs|-1>] [-p] [-r] [-h] "
}
# Help
help() {
cat <<EOF
-D - Set max download speed [KBs], use "-1" to set unlimited
-U - Set max upload 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_download_speed
;;
U)
U="${OPTARG}"
max_upload_speed
;;
p)
pause_active
;;
r)
resume_active
;;
i)
info
;;
h|-help)
help
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
@theovit
Copy link

theovit commented Feb 16, 2022

I'm having some issues. I have a VPS with limited privileges. I don't have access to make changes to dockers or run the command useradd. The script runs fine from SSH but not when I test it in Tautulli. I have a feeling I'm just not going to be able to use this. Below is the log from Tautulli. It doesn't seem to have access to deluge-console. Thoughts?

Thanks in advance!

2022-02-16 01:27:49 - INFO :: ('CP Server Thread-19',) : Tautulli Notifiers :: Updated notification agent: Script (notifier_id 2).
2022-02-16 01:27:49 - DEBUG :: ('CP Server Thread-22',) : Sending test Script notification.
2022-02-16 01:27:49 - DEBUG :: ('CP Server Thread-22',) : Tautulli NotificationHandler :: Notifiers enabled for notify_action 'test' (manual trigger).
2022-02-16 01:27:49 - INFO :: Thread-3 : Tautulli NotificationHandler :: Preparing notification for notifier_id 2.
2022-02-16 01:27:50 - DEBUG :: Thread-3 : Tautulli Notifiers :: Trying to run notify script: /config/scripts/deluge_throttle.sh, arguments: ['-p'], action: test
2022-02-16 01:27:50 - DEBUG :: Thread-3 : Tautulli Notifiers :: Full script is: ['/config/scripts/deluge_throttle.sh', '-p']
2022-02-16 01:27:50 - DEBUG :: Thread-3 : Tautulli Notifiers :: Executing script in a new thread.
2022-02-16 01:27:50 - DEBUG :: Thread-38 : Tautulli Notifiers :: Subprocess returned with status code 0.
2022-02-16 01:27:50 - ERROR :: Thread-38 : Tautulli Notifiers :: Script error:
/config/scripts/deluge_throttle.sh: line 19: deluge-console: command not found
2022-02-16 01:27:50 - DEBUG :: Thread-38 : Tautulli Notifiers :: Script returned:
No active torrents to pause.
2022-02-16 01:27:50 - INFO :: Thread-38 : Tautulli Notifiers :: Script notification sent.

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