Support des fonctions de who2b dans whosthere avec comme arg 2b

This commit is contained in:
Gabriel Detraz 2015-08-17 19:28:33 +02:00
parent 686bc45ed5
commit 39409ec894

View file

@ -8,6 +8,8 @@ import sys
import collections import collections
import os import os
import xml.dom.minidom import xml.dom.minidom
import subprocess
from socket import gethostname
from lc_ldap import shortcuts from lc_ldap import shortcuts
@ -44,6 +46,8 @@ def pretty_name(item):
except KeyError: except KeyError:
nom = unicode(item['host'][0]) nom = unicode(item['host'][0])
v = nom v = nom
except TypeError:
v = item
v = v.replace('.wifi.crans.org', ' (WiFi)') v = v.replace('.wifi.crans.org', ' (WiFi)')
v = v.replace('.adm.crans.org', '') v = v.replace('.adm.crans.org', '')
v = v.replace('.crans.org', '') v = v.replace('.crans.org', '')
@ -97,8 +101,6 @@ class WhosThere(object):
"""Rempli à partir de la mac""" """Rempli à partir de la mac"""
fm = self.db.search(u"macAddress=%s" % mac) fm = self.db.search(u"macAddress=%s" % mac)
res = self._res res = self._res
if mac in self.expected:
return
if fm: if fm:
m = fm[0] m = fm[0]
proprio = m.proprio() proprio = m.proprio()
@ -122,6 +124,16 @@ class WhosThere(object):
else: else:
res['unknown_macs'].append(mac) res['unknown_macs'].append(mac)
def expected_machine(self):
"""Remplie la liste des machines qui devraient etre dans le local"""
current = self.query()
mach_manq = []
detected_mach = [machine['host'][0] for machine in current['crans'] + current['bde']]
for mach in self.expected:
if mach not in detected_mach:
mach_manq.append(mach)
return mach_manq
def populate_from_switch(self, host, port): def populate_from_switch(self, host, port):
"""Rempli les macs à partir de la prise d'un switch""" """Rempli les macs à partir de la prise d'un switch"""
sw = hpswitch(host) sw = hpswitch(host)
@ -142,6 +154,13 @@ class WhosThere(object):
for mac in doc.getElementsByTagName('mac'): for mac in doc.getElementsByTagName('mac'):
self.populate_from_mac(mac.firstChild.nodeValue) self.populate_from_mac(mac.firstChild.nodeValue)
def populate_from_tty(self):
res = self._res
p = subprocess.check_output(["/usr/bin/w"])
for line in p.split("\n"):
if "gdm-session" in line:
res['ttyfound'].append(line)
def do_scan(self): def do_scan(self):
"""Fonction à surcharger pour remplir la liste de personnes présentes. """Fonction à surcharger pour remplir la liste de personnes présentes.
La fonction pourra faire appel à populate_from_*""" La fonction pourra faire appel à populate_from_*"""
@ -157,7 +176,7 @@ class WhosThere(object):
'adh': [], 'adh': [],
'bde': [], 'bde': [],
'unknown_macs': [], 'unknown_macs': [],
'ttyfound': 0, 'ttyfound': [],
} }
self.db = shortcuts.lc_ldap_readonly() self.db = shortcuts.lc_ldap_readonly()
self.do_scan() self.do_scan()
@ -169,11 +188,18 @@ class WhosThere(object):
if current['ma']: if current['ma']:
cprint('---=== Machines des membres actifs ===---', 'bleu') cprint('---=== Machines des membres actifs ===---', 'bleu')
show_liste_by_prop(current['ma']) show_liste_by_prop(current['ma'])
if hasattr(self, "tty_server"):
if gethostname()!=self.tty_server:
cprint(u'---=== Il faut executer ce script sur %s pour avoir les users logués ! ===---' % self.tty_server, 'rouge')
else:
if current['ttyfound']:
cprint('---=== W(ho) sur %s ===---' % self.tty_server, 'bleu')
for user in current["ttyfound"]:
cprint(user, 'jaune')
if current['ma'] or current['ttyfound']:
cprint("---=== Il y a du monde ===---", 'vert') cprint("---=== Il y a du monde ===---", 'vert')
else: else:
cprint("---=== Il semble n'y avoir personne ... ===---", 'rouge') cprint("---=== Il semble n'y avoir personne ... ===---", 'rouge')
for mac in current['unknown_macs']:
cprint("Machine inconnue: %s" % mac, 'rouge')
if current['crans']: if current['crans']:
cprint("---=== Machines Cr@ns ===---", 'bleu') cprint("---=== Machines Cr@ns ===---", 'bleu')
show_liste(current['crans']) show_liste(current['crans'])
@ -183,6 +209,10 @@ class WhosThere(object):
if current['adh']: if current['adh']:
cprint("---=== Machines d'adhérents ===---", 'bleu') cprint("---=== Machines d'adhérents ===---", 'bleu')
show_liste_by_prop(current['adh']) show_liste_by_prop(current['adh'])
for mac in current['unknown_macs']:
cprint("Machine inconnue: %s" % mac, 'rouge')
for machine_manquante in self.expected_machine():
cprint("Machine %s manquante !" % pretty_name(machine_manquante), 'rouge')
def munin_config(self): def munin_config(self):
"""Donne la configuration du graphe munin""" """Donne la configuration du graphe munin"""
@ -230,7 +260,6 @@ class Who2B(WhosThere):
name = u"2B" name = u"2B"
expected = [ expected = [
'00:07:cb:b1:99:4e', # Freebox
'terminal.crans.org', 'terminal.crans.org',
'minigiga.adm.crans.org', 'minigiga.adm.crans.org',
'belides.wifi.crans.org', 'belides.wifi.crans.org',
@ -243,6 +272,8 @@ class Who2B(WhosThere):
# mais on cache quand-même les personnes connectées en WiFi # mais on cache quand-même les personnes connectées en WiFi
self.set_ignore_inactive(True, wifi_only=True) self.set_ignore_inactive(True, wifi_only=True)
self.populate_from_switch('backbone.adm.crans.org', 33) self.populate_from_switch('backbone.adm.crans.org', 33)
self.tty_server=u"vo"
self.populate_from_tty()
class WhoDAlembert(WhosThere): class WhoDAlembert(WhosThere):
name = u"D'Alembert (PR)" name = u"D'Alembert (PR)"