switchs2.py: ascii-art d'une prise
This commit is contained in:
parent
9f03f41d6a
commit
d8b258393e
1 changed files with 73 additions and 0 deletions
|
@ -39,6 +39,11 @@ GIGABIT_MODELS = ['J9021A', 'J9145A']
|
|||
MIB_PRISE_VLAN = 'SNMPv2-SMI::enterprises.11.2.14.11.5.1.7.1.15.3.1.1'
|
||||
MIB_PRISE_MAC = 'SNMPv2-SMI::enterprises.11.2.14.11.5.1.9.4.2'
|
||||
|
||||
# Blocs verticaux pour ascii art des prises d'un switch
|
||||
START_BLOCK = [u'', u'┌', u'│', u'│', u'├', u'│', u'│', u'└', u'']
|
||||
MIDDLE_BLOCK = [u'', u'┬', u'│', u'│', u'┼', u'│', u'│', u'┴', u'']
|
||||
END_BLOCK = [u'', u'\u2510', u'│', u'│', u'\u2524', u'│', u'│', u'\u2518', u'']
|
||||
|
||||
ldap = make_ldap_conn()
|
||||
|
||||
# états possibles
|
||||
|
@ -113,6 +118,16 @@ class Port(object):
|
|||
for b in self.bornes))
|
||||
return ",".join(labels) or "Inconnu"
|
||||
|
||||
def brief(self):
|
||||
"""Description brève de la prise"""
|
||||
if self.uplink:
|
||||
return unicode(self.uplink).replace(u'uplink->', u'')
|
||||
else:
|
||||
labels = self.servers + self.bornes
|
||||
labels = map(lambda s: s['host'][0].value.split('.', 1)[0], labels)
|
||||
labels += map(unicode, self.chambres)
|
||||
return u",".join(labels)
|
||||
|
||||
def __int__(self):
|
||||
return self.num
|
||||
|
||||
|
@ -372,6 +387,60 @@ def fix_prise(machine, prise):
|
|||
machine.save()
|
||||
print("Done !")
|
||||
|
||||
def format_prises_group(data, first, last):
|
||||
"""Affiche sous forme d'un groupe de prise, en ascii-art, les noms des
|
||||
prises. Entre first et last"""
|
||||
first = (first-1)/2*2+1
|
||||
last = (-last/2)*-2
|
||||
|
||||
def align5(txt):
|
||||
"""Aligne le texte en limitant à 5 char"""
|
||||
if len(txt) > 5:
|
||||
return txt[0:4] + u'*'
|
||||
return txt + (5-len(txt))*u' '
|
||||
|
||||
def align2x5(txt):
|
||||
"""Aligne le texte sur deux lignes de 5 char"""
|
||||
return (align5(txt[0:5]), align5(txt[5:]))
|
||||
|
||||
lines = list(START_BLOCK)
|
||||
def fill_prise(prise, i_info, i_prise):
|
||||
"""Remplis le contenu d'une prise, sur deux lignes"""
|
||||
if prise in data:
|
||||
txt = data[prise].brief()
|
||||
else:
|
||||
txt = u""
|
||||
(txt1, txt2) = align2x5(txt)
|
||||
lines[i_info] += txt1
|
||||
lines[i_info+1] += txt2
|
||||
lines[i_prise] += u" " + align5(u"%d" % prise)
|
||||
|
||||
sep = MIDDLE_BLOCK
|
||||
for prise in xrange(first, last, 2):
|
||||
for li in [1, 4, 7]:
|
||||
lines[li] += u'─'*5
|
||||
fill_prise(prise, 2, 0)
|
||||
fill_prise(prise+1, -4, -1)
|
||||
if prise == last -1:
|
||||
sep = END_BLOCK
|
||||
for line_i in xrange(1, 8):
|
||||
lines[line_i] += sep[line_i]
|
||||
|
||||
return lines
|
||||
|
||||
def pretty_print(hostname):
|
||||
"""Affiche joliement le plan de connexion d'un switch"""
|
||||
bat, sw_num = get_bat_num(hostname)
|
||||
switch = ldap.search(u'host=bat%s-%d.adm.crans.org' % (bat, sw_num))[0]
|
||||
|
||||
port_dict = get_port_dict(switch)
|
||||
total = max(port_dict.keys())
|
||||
|
||||
first = 1
|
||||
while first < total:
|
||||
for line in format_prises_group(port_dict, first, min(first+11, total)):
|
||||
print(line)
|
||||
first += 12
|
||||
|
||||
def get_bat_num(hostname):
|
||||
"""Renvoie un tuple (bat, num) où bat est la lettre du bâtiment et
|
||||
|
@ -487,11 +556,15 @@ if __name__ == "__main__":
|
|||
parser.add_argument('-c', '--check', action='store_true', default=False,
|
||||
help="Vérifie la conf par rapport aux macs et vlans effectivement" +\
|
||||
"présents sur le switch")
|
||||
parser.add_argument('--pretty', action='store_true', default=False,
|
||||
help="Affiche un tableau ascii du plan de connexion du switch")
|
||||
|
||||
options = parser.parse_args(sys.argv[1:])
|
||||
|
||||
if options.check:
|
||||
check_conf_ldap(options.hostname)
|
||||
elif options.pretty:
|
||||
pretty_print(options.hostname)
|
||||
else:
|
||||
print(conf_switch(options.hostname))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue