Skip to content

Instantly share code, notes, and snippets.

@rourke
Last active January 31, 2024 11:49
Show Gist options
  • Save rourke/a553e54062af1149ff226df36cdc9e90 to your computer and use it in GitHub Desktop.
Save rourke/a553e54062af1149ff226df36cdc9e90 to your computer and use it in GitHub Desktop.
qBittorrent healthcheck script

qBittorrent healthcheck script

A qBittorrent script that takes advantage of the docker healthcheck ability to do a few things every x seconds:

  1. Check if qBitorrent is up
  2. If yes, check if the configured listening port is the same as the forwarded port of the VPN provider.
  3. If they don't match, update the listening port.

If at any stage the results are unexpected, it will exit with code 1, meaning the container is classified as unhealthy in Docker.

Usage

The script assumes the following:

  • The forwarded port is stored in a file mounted to /forwarded_port.dat
  • The Bypass authentication for clients on localhost setting in the qBittorrent Web UI options is enabled.
  • Logging is disabled by default, but if enabled the log file is /healthcheck.log. Mounting is optional.

An example healthcheck in docker-compose.yml would look like this:

    healthcheck:
      test: ["CMD", "/qbit-healthcheck.sh"]
      interval: 20s
      timeout: 10s
      start_period: 30s

NOTE: The script needs to be mounted to /qbit-healthcheck.sh if you're using this healthcheck example.

#!/bin/bash
#
# This script first checks if qBittorrent is up, and after
# that changes the listening port to that of the forwarded
# port of the VPN.
#
# This script requires to enable the "Bypass authentication
# for clients on localhost" in the Web UI options.
#
# https://gist.github.com/rourke/a553e54062af1149ff226df36cdc9e90
#
# Author: github.com/rourke
# Logging information | 0 = disabled | 1 = enabled
logging=0
# The file containing the forwarded port of the VPN
vpn_port_file="/forwarded_port.dat"
# Log file in case logging is enabled
healthcheck_log_file="/healthcheck.log"
function log() {
if [[ $logging == 1 ]]; then
echo "$(date): $1" >> ${healthcheck_log_file}
fi
}
log "-------------"
# Check if vpn port file exists
if [ ! -f "${vpn_port_file}" ]; then
log "Script error: the file containing the VPN forwarded port (${vpn_port_file}) does not exist."
exit 1
fi
# Check if API call returns http code 200
http_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/api/v2/app/version)
if [[ $http_code != 200 ]]; then
log "App status: not up yet, did you enable \"Bypass authentication for clients on localhost\" in the Web UI options?"
exit 1
else
log "App status: up and running"
fi
# Get current listening port and vpn forwarded port
current_forwarded_port=$(curl -s http://localhost:8080/api/v2/app/preferences | jq '.listen_port')
forwarded_port_vpn=$(cat ${vpn_port_file})
if [[ $forwarded_port_vpn != $current_forwarded_port ]]; then
# Ports are not the same, so update the port
resp_code=$(curl -s -o /dev/null -w "%{http_code}" --request POST --data 'json={"listen_port":'${forwarded_port_vpn}'}' http://localhost:8080/api/v2/app/setPreferences)
if [[ $resp_code == 200 ]]; then
log "Listening port: updated from ${current_forwarded_port} to ${forwarded_port_vpn}"
else
log "Listening port: could not update from ${current_forwarded_port} to ${forwarded_port_vpn}: HTTP ${resp_code}"
exit 1
fi
else
log "Listening port: good (${current_forwarded_port})"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment