Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nix/modules/plex/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM ghcr.io/jsclayton/prometheus-plex-exporter:main@sha256:18ef1b2197efbcb75bd7276380955760995f10a9fbe55106809a6fcff91c2940
3 changes: 3 additions & 0 deletions nix/modules/plex/images.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ghcr.io/jsclayton/prometheus-plex-exporter@sha256:18ef1b2197efbcb75bd7276380955760995f10a9fbe55106809a6fcff91c2940": "sha256-mQamhULYIedN46UgjZRnegqL6pWM5bmydH2NPKxAH68="
}
72 changes: 21 additions & 51 deletions nix/modules/plex/metrics.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,22 @@
...
}:
let
ccfg = config.homelab.cluster;
cfg = config.homelab.plex;
exporter = pkgs.buildGoModule {
pname = "prometheus-plex-exporter";
# No releases: main as of 2024-08-21
version = "0-unstable-2024-08-21";
src = pkgs.fetchFromGitHub {
owner = "jsclayton";
repo = "prometheus-plex-exporter";
rev = "041ea971fadc6db467ef8f792b6f4f824cc3034e";
hash = "sha256-hKscZpuBXV0fJ3uAdzfFqyByvOaJNjJTJd8Ey+tD/CQ=";
};
# Vendored in the repo
vendorHash = null;
subPackages = [ "cmd/prometheus-plex-exporter" ];
meta.mainProgram = "prometheus-plex-exporter";
};
# The exporter takes the admin token in PLEX_TOKEN. Plex keeps the token of the account that claimed it in
# Preferences.xml, on the data volume the sidecar mounts read-only: no secret to set up. It exits when Plex isn't up
# at its start or its event stream drops, and a sidecar that restarts takes the pod out of the Service, so this
# runs it again instead, and waits for the token while the server isn't claimed yet
wrapper = pkgs.writeShellApplication {
name = "plex-exporter";
runtimeInputs = [
exporter
pkgs.xq-xml
pkgs.coreutils
];
text = ''
prefs=$1
pid=
trap 'kill "$pid" 2>/dev/null; wait "$pid" || true; exit 0' TERM INT
while :; do
if PLEX_TOKEN=$(xq -x '//Preferences/@PlexOnlineToken' "$prefs" 2>/dev/null) && [[ -n $PLEX_TOKEN ]]; then
export PLEX_TOKEN
prometheus-plex-exporter &
pid=$!
wait "$pid" || true
fi
sleep 10 &
pid=$!
wait "$pid" || true
done
'';
};
image = pkgs.dockerTools.buildImage {
name = "cluster.local/plex-exporter";
copyToRoot = [ wrapper ] ++ lib.optionals cfg.debug ccfg.debugTools;
config.Entrypoint = [ (lib.getExe wrapper) ];
};
# Its published image (alpine and the binary; no releases, so main by digest), pinned in the Dockerfile here
image = (inputs.homelab-shared.lib.images.pinned pkgs ./Dockerfile)."ghcr.io/jsclayton/prometheus-plex-exporter";
# The exporter takes the admin token in PLEX_TOKEN. Plex keeps it in Preferences.xml once claimed, on the data
# volume the sidecar mounts read-only: wait until it's there, then exec the exporter. Waiting keeps the pod Ready,
# so an unclaimed Plex stays reachable to be claimed; when the exporter exits, the kubelet restarts it
start = ''
prefs='/Library/Application Support/Plex Media Server/Preferences.xml'
trap 'exit 0' TERM INT
until PLEX_TOKEN=$(sed -n 's/.*PlexOnlineToken="\([^"]*\)".*/\1/p' "$prefs" 2>/dev/null) && [ -n "$PLEX_TOKEN" ]; do
sleep 10 &
wait $!
done
export PLEX_TOKEN
exec /prometheus-plex-exporter
'';
# Where it listens, fixed in the exporter
port = 9000;
in
Expand All @@ -71,9 +37,13 @@ in
{
# After the main container: kubectl logs and exec still go to Plex without -c
deployments.plex.spec.template.spec.containers.plex-exporter = {
image = "${image.buildArgs.name}:${image.imageTag}";
image = image.ref;
imagePullPolicy = "Never";
args = [ "/Library/Application Support/Plex Media Server/Preferences.xml" ];
command = [
"/bin/sh"
"-c"
start
];
env.PLEX_SERVER.value = "http://127.0.0.1:32400";
ports = [
{
Expand Down
Loading