Update tdarr_node_killer.sh
This commit is contained in:
@@ -1,45 +1,55 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ============================================================
|
||||
# Script Name: Plex Transcoding & Docker Management Script
|
||||
# Author: Admin9705 & Reddit
|
||||
# Description:
|
||||
# This script monitors Plex transcoding activity on a specified
|
||||
# GPU device. If Plex is detected using the GPU, it resets a
|
||||
# restart counter. If Plex does not use the GPU for a defined
|
||||
# number of checks, it increments a counter. Once the counter
|
||||
# reaches a threshold, the script restarts a specified Docker
|
||||
# container. The script is designed to run indefinitely and
|
||||
# includes logging for monitoring and troubleshooting.
|
||||
# ============================================================
|
||||
# NOTE: Requires Tautulli
|
||||
# ============================================================
|
||||
#!/bin/bash
|
||||
# v1 - Intial
|
||||
# v2 - Prevent Tdarr Node from being killed when only LOCAL is transcoding
|
||||
|
||||
# Configuration
|
||||
TAUTULLI_API_KEY="dad9bbb78bde43249754b630b58fbf6a" # Tautulli API Key
|
||||
TAUTULLI_URL="http://10.0.0.10:8181/api/v2" # Tautulli URL
|
||||
WAIT_SECONDS=180 # Wait time (seconds) to bring docker container back up when it was stopped
|
||||
BASIC_CHECK=3 # Check Plex Activity intervals (seconds) when not playing
|
||||
CONTAINER_NAME="N1" # Exact of your Tdarr Node that you want stopped
|
||||
TAUTULLI_API_KEY="dad9bbb78bde43249754b630b58fbf7c" # Tautulli API Key
|
||||
TAUTULLI_URL="http://10.0.0.10:8181/api/v2" # Tautulli URL
|
||||
WAIT_SECONDS=180 # Wait time (in seconds) after killing the tdarr node before checking again
|
||||
BASIC_CHECK=3 # Basic check interval when Plex is idle
|
||||
CONTAINER_NAME="N4" # The exact name of your tdarr node container
|
||||
# Option: set to "yes" to disable tdarr only when a remote transcode is detected.
|
||||
# When set to "yes", if a remote transcode is active (even with local transcodes), tdarr is disabled.
|
||||
# Set to "no" to disable tdarr whenever any transcoding session (local or remote) is detected.
|
||||
DISABLE_TDARR_FOR_LOCAL_ONLY="yes"
|
||||
|
||||
# Function to check if Plex is transcoding via Tautulli
|
||||
# Function to check if Plex is transcoding via Tautulli.
|
||||
is_plex_transcoding() {
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - Checking Plex activity via Tautulli API"
|
||||
response=$(curl -s "${TAUTULLI_URL}?apikey=${TAUTULLI_API_KEY}&cmd=get_activity")
|
||||
# Count how many sessions are transcoding
|
||||
transcoding_count=$(echo "$response" | jq '[.response.data.sessions[]?.transcode_decision == "transcode"] | map(select(. == true)) | length')
|
||||
|
||||
if [ "$transcoding_count" -gt 0 ]; then
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - Plex is currently transcoding $transcoding_count session(s)."
|
||||
return 0
|
||||
# Count local and remote transcoding sessions.
|
||||
# Assumes that each session includes an "ip_address" field.
|
||||
local_count=$(echo "$response" | jq '[.response.data.sessions[]? | select(.transcode_decision == "transcode" and (.ip_address | startswith("10.0.0.")))] | length')
|
||||
remote_count=$(echo "$response" | jq '[.response.data.sessions[]? | select(.transcode_decision == "transcode" and (.ip_address | startswith("10.0.0.") | not))] | length')
|
||||
total_count=$(( local_count + remote_count ))
|
||||
|
||||
# Log counts.
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - Detected ${local_count} local and ${remote_count} remote transcoding session(s)."
|
||||
|
||||
if [ "$DISABLE_TDARR_FOR_LOCAL_ONLY" = "yes" ]; then
|
||||
# Only remote transcoding triggers tdarr disable, even if local transcodes exist.
|
||||
if [ "$remote_count" -gt 0 ]; then
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - Remote transcoding detected."
|
||||
return 0
|
||||
else
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - No remote transcoding detected (only local transcoding active or none at all)."
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - No Plex transcoding detected."
|
||||
return 1
|
||||
# Any transcoding session triggers tdarr disable.
|
||||
if [ "$total_count" -gt 0 ]; then
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - Plex is transcoding ${total_count} session(s)."
|
||||
return 0
|
||||
else
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - No Plex transcoding detected."
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if the container is running
|
||||
# Function to check if the container is running.
|
||||
is_container_running() {
|
||||
state=$(docker inspect -f '{{.State.Running}}' "${CONTAINER_NAME}" 2>/dev/null)
|
||||
if [ "$state" = "true" ]; then
|
||||
@@ -51,7 +61,7 @@ is_container_running() {
|
||||
|
||||
while true; do
|
||||
if is_plex_transcoding; then
|
||||
# Plex is transcoding, ensure container is not running
|
||||
# If transcoding (by the chosen criteria) is detected, ensure the container is not running.
|
||||
if is_container_running; then
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - Killing Docker container ${CONTAINER_NAME} due to Plex transcoding."
|
||||
docker kill "${CONTAINER_NAME}"
|
||||
@@ -59,12 +69,11 @@ while true; do
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - ${CONTAINER_NAME} is already stopped."
|
||||
fi
|
||||
|
||||
# Wait before checking again
|
||||
# Wait before checking again.
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - Sleeping for ${WAIT_SECONDS} seconds..."
|
||||
sleep "${WAIT_SECONDS}"
|
||||
else
|
||||
# Plex is not transcoding
|
||||
# Check if container is running, if not, start it
|
||||
# When no triggering transcode is detected, if the container is stopped then start it.
|
||||
if ! is_container_running; then
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting Docker container ${CONTAINER_NAME} since Plex is not transcoding."
|
||||
docker start "${CONTAINER_NAME}"
|
||||
@@ -72,7 +81,7 @@ while true; do
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - ${CONTAINER_NAME} is already running, no action needed."
|
||||
fi
|
||||
|
||||
# Sleep before next check
|
||||
# Sleep before next check.
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - Sleeping for ${BASIC_CHECK} seconds..."
|
||||
sleep "${BASIC_CHECK}"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user