Skip to content

Instantly share code, notes, and snippets.

@MorbZ
Last active May 18, 2024 03:12
Show Gist options
  • Save MorbZ/e5d05856a6bdbcf4ca52c4cae21f2228 to your computer and use it in GitHub Desktop.
Save MorbZ/e5d05856a6bdbcf4ca52c4cae21f2228 to your computer and use it in GitHub Desktop.
Munin plugin for Plex: Shows playing streams and transcoding sessions
#!/usr/bin/env python
# If the server needs authentication add file /etc/munin/plugin-conf.d/plex:
# [plex]
# env.plex_token yourplextoken
import urllib2, sys, os
import xml.etree.ElementTree as ET
# Show Config
if len(sys.argv) > 1 and sys.argv[1] == 'config':
print 'graph_title Plex Streams'
print 'graph_vlabel Number of streams'
print 'graph_category plex'
print 'graph_scale no'
print 'playing.label Playing'
print 'playing.draw AREASTACK'
print 'transcoding.label Transcoding'
print 'transcoding.draw LINE2'
sys.exit(0)
# Show values
# Make URL
url = 'http://localhost:32400/status/sessions/';
token = os.getenv('plex_token');
if token is not None:
url += '?X-Plex-Token=' + token
# Get playing streams
file = urllib2.urlopen(url)
root = ET.ElementTree(file=file).getroot()
playingNum = 0
for video in root.iter('Video'):
for player in video.iter('Player'):
if player.get('state') == 'playing':
playingNum += 1
print 'playing.value', playingNum
# Get transcoding sessions
procs = os.popen('pgrep -l "Plex Transcoder"').read()
lines = len(procs.split('\n')) - 1
print 'transcoding.value', lines
@phipac
Copy link

phipac commented Feb 1, 2021

The following code prints the three values (including the transcopy.value) but only for one single server. However, it has been updated for Python3!

#!/usr/bin/env python3
# Updated for Python3 by phipac 20210131
# If the server needs authentication add file /etc/munin/plugin-conf.d/plex:
#       [plex]
#       env.plex_token yourplextoken

import urllib.request, urllib.error, urllib.parse, sys, os
import xml.etree.ElementTree as ET

# Show Config
if len(sys.argv) > 1 and sys.argv[1] == 'config':
        print('graph_title Plex Streams')
        print('graph_vlabel Number of streams')
        print('graph_category plex')
        print('graph_scale no')
        print('playing.label Playing')
        print('playing.draw AREASTACK')
        print('transcoding.label Transcoding')
        print('transcopy.label Transcopy')
        print('transcoding.draw LINE2')
        sys.exit(0)

# Show values
# Make URL
url = 'http://localhost:32400/status/sessions/';
token = os.getenv('plex_token');
if token is not None:
        url += '?X-Plex-Token=' + token

# Get playing streams
file = urllib.request.urlopen(url)
root = ET.ElementTree(file=file).getroot()
playingNum = 0
for video in root.iter('Video'):
        for player in video.iter('Player'):
                if player.get('state') == 'playing':
                        playingNum += 1
print('playing.value', playingNum)

# Get transcoding sessiong for playback and copy
file = urllib.request.urlopen(url)
root = ET.ElementTree(file=file).getroot()
# Get transcoding sessions
transcodeNum = 0
transcopyNum = 0
for video in root.iter('MediaContainer'):
        for TranscodeSession in video.iter('TranscodeSession'):
                if TranscodeSession.get('videoDecision') == 'transcode':
                        transcodeNum += 1
                if TranscodeSession.get('videoDecision') == 'copy':
                        transcopyNum += 1
print('transcoding.value', transcodeNum)
print('transcopy.value', transcopyNum)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment