From c3e28ac9c1cc1322b0f6fae77558e048a0d28877 Mon Sep 17 00:00:00 2001 From: Daniel STAN Date: Tue, 7 May 2013 21:02:32 +0200 Subject: [PATCH] [whokfet] ajout d'un plugin munin --- gestion/tools/__init__.py | 0 gestion/tools/whokfet.py | 46 ++++++++++++++++++++++++++-------- munin/scripts/hosts_plugins.py | 1 + munin/whokfet | 16 ++++++++++++ 4 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 gestion/tools/__init__.py create mode 100755 munin/whokfet diff --git a/gestion/tools/__init__.py b/gestion/tools/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/gestion/tools/whokfet.py b/gestion/tools/whokfet.py index 4c0a697c..8b5a7517 100755 --- a/gestion/tools/whokfet.py +++ b/gestion/tools/whokfet.py @@ -27,12 +27,19 @@ def get_wifi_connected_client(host): f.close() return [ mac.firstChild.nodeValue for mac in doc.getElementsByTagName('mac') ] +STATE_DESCR = { + 'ma': ('machines de membres actifs', 0x00ff00), + 'crans': ('machines du crans', 0x0000ff), + 'adh': ('autres machines appartenant aux autres adhérents', 0xe5ff00), + 'unknown_macs': ('machines inconnues de la base', 0xff0000), +} def get_state(): sw = hpswitch('backbone.adm.crans.org') db = crans_ldap() - res = {'machines': [], - 'machines_crans': [], - 'unknown': [], + res = {'ma': [], + 'crans': [], + 'adh': [], + 'unknown_macs': [], 'ttyfound': 0, } try: @@ -45,27 +52,44 @@ def get_state(): fm = db.search("mac=%s" % mac) if fm['machine']: m = fm['machine'][0] - if not fm['machineCrans'] and hasattr(m.proprietaire(),'droits') and m.proprietaire().droits(): - # On filtre ceux qui ont des droits et qui sont pas des machines crans - res['machines'].append(m) + if fm['machineCrans']: + key = 'crans' + elif hasattr(m.proprietaire(),'droits') and m.proprietaire().droits(): + key = 'ma' + else: + key = 'adh' + res[key].append(m) else: - res['unknown'].append(mac) + res['unknown_macs'].append(mac) return res def summary(current): u"""Réalise un joli aperçu de l'état donné en paramètre.""" - if current['machines']: + if current['ma']: cprint('---=== Machines des membres actifs ===---', 'bleu') - aff(current['machines']) + aff(current['ma']) cprint("---=== Il y a du monde à la Kfet ! ===---", 'vert') else: cprint("---=== Il semble n'y avoir personne à la Kfet ... ===---", 'rouge') - for mac in current['unknown']: + for mac in current['unknown_macs']: cprint("Machine inconnue: %s" % mac, 'rouge') +def munin_config(): + """Donne la configuration du graphe munin""" + print """graph_title Membres actifs à la kfet +graph_vlabel N +graph_category environnement""" + for (name,(descr,color)) in STATE_DESCR.iteritems(): + print """%(name)s.label %(descr)s +%(name)s AREASTACK +%(name)s.colour %(color)06X""" % {'name': name, 'descr': descr, 'color': color} + # Dans le doute, n'affichons pas les adhérents + print "adh.graph no" + def munin(current): """S'occupe de l'affichage pour munin. TODO: l'écrire.""" - pass + for name in STATE_DESCR.iterkeys(): + print """%(name)s.value %(value)s""" % {'name': name, 'value': len(current[name])} if __name__ == '__main__': state = get_state() diff --git a/munin/scripts/hosts_plugins.py b/munin/scripts/hosts_plugins.py index 7f593e6c..2591f872 100755 --- a/munin/scripts/hosts_plugins.py +++ b/munin/scripts/hosts_plugins.py @@ -37,6 +37,7 @@ hosts_plugins = { "webalizer_intranet": "webalizer_", "webalizer_perso": "webalizer_", "who": "who", + "whokfet": "whokfet", }, "vert": { "slapd_bdb_cache_pages": "slapd_bdb_cache_", diff --git a/munin/whokfet b/munin/whokfet new file mode 100755 index 00000000..ebcc9096 --- /dev/null +++ b/munin/whokfet @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""Graphe des membres actifs présents à la Kfet""" + +import sys +sys.path.append('/usr/scripts') +from gestion.tools import whokfet + +action = ''.join(sys.argv[1:2]) +if action == 'autoconfig': + print "yes" +elif action == 'config': + whokfet.munin_config() +else: + whokfet.munin(whokfet.get_state())