Skip to content

Instantly share code, notes, and snippets.

@dimo414
Last active April 3, 2021 18:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimo414/aaaee1c639d292a64b72f4644606fbf0 to your computer and use it in GitHub Desktop.
Save dimo414/aaaee1c639d292a64b72f4644606fbf0 to your computer and use it in GitHub Desktop.
Plex Healthcheck
#!/bin/bash
# Checks that a Plex server is up. For devices on a local network your
# router may have created [hostname].local or [hostname].lan DNS entries;
# otherwise you can pass an IP address or ensure your server is remotely
# accessible (https://support.plex.tv/articles/200931138/).
#
# See also
# https://old.reddit.com/r/PleX/comments/7qolre/what_do_others_do_to_monitor_plex_health/
# https://support.plex.tv/articles/201638786-plex-media-server-url-commands/
#
# Plex supports secure connections via a clever custom certificate scheme
# (https://blog.filippo.io/how-plex-is-doing-https-for-all-its-users/).
#
# Note you must set SECRET_TOKEN to your X-Plex-Token; see
# https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
: "${SECRET_TOKEN:?Must set SECRET_TOKEN env variable}"
HEALTHCHECK_ID=${1:?healthcheck id}
PROTOCOL=http
HOST=${2:?hostname or IP address required}
PORT=${3:-32400}
if [[ -n "$CERTIFICATE_SUFFIX" ]]; then
if RESOLOVED_HOST=$(getent hosts "$HOST"); then
RESOLOVED_HOST=$(awk '{ print $1 }' <<<"$RESOLOVED_HOST")
PROTOCOL=https
HOST="${RESOLOVED_HOST//\./-}.${CERTIFICATE_SUFFIX}"
fi
fi
URL="${PROTOCOL}://${HOST}:${PORT}/connections?X-Plex-Token=${SECRET_TOKEN}"
# https://healthchecks.io/docs/bash/
ping_hc() {
curl -fsS -m 10 --retry 5 -o /dev/null --data-raw "$2" "https://hc-ping.com/${HEALTHCHECK_ID}/$1"
}
connections=$(set -x; { wget -nv -O - "$URL"; } 2>&1)
ping_hc "$?" "$connections"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment