root / plugins / streaming / icecast2_ @ e5ce7492
Historique | Voir | Annoter | Télécharger (5,51 ko)
| 1 |
#! /usr/bin/python |
|---|---|
| 2 |
# -*- coding: iso-8859-1 -*- |
| 3 |
|
| 4 |
# Hostname of Icecast server |
| 5 |
# Just canonical name, no http:// nor ending / |
| 6 |
host = "foo.bar.com" |
| 7 |
username = "admin" |
| 8 |
# Password for admin access to Icecast2 server to fetch statistics |
| 9 |
password = "" |
| 10 |
realm = "Icecast2 Server" |
| 11 |
|
| 12 |
# Bitrates the MP3 stream is served with |
| 13 |
mp3bitrates = [56, 128] |
| 14 |
# Bitrates the Ogg Stream is served with |
| 15 |
oggbitrates = [56, 128, 172] |
| 16 |
|
| 17 |
# This plugin shows the statistics of a specific subset of sources connected to an Icecast2 server. |
| 18 |
# Place the file in /usr/share/munin/plugins and then symlink to it from |
| 19 |
# /etc/munin/plugins |
| 20 |
# The name icecast2_total will show total number of listeners on server, as |
| 21 |
# well the total number of listeners for any configured stream. |
| 22 |
# For each stream with multiple bitrates, create one |
| 23 |
# icecast2_streamname |
| 24 |
# If the name contains a "-" exchange it with a "_", and the script will change it back for you. This is to satisfy internal requirements of Munin. |
| 25 |
# For each streamname, the plugin will check for the configured bitrates |
| 26 |
# Expecting the mountpoints to be on the form of |
| 27 |
# /streamname_<bitrate> for mp3 |
| 28 |
# /streamname_<bitrate>.ogg for Ogg/Vorbis |
| 29 |
|
| 30 |
import urllib2, os.path, time, sys |
| 31 |
from xml.dom import minidom |
| 32 |
|
| 33 |
def hent_XML(): |
| 34 |
auth_handler = urllib2.HTTPBasicAuthHandler() |
| 35 |
auth_handler.add_password(realm, host, username, password) |
| 36 |
opener = urllib2.build_opener(auth_handler) |
| 37 |
urllib2.install_opener(opener) |
| 38 |
|
| 39 |
xmlweb = urllib2.urlopen("http://%s/admin/stats" % host)
|
| 40 |
xml = xmlweb.read() |
| 41 |
xmlweb.close() |
| 42 |
|
| 43 |
# Parser oversikt |
| 44 |
|
| 45 |
xmldoc = minidom.parseString(xml) |
| 46 |
xmldoc = xmldoc.firstChild |
| 47 |
|
| 48 |
#Totalt antall lyttere |
| 49 |
total_lyttere = xmldoc.getElementsByTagName("clients")[0].firstChild.nodeValue
|
| 50 |
#Totalt antall kilder |
| 51 |
total_kilder = xmldoc.getElementsByTagName("sources")[0].firstChild.nodeValue
|
| 52 |
#Status for enkelt str?m |
| 53 |
sources = xmldoc.getElementsByTagName("source")
|
| 54 |
sourcelist = {}
|
| 55 |
for source in sources: |
| 56 |
mount = source.getAttribute("mount")
|
| 57 |
listeners = source.getElementsByTagName("listeners")[0].firstChild.nodeValue
|
| 58 |
name = source.getElementsByTagName("server_name")[0].firstChild.nodeValue
|
| 59 |
mount = mount.replace("-", "_")
|
| 60 |
sourcelist[mount[1:]] = (listeners, name) |
| 61 |
|
| 62 |
sourcename = sys.argv[0].split("/")[-1][len("icecast2_"):]
|
| 63 |
if len(sys.argv) == 1: |
| 64 |
sys.argv.append("")
|
| 65 |
if sys.argv[1] == "autoconf": |
| 66 |
print "yes" |
| 67 |
elif sys.argv[1] == "config": |
| 68 |
if sourcename == "total": |
| 69 |
print "graph_title Totalt antall lyttere" |
| 70 |
print "graph_vlabel lyttere" |
| 71 |
print "graph_category Icecast" |
| 72 |
print "totallyttere.label Totalt antall lyttere" |
| 73 |
print "totalkilder.label Totalt antall kilder" |
| 74 |
chanlist = {}
|
| 75 |
for a, b, filelist in os.walk("/etc/munin/plugins"):
|
| 76 |
for file in filelist: |
| 77 |
if file.find("icecast2_") != -1:
|
| 78 |
channelname = file[len("icecast2_"):]
|
| 79 |
if channelname != "total" and chanlist.has_key(channelname) != 1: |
| 80 |
chanlist[channelname] = 0 |
| 81 |
chanlist = chanlist.keys() |
| 82 |
chanlist.sort() |
| 83 |
for chan in chanlist: |
| 84 |
graphtitle = "" |
| 85 |
for key in sourcelist.keys(): |
| 86 |
if key.find(chan) != -1: |
| 87 |
l, graphtitle = sourcelist[key] |
| 88 |
break |
| 89 |
if graphtitle == "": |
| 90 |
graphtitle = chan |
| 91 |
print "%s.label %s" % (chan, graphtitle) |
| 92 |
|
| 93 |
else: |
| 94 |
sumstring = "" |
| 95 |
graphtitle = "" |
| 96 |
for key in sourcelist.keys(): |
| 97 |
if key.find(sourcename) != -1: |
| 98 |
l, graphtitle = sourcelist[key] |
| 99 |
break |
| 100 |
if graphtitle == "": |
| 101 |
graphtitle = sourcename |
| 102 |
print "graph_title %s" % graphtitle |
| 103 |
print "graph_vlabel lyttere" |
| 104 |
print "graph_category Icecast" |
| 105 |
for bitrate in mp3bitrates: |
| 106 |
print "%s_%s.label %s-%s" % (sourcename, bitrate, "/" + sourcename.replace("_", "-"), bitrate)
|
| 107 |
sumstring += "%s_%s " % (sourcename, bitrate) |
| 108 |
print "%s_%s.critical -0.5:" % (sourcename, bitrate) |
| 109 |
for bitrate in oggbitrates: |
| 110 |
print "%s_%s_ogg.label %s-%s.ogg" % (sourcename, bitrate, "/" + sourcename.replace("_", "-"), bitrate)
|
| 111 |
print "%s_%s_ogg.critical -0.5:" % (sourcename, bitrate) |
| 112 |
sumstring += "%s_%s_ogg " % (sourcename, bitrate) |
| 113 |
print "%slyttere.label Totalt antall lyttere" % sourcename |
| 114 |
print "%slyttere.sum %s" % (sourcename, sumstring) |
| 115 |
elif sys.argv[1] != "config": |
| 116 |
if sourcename == "total": |
| 117 |
print "totallyttere.value %s" % total_lyttere |
| 118 |
print "totalkilder.value %s" % total_kilder |
| 119 |
statslist = {}
|
| 120 |
for a, b, filelist in os.walk("/etc/munin/plugins"):
|
| 121 |
for file in filelist: |
| 122 |
if file.find("icecast2_") != -1:
|
| 123 |
channelname = file[len("icecast2_"):]
|
| 124 |
if channelname != "total" and statslist.has_key(channelname) != 1: |
| 125 |
statslist[channelname] = 0 |
| 126 |
|
| 127 |
for source in sourcelist: |
| 128 |
listeners, name = sourcelist[source] |
| 129 |
if not statslist.has_key(source[:source.rfind("_")]):
|
| 130 |
statslist[source[:source.rfind("_")]] = 0
|
| 131 |
statslist[source[:source.rfind("_")]] += int(listeners)
|
| 132 |
for stat in statslist: |
| 133 |
print "%s.value %s" % (stat, statslist[stat]) |
| 134 |
else: |
| 135 |
for bitrate in mp3bitrates: |
| 136 |
if sourcelist.has_key("%s_%s" % (sourcename, bitrate)):
|
| 137 |
listeners = sourcelist["%s_%s" % (sourcename, bitrate)][0] |
| 138 |
print listeners |
| 139 |
else: |
| 140 |
listeners = -1 |
| 141 |
print "%s_%s.value %s" % (sourcename, bitrate, listeners) |
| 142 |
for bitrate in oggbitrates: |
| 143 |
if sourcelist.has_key("%s_%s.ogg" % (sourcename, bitrate)):
|
| 144 |
listeners = sourcelist["%s_%s.ogg" % (sourcename, bitrate)][0] |
| 145 |
else: |
| 146 |
listeners = -1 |
| 147 |
print "%s_%s_ogg.value %s" % (sourcename, bitrate, listeners) |
| 148 |
else: |
| 149 |
print sys.argv[1] |
| 150 |
|
| 151 |
|
| 152 |
if __name__ == "__main__": |
| 153 |
hent_XML() |
