From c56f3edaacc4f4b67e5912028ca71e086b1643fe Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Wed, 21 Feb 2024 10:46:34 -0500 Subject: [PATCH] test --- HostConfig.sh | 258 ++++++++++++++++++++++++++---------------- plex-library-stats.sh | 60 ++++++++++ 2 files changed, 222 insertions(+), 96 deletions(-) create mode 100644 plex-library-stats.sh diff --git a/HostConfig.sh b/HostConfig.sh index 9b92a0e..065b2e1 100644 --- a/HostConfig.sh +++ b/HostConfig.sh @@ -5,6 +5,10 @@ HostConfig() { PIDOF="pidof" STATFMT="-c" STATBYTES="%s" + STATPERMS="%a" + + # On all hosts except QNAP + DFFLAGS="-m" # Synology (DSM 7) if [ -d /var/packages/PlexMediaServer ] && \ @@ -81,9 +85,30 @@ HostConfig() { StopCommand="/etc/init.d/plex.sh stop" fi + # Use custom DFFLAGS (force POSIX mode) + DFFLAGS="-Pm" + HostType="QNAP" return 0 + # SNAP host (check before standard) + elif [ -d "/var/snap/plexmediaserver/common/Library/Application Support/Plex Media Server" ]; then + + # Where things are + PLEX_SQLITE="/snap/plexmediaserver/current/Plex SQLite" + AppSuppDir="/var/snap/plexmediaserver/common/Library/Application Support" + PID_FILE="$AppSuppDir/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + HaveStartStop=1 + StartCommand="snap start plexmediaserver" + StopCommand="snap stop plexmediaserver" + + HostType="SNAP" + return 0 + # Standard configuration Linux host elif [ -f /etc/os-release ] && \ [ -d /usr/lib/plexmediaserver ] && \ @@ -102,18 +127,13 @@ HostConfig() { # Find the metadata dir if customized if [ -e /etc/systemd/system/plexmediaserver.service.d ]; then - # Glob up all 'conf files' found - NewSuppDir="$(cd /etc/systemd/system/plexmediaserver.service.d ; \ - cat override.conf local.conf *.conf 2>/dev/null | grep "APPLICATION_SUPPORT_DIR" | head -1)" + # Get custom AppSuppDir if specified + NewSuppDir="$(GetOverride PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR)" - if [ "$NewSuppDir" != "" ]; then - NewSuppDir="$(echo $NewSuppDir | sed -e 's/.*_DIR=//' | tr -d '"' | tr -d "'")" - - if [ -d "$NewSuppDir" ]; then + if [ -d "$NewSuppDir" ]; then AppSuppDir="$NewSuppDir" - else + else Output "Given application support directory override specified does not exist: '$NewSuppDir'. Ignoring." - fi fi fi @@ -144,6 +164,10 @@ HostConfig() { LOGFILE="$DBDIR/DBRepair.log" LOG_TOOL="logger" + HaveStartStop=1 + StartCommand="systemctl start fvapp-plexmediaserver" + StopCommand="systemctl stop fvapp-plexmediaserver" + HostType="Netgear ReadyNAS" return 0 fi @@ -163,93 +187,6 @@ HostConfig() { HostType="ASUSTOR" return 0 - # Containers: - # - Docker cgroup v1 & v2 - # - Podman (libpod) - elif [ "$(grep docker /proc/1/cgroup | wc -l)" -gt 0 ] || [ "$(grep 0::/ /proc/1/cgroup)" = "0::/" ] || - [ "$(grep libpod /proc/1/cgroup | wc -l)" -gt 0 ]; then - - # HOTIO Plex image structure is non-standard (contains symlink which breaks detection) - if [ -d "/app/usr/lib/plexmediaserver" ] && [ -d "/config/Plug-in Support" ]; then - PLEX_SQLITE="/app/usr/lib/plexmediaserver/Plex SQLite" - AppSuppDir="/config" - PID_FILE="$AppSuppDir/plexmediaserver.pid" - DBDIR="$AppSuppDir/Plug-in Support/Databases" - LOGFILE="$DBDIR/DBRepair.log" - LOG_TOOL="logger" - - HostType="HOTIO" - return 0 - - # Docker (All main image variants except binhex and hotio) - elif [ -d "/config/Library/Application Support" ]; then - - PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite" - AppSuppDir="/config/Library/Application Support" - PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" - DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" - LOGFILE="$DBDIR/DBRepair.log" - LOG_TOOL="logger" - - if [ -d "/var/run/service/svc-plex" ]; then - HaveStartStop=1 - StartCommand="s6-svc -u /var/run/service/svc-plex" - StopCommand="s6-svc -d /var/run/service/svc-plex" - fi - - if [ -d "/var/run/s6/services/plex" ]; then - HaveStartStop=1 - StartCommand="s6-svc -u /var/run/s6/services/plex" - StopCommand="s6-svc -d /var/run/s6/services/plex" - fi - - # lsio stop - if [ -d "/var/run/service/svc-plex" ]; then - HaveStartStop=1 - StartCommand="s6-svc -u /var/run/service/svc-plex" - StopCommand="s6-svc -d /var/run/service/svc-plex" - fi - - # HOTIO - if [ -d /run/service/plex ]; then - HaveStartStop=1 - StartCommand="s6-svc -u /run/service/plex" - StopCommand="s6-svc -d /run/service/plex" - fi - - HostType="Docker" - return 0 - - # BINHEX Plex image - elif [-f /usr/lib/python3.10/binhex.py ] && [ -d "/config/Plex Media Server" ]; then - - PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite" - AppSuppDir="/config" - PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" - DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" - LOGFILE="$DBDIR/DBRepair.log" - LOG_TOOL="logger" - - HostType="BINHEX" - return 0 - - fi - - - # Western Digital (OS5) - elif [ -f /etc/system.conf ] && [ -d /mnt/HD/HD_a2/Nas_Prog/plexmediaserver ] && \ - grep "Western Digital Corp" /etc/system.conf >/dev/null; then - - # Where things are - PLEX_SQLITE="/mnt/HD/HD_a2/Nas_Prog/plexmediaserver/binaries/Plex SQLite" - AppSuppDir="$(echo /mnt/HD/HD*/Nas_Prog/plex_conf)" - PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" - DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" - LOGFILE="$DBDIR/DBRepair.log" - LOG_TOOL="logger" - - HostType="Western Digital" - return 0 # Apple Mac elif [ -d "/Applications/Plex Media Server.app" ] && \ @@ -267,6 +204,10 @@ HostConfig() { PIDOF="pgrep" STATFMT="-f" STATBYTES="%z" + STATPERMS="%A" + + # Root not required on MacOS. PMS runs as username. + RootRequired=0 # make the TMP directory in advance to store plexmediaserver.pid mkdir -p "$DBDIR/dbtmp" @@ -280,8 +221,133 @@ HostConfig() { HostType="Mac" return 0 + + # Western Digital (OS5) + elif [ -f /etc/system.conf ] && [ -d /mnt/HD/HD_a2/Nas_Prog/plexmediaserver ] && \ + grep "Western Digital Corp" /etc/system.conf >/dev/null; then + + # Where things are + PLEX_SQLITE="/mnt/HD/HD_a2/Nas_Prog/plexmediaserver/binaries/Plex SQLite" + AppSuppDir="$(echo /mnt/HD/HD*/Nas_Prog/plex_conf)" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + HostType="Western Digital" + return 0 + + + # - Docker cgroup v1 & v2 + # - Podman (libpod) + # - Kubernetes (and TrueNAS platforms) + elif [ "$(grep docker /proc/1/cgroup | wc -l)" -gt 0 ] || [ "$(grep 0::/ /proc/1/cgroup)" = "0::/" ] || + [ "$(grep libpod /proc/1/cgroup | wc -l)" -gt 0 ] || [ "$(grep kube /proc/1/cgroup | wc -l)" -gt 0 ]; then + + # HOTIO Plex image structure is non-standard (contains symlink which breaks detection) + if [ -n "$(grep -irslm 1 hotio /etc/s6-overlay/s6-rc.d)" ]; then + PLEX_SQLITE=$(find /app/usr/lib/plexmediaserver /usr/lib/plexmediaserver -maxdepth 0 -type d -print -quit 2>/dev/null); PLEX_SQLITE="$PLEX_SQLITE/Plex SQLite" + AppSuppDir="/config" + PID_FILE="$AppSuppDir/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + if [ -d "/run/service/plex" ] || [ -d "/run/service/service-plex" ]; then + SERVICE_PATH=$([ -d "/run/service/plex" ] && echo "/run/service/plex" || [ -d "/run/service/service-plex" ] && echo "/run/service/service-plex") + HaveStartStop=1 + StartCommand="s6-svc -u $SERVICE_PATH" + StopCommand="s6-svc -d $SERVICE_PATH" + fi + + HostType="HOTIO" + return 0 + + # Docker (All main image variants except binhex and hotio) + elif [ -d "/config/Library/Application Support" ]; then + + PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite" + AppSuppDir="/config/Library/Application Support" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + # Miscellaneous start/stop methods + if [ -d "/var/run/service/svc-plex" ]; then + HaveStartStop=1 + StartCommand="s6-svc -u /var/run/service/svc-plex" + StopCommand="s6-svc -d /var/run/service/svc-plex" + fi + + if [ -d "/var/run/s6/services/plex" ]; then + HaveStartStop=1 + StartCommand="s6-svc -u /var/run/s6/services/plex" + StopCommand="s6-svc -d /var/run/s6/services/plex" + fi + + HostType="Docker" + return 0 + + # BINHEX Plex image + elif [ -e /etc/os-release ] && grep "IMAGE_ID=archlinux" /etc/os-release 1>/dev/null && \ + [ -e /home/nobody/start.sh ] && grep PLEX_MEDIA /home/nobody/start.sh 1> /dev/null ; then + + PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite" + AppSuppDir="/config" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + HostType="BINHEX" + return 0 + + fi + + # Last chance to identify this host + elif [ -e /etc/os-release ]; then + + # Arch Linux (must check for native Arch after binhex) + if [ "$(grep -E '=arch|="arch"' /etc/os-release)" != "" ] && \ + [ -d /usr/lib/plexmediaserver ] && \ + [ -d /var/lib/plex ]; then + + # Where is the software + PKGDIR="/usr/lib/plexmediaserver" + PLEX_SQLITE="$PKGDIR/Plex SQLite" + LOG_TOOL="logger" + + # Where is the data + AppSuppDir="/var/lib/plex" + + # Find the metadata dir if customized + if [ -e /etc/systemd/system/plexmediaserver.service.d ]; then + + # Get custom AppSuppDir if specified + NewSuppDir="$(GetOverride PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR)" + + if [ "$NewSuppDir" != "" ]; then + if [ -d "$NewSuppDir" ]; then + AppSuppDir="$NewSuppDir" + else + Output "Given application support directory override specified does not exist: '$NewSuppDir'. Ignoring." + fi + fi + fi + + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + HostType="$(grep PRETTY_NAME /etc/os-release | sed -e 's/^.*="//' | tr -d \" )" + + HaveStartStop=1 + StartCommand="systemctl start plexmediaserver" + StopCommand="systemctl stop plexmediaserver" + return 0 + fi fi + # Unknown / currently unsupported host return 1 } diff --git a/plex-library-stats.sh b/plex-library-stats.sh new file mode 100644 index 0000000..9fd145a --- /dev/null +++ b/plex-library-stats.sh @@ -0,0 +1,60 @@ +#!/bin/bash +# Generate Plex Library stats + + +# ChuckPa already wrote a great function for getting host info +source ./HostConfig.sh + +logfile="$DBDIR/plexstats.log" +db="$DBDIR/com.plexapp.plugins.library.db" +echo "$(date "+%d.%m.%Y %T") PLEX LIBRARY STATS" | tee -a $logfile +echo "Media items in Libraries" | tee -a $logfile +query="SELECT Library, Items \ + FROM ( SELECT name AS Library, \ + COUNT(duration) AS Items \ + FROM media_items m \ + LEFT JOIN library_sections l ON l.id = m.library_section_id \ + WHERE library_section_id > 0 GROUP BY name );" +sqlite3 -header -line "$db" "$query" | tee -a $logfile +echo " " | tee -a +echo "Time to watch" | tee -a $logfile +query="SELECT Library, Minutes, Hours, Days \ + FROM ( SELECT name AS Library, \ + SUM(duration)/1000/60 AS Minutes, \ + SUM(duration)/1000/60/60 AS Hours, \ + SUM(duration)/1000/60/60/24 AS Days \ + FROM media_items m \ + LEFT JOIN library_sections l ON l.id = m.library_section_id \ + WHERE library_section_id > 0 GROUP BY name );" +sqlite3 -header -line "$db" "$query" | tee -a $logfile + +echo " " | tee -a + +query="select count(*) from media_items" +result=$(sqlite3 -header -line "$db" "$query") +echo "${result:11} files in library" | tee -a $logfile + +query="select count(*) from media_items where bitrate is null" +result=$(sqlite3 -header -line "$db" "$query") +echo "${result:11} files missing analyzation info" | tee -a $logfile + +query="SELECT count(*) FROM media_parts WHERE deleted_at is not null" +result=$(sqlite3 -header -line "$db" "$query") +echo "${result:11} media_parts marked as deleted" | tee -a $logfile + +query="SELECT count(*) FROM metadata_items WHERE deleted_at is not null" +result=$(sqlite3 -header -line "$db" "$query") +echo "${result:11} metadata_items marked as deleted" | tee -a $logfile + +query="SELECT count(*) FROM directories WHERE deleted_at is not null" +result=$(sqlite3 -header -line "$db" "$query") +echo "${result:11} directories marked as deleted" | tee -a $logfile + +query="select count(*) from metadata_items meta \ + join media_items media on media.metadata_item_id = meta.id \ + join media_parts part on part.media_item_id = media.id \ + where part.extra_data not like '%deepAnalysisVersion=2%' \ + and meta.metadata_type in (1, 4, 12) and part.file != '';" +result=$(sqlite3 -header -line "$db" "$query") +echo "${result:11} files missing deep analyzation info." | tee -a $logfile +exit