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
@thejohnlewis
Copy link

thejohnlewis commented Mar 3, 2020

Hi, nice job on the munin plugin for plex. I have 3 plex server in my network and wanted to have one plugin that would work for all plex servers. I found a solution for remote Munin/Plex monitoring. Plex has a transcoder API that not only shows the transcoder but the type. I've only seen 2 types so far; Copy and Playback. Below is the complete code. I added the second transcoder type for Munin. The following graph shows the results - Munin produces a graph for each server.

image

The completed code for the Plex plugin below:

#!/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
import sys
import re


name = sys.argv[0]
addr = name.partition("_")[2]
# Show Config
if len(sys.argv) > 1 and sys.argv[1] == 'config':
	print 'graph_title Plex Streams for ' + addr
	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://' + addr + ':32400/status/sessions/';
urlt = 'http://' + addr + ':32400/transcode/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
# Get transcoding sessiong for playback and copy
file = urllib2.urlopen(urlt)
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 'playing.value', playingNum
print 'transcoding.value', transcodeNum
print 'transcopy.value', transcopyNum

The plugin name should be like "plex_". When creating the link to the new plugin add the IP address after the underscore. For example on ubuntu the main program would go in /usr/share/munin/plugins/plex_ and the linked file(s) would go in /etc/munin/plugins/. Create the links as follows:

ln -s /usr/share/munin/plugins/plex_ /etc/munin/plugins/plex_192.168.1.20

Much thanks to Merten Peetz for the original code.

@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