Advertisement
JohnnyGrey86

Plex_DRI_Check

Apr 15th, 2020
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.67 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Update 4182020: Changed all console outputs to instead write to a logfile. The logfile will
  4. # always be in the scripts directory, regardless of where script is executed from.
  5.  
  6. # This is a script that checks for the /dev/dri device existing inside the Plex jail.
  7. # If it does not exist, it kills the jail, restarts devfs service, and restarts the jail.
  8. # If there are open streams, the script waits 5 minutes and checks again. After checking 12 times,
  9. # the script will exit. When there are 0 streams open, the script continues with
  10. # the check.
  11.  
  12. # To email and wipe the logfile, use this as a cron task:
  13. # mail -s "Plex DRI Check Log" EmailAddress@here.com < /path/to/log/plex_dri_check.log; echo "" > /path/to/log/plex_dri_check.log
  14.  
  15. # Specify Plex jail name, IP, port, auth token, and custom ruleset number:
  16. PlexName="plex"
  17. PlexIP="192.168.1.100"
  18. PlexPort="32400"
  19. Token="replace_this_with_token"
  20. RuleNum="110"
  21.  
  22. TimeStamp()
  23. {
  24.         date +"%Y-%m-%d %T:"
  25. }
  26.  
  27. TimeStampPre() {
  28.     while IFS= read -r line; do
  29.         printf '%s %s\n' "$(date +"%Y-%m-%d %T:")" "$line";
  30.     done
  31. }
  32.  
  33. LogFile="${0%/*}/plex_dri_check.log"
  34. RetryCount=0
  35. StreamCount=$(curl "http://$PlexIP:$PlexPort/status/sessions?X-Plex-Token=$Token" --stderr - \
  36.         | grep 'MediaContainer size=' | grep -o '[0-9]\+')
  37.  
  38. while [ $StreamCount -gt 0 ]
  39. do
  40.         echo "$(TimeStamp) There are currently $StreamCount open stream(s). Sleeping for 5m, then checking again." >> "$LogFile"
  41.         RetryCount=$((RetryCount+1))
  42.         if [ $RetryCount = "12" ]
  43.         then
  44.                 echo "$(TimeStamp) Checked $RetryCount times. Stream(s) still open. Exiting." >> "$LogFile"
  45.                 exit 0
  46.         fi
  47.         sleep 300
  48.         StreamCount=$(curl "http://$PlexIP:$PlexPort/status/sessions?X-Plex-Token=$Token" --stderr - \
  49.         | grep 'MediaContainer size=' | grep -o '[0-9]\+')
  50. done
  51.  
  52. if iocage exec $PlexName ls /dev/ | grep -c "dri" > /dev/null
  53. then
  54.         echo "$(TimeStamp) DRI pass-through still exists. Exiting." >> "$LogFile"
  55. else
  56.         echo "$(TimeStamp) DRI pass-through no longer exists. Killing Plex, restarting devfs, restarting Plex." >> "$LogFile"
  57.         iocage stop $PlexName | TimeStampPre >> "$LogFile"
  58.         sleep 3
  59.         service devfs restart | TimeStampPre >> "$LogFile"
  60.         sleep 3
  61.         iocage start $PlexName | TimeStampPre >> "$LogFile"
  62.         if iocage get all $PlexName | grep -c "ruleset:$RuleNum" > /dev/null
  63.         then
  64.                 echo "$(TimeStamp) devfs_ruleset:$RuleNum successfully loaded." >> "$LogFile"
  65.         else
  66.                 echo "$(TimeStamp) CRITICAL: devfs_ruleset:$RuleNum failed to load!" >> "$LogFile"
  67.         fi
  68. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement