Si un serveur Shoutcast est installé sur la machine, il peut être intéressant de le monitorer.
Voici un script de monitoring pour Munin écrit en Python qui permet de :
- compter le nombre de clients connectés au flux
- comptabiliser le temps d’écoute et d’en réaliser une moyenne
Pour l’utiliser, il faut s’assurer que python 2.5 soit installé sur la machine :
- sudo apt-get install python2.5
On a décidé de déposer nos plugins personnalisés dans le répertoire /opt/share/munin/plugins
, on vérifie qu’il existe bien et on s’y déplace :
- #Créer le répertoire des plugins dans le cas ou il n'existe pas déjà
- sudo mkdir -p /opt/share/munin/plugins
- cd /opt/share/munin/plugins
On édite le fichier qui nous servira de plugin :
- sudo nano shoutcast_
On y colle le contenu suivant en modifiant les lignes :
- server : si le serveur a monitorer est sur une autre machine ou sur un port différent
- pw : le mot de passe d’admin du serveur pour avoir accès aux statistiques
- #!/usr/bin/env python2.5
- """
- Plugin to monitor shoutcast streaming servers.
- Author: kent1 <kent1@arscenic.info>
- Version: 2010091301
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- Usage:
- - Link or copy to /etc/munin/plugins
- - To enable listening duration,
- also link to shoutcast_duration respectively.
- Configuration:
- - Enter your server, user and pw below
- (The plugin does not need to run on
- the same host as your shoutcast server)
- - Optionally provide hosts to exclude.
- Possible TODOs:
- - use autoconf
- - use munin's configuration system
- - documentation
- """
- # CONFIGURATION
- server = "127.0.0.1:8000"
- user = "admin"
- pw = "password"
- # Exclude these hosts when calculating listening duration / listeners count
- # (we use this to prevent our aircheck recording system from appearing in the stats)
- #exclude = ("123.123.123.123",)
- exclude = ()
- # /CONFIGURATION
- from sys import argv, exit, stderr
- import urllib2
- from xml.etree.ElementTree import ElementTree
- from os.path import basename
- stats_url = "http://%s/admin.cgi?mode=viewxml" % server
- realm = "Shoutcast Server"
- auth_handler = urllib2.HTTPBasicAuthHandler()
- auth_handler.add_password(realm, server, user, pw)
- opener = urllib2.build_opener(auth_handler)
- opener.addheaders = [('User-agent', 'Mozilla/5.0')]
- f = opener.open(stats_url)
- tree = ElementTree()
- tree.parse(f)
- f.close()
- sources = []
- sources.append({"mount": tree.find("SERVERTITLE").text,
- "listeners": tree.find("CURRENTLISTENERS").text,
- "connected": tree.find("STREAMHITS").text})
- plugin_name = basename(argv[0])
- try:
- if argv[1] == "config":
- if plugin_name == "shoutcast_duration":
- print "graph_title Shoutcast client listening duration"
- print "graph_args --base 1000 -l 0"
- print "graph_scale no"
- print "graph_category Shoutcast"
- print "graph_vlabel minutes"
- print "avg.label average listening duration"
- print "mdn.label median listening duration"
- else:
- print "graph_title Shoutcast listeners count"
- print "graph_args --base 1000 -l 0"
- print "graph_scale no"
- print "graph_category Shoutcast"
- print "graph_vlabel listeners"
- is_first = True
- for s in sources:
- sname = s["mount"].strip("/").replace(".","_")
- print "%s.label source %s" % (sname, s["mount"])
- if is_first:
- print "%s.draw AREA" % sname
- is_first = False
- else:
- print "%s.draw STACK" % sname
- exit(0)
- except IndexError:
- pass
- durations = {}
- for s in sources:
- durations[s["mount"]] = []
- for l in tree.find("LISTENERS").getiterator("LISTENER"):
- if l.find("HOSTNAME").text not in exclude:
- durations[s["mount"]].append(int(l.find("CONNECTTIME").text))
- if plugin_name == "shoutcast_duration":
- if not durations:
- exit(0)
- alldurations = reduce(lambda x, y: x+y, durations.values())
- alldurations.sort()
- print "avg.value %s" % (sum(alldurations) / float(len(alldurations)) / 60.,)
- if len(alldurations) % 2:
- median = alldurations[len(alldurations) / 2] / 60.
- elif len(alldurations):
- median = (alldurations[len(alldurations) / 2 - 1] + alldurations[len(alldurations) / 2]) / 2. / 60.
- else:
- median = 0
- print "mdn.value %s" % median
- else:
- for s in sources:
- print "%s.value %s" % (s["mount"].strip("/").replace(".","_"), len(durations[s["mount"]]))
Ensuite on donne les droits d’exécution sur le fichier :
- sudo chmod +x shoutcast_
Le script permettant de monitorer deux choses différentes, on crée deux liens symboliques vers le répertoire des plugins de Munin :
- # Le compteur de clients connectés au flux
- sudo ln -s /opt/share/munin/plugins/shoutcast_ /etc/munin/plugins/shoutcast_counts
- # Le graphique donnant la durée des connections
- sudo ln -s /opt/share/munin/plugins/shoutcast_ /etc/munin/plugins/shoutcast_duration
Il est possible ensuite de tester le bon fonctionnement des modules en lançant les commandes :
- sudo munin-run shoutcast_counts
- sudo munin-run shoutcast_duration
Le résultat de ces commandes doivent donner les valeurs qui s’afficheront dans les graphiques.
Puis on redémarre le noeud :
- sudo /etc/init.d/munin-node restart
Attendez quelques minutes et vous devriez voir apparaitre ces nouveaux graphiques sur votre affichage web du serveur.