From 67108e55295a7309b38f50bb5fa463f6bc8a586b Mon Sep 17 00:00:00 2001 From: Finn Christiansen Date: Mon, 9 Jun 2025 22:19:41 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20Project=20init?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 45 ++++++++++++++++++++++++++++++++++++++++++-- jellyfin-wol.service | 13 +++++++++++++ jellyfin_wol.py | 31 ++++++++++++++++++++++++++++++ mount_nas.sh | 25 ++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 jellyfin-wol.service create mode 100755 jellyfin_wol.py create mode 100755 mount_nas.sh diff --git a/README.md b/README.md index f7dc8ac..6f848db 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,44 @@ -# jellyfin-wol +# Jellyfin WOL -Wake up your NAS or another server on your LAN when someone logs into your Jellyfin. +Wake up your NAS or another server on your LAN when someone logs into your Jellyfin. + +## Installation + +Clone or copy the files and place them here: + + +``` +/usr/local/bin/jellyfin_wol.py +/etc/systemd/system/jellyfin-wol.service +``` + +Create a `.env` at `/etc/jellyfin_wol_environment` with following content: + +``` +MAC_ADDRESS=1c:1b:0d:99:5c:48 +LISTEN_ADDRESS=0.0.0.0 +LISTEN_PORT=8092 +``` + +Set the MAC address to the one you want to wake up. + +Enable and start the service: + +``` +systemctl enable jellyfin-wol +systemctl start jellyfin-wol +``` + +Install the Jellyfin webhook plugin and click **Add Generic Form Destination**. Give ti some name, set the URL to +`http://your-jellyfin:8092`, change the hostname and port as neeed. Make sure **Status** is enabled and +**Notification Type** has "Authentication Success" checked. + +Scroll down to **Item Types** and make sure these two options are checked: + +- Send All Properties (ignores template) +- Trim leading and trailing whitespace from message body before sending + +Hit **Save**, log out and log in again. The magic packet now has been sent. + +Check the `mount_nas` script, maybe it could be helpful. I run it as a cron every minute to mount the NFS share after the +NAS becomes online. As a last step I restart the Jellyfin container (the NAS share is mounted via docker volume). diff --git a/jellyfin-wol.service b/jellyfin-wol.service new file mode 100644 index 0000000..7d765ac --- /dev/null +++ b/jellyfin-wol.service @@ -0,0 +1,13 @@ +[Unit] +Description=Jellyfin WOL Service + +[Service] +Type=simple +User=root +#EnvironmentFile=/etc/powermeter-environment +ExecStart=/usr/local/bin/jellyfin_wol.py +Restart=always +RestartSec=30 + +[Install] +WantedBy=multi-user.target diff --git a/jellyfin_wol.py b/jellyfin_wol.py new file mode 100755 index 0000000..15a0132 --- /dev/null +++ b/jellyfin_wol.py @@ -0,0 +1,31 @@ +#!/usr/bin/python3 +from http.server import HTTPServer, BaseHTTPRequestHandler +from urllib.parse import parse_qs +from wakeonlan import send_magic_packet +import os + + +class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): + def do_POST(self): + content_length = int(self.headers['Content-Length']) + raw_data = self.rfile.read(content_length).decode('utf-8') + post_data = parse_qs(raw_data) + + # Check if the payload indicates a user login event + if 'NotificationType' in post_data and 'AuthenticationSuccess' in post_data['NotificationType']: + print("User login detected! Waking up NAS.") + send_magic_packet('1c:1b:0d:99:5c:48') + + # Send a 200 OK response + self.send_response(200) + self.end_headers() + self.wfile.write(b'{"status": "success"}') + + +# Configure and start the HTTP server +if __name__ == '__main__': + host = os.getenv('LISTEN_ADDRESS', '0.0.0.0') + port = int(os.getenv('LISTEN_PORT', 8092)) + httpd = HTTPServer((host, port), SimpleHTTPRequestHandler) + print(f"Listening on {host}:{port}...") + httpd.serve_forever() diff --git a/mount_nas.sh b/mount_nas.sh new file mode 100755 index 0000000..c42e406 --- /dev/null +++ b/mount_nas.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Automatically mount NFS share if NAs becomes available. +# Restart jellyfin container after mounting. + +DIR="/mnt/Medien" + +ping 192.168.1.3 -c 1 > /dev/null 2>&1 +PING_STATUS=$? +mount | grep "$DIR" > /dev/null 2>&1 +NAS_MOUNT=$? + +if [ "$PING_STATUS" -eq 0 ]; then + # Host reachable + if [ "$NAS_MOUNT" -eq 1 ]; then + # nout mounted yet + mount "$DIR" && cd /opt/stacks/jellyfin/ && docker compose down && docker compose up -d + fi +else + if [ $NAS_MOUNT -eq 0 ]; then + # still mounted + umount -l "$DIR" + fi +fi +