On modifie un peu le plugin de dim pour handle les bonnes choses, on ajoute un tool python, et on met mac_prises dans le dépot.
Ignore-this: a97c9dd382aae257e5d64b3fc14d0469 darcs-hash:20121211191448-b6762-f8705ea02bdfe778cd81d98ee52f2fa2533d68e0.gz
This commit is contained in:
parent
fbc7f2c0f4
commit
dfcda07054
6 changed files with 852 additions and 7 deletions
100
surveillance/mac_prises/mac_prise.py
Executable file
100
surveillance/mac_prises/mac_prise.py
Executable file
|
@ -0,0 +1,100 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf8 -*-
|
||||
#
|
||||
#
|
||||
# PEB - 01/04/2012
|
||||
#
|
||||
|
||||
import os, sys, re
|
||||
from commands import getstatusoutput
|
||||
|
||||
sys.path.append('/usr/scripts/gestion')
|
||||
import annuaires_pg
|
||||
|
||||
# nécessite apparemment que l'objet conn soit bien créé lors de l'exec
|
||||
# de annuaires_pg, il faut être root (ou dans je ne sais quel groupe)
|
||||
# pour que l'authentification de l'user crans avec psycopg2 se fasse
|
||||
# (plante lamentablement quand j'essaye avec mon compte sur vo, sous
|
||||
# ipython. Mais si je sudo ipython, ça marche...
|
||||
|
||||
def is_really_crans(chbre):
|
||||
|
||||
def liste_prises_macs(switch):
|
||||
u'''
|
||||
Fonction générant un dictionnaire (macs) contenant pour chaque prise une
|
||||
liste des macs qui y sont actives.
|
||||
'''
|
||||
liste_bats = ['a', 'b', 'c', 'g', 'h', 'i', 'j', 'm', 'p']
|
||||
|
||||
split = switch.replace('.adm.crans.org', '').split('-')
|
||||
bat, num_switch = split[0][-1], int(split[1][0])
|
||||
if bat not in liste_bats:
|
||||
print 'Le bâtiment '+bat+' ne figure pas dans la liste des bâtiments.'
|
||||
data = walk(switch, 'STATISTICS-MIB::hpSwitchPortFdbAddress')
|
||||
|
||||
liste_chbres = []
|
||||
macs = {}
|
||||
for i in data:
|
||||
if i == '':
|
||||
continue
|
||||
else:
|
||||
port = int(i.replace('STATISTICS-MIB::hpSwitchPortFdbAddress.', '').split('.')[0])
|
||||
mac = data[i].replace(' ', '').lower().replace('"', '')
|
||||
if not re.match('([0-9a-f]{2}){6}', mac):
|
||||
mac = mac.encode('hex').lower()
|
||||
mac = "%s:%s:%s:%s:%s:%s" % (mac[0:2], mac[2:4], mac[4:6], mac[6:8], mac[8:10], mac[10:12])
|
||||
uplink = annuaires_pg.uplink_prises[bat]
|
||||
prise = num_switch*100+port
|
||||
if prise in uplink:
|
||||
continue
|
||||
|
||||
chbre = chbre_prises(bat, prise)
|
||||
|
||||
if chbre in liste_chbres:
|
||||
macs[chbre].append(mac+'\n')
|
||||
else:
|
||||
macs[chbre] = []
|
||||
macs[chbre].append(mac+'\n')
|
||||
liste_chbres.append(chbre)
|
||||
return macs
|
||||
|
||||
def walk(host, oid):
|
||||
u'''
|
||||
Hack sale remplaçant la fonction walk contenue dans hptools, qui
|
||||
splitte suivant des espaces, ce qui engendre des failles.
|
||||
Ici, snmpwalk -Ox (au lieu de -Oq) fait qu'on récupère bien des macs,
|
||||
et on splitte suivant le keyword Hex-STRING, qui n'est pas redondant, lui.
|
||||
'''
|
||||
received = __exec('snmpwalk -Ox -v 1 -c public %s %s' % (host, oid)).split('\n')
|
||||
result = {}
|
||||
for ligne in received:
|
||||
pport, pmac = ligne.split('Hex-STRING: ')
|
||||
result[pport] = pmac
|
||||
return result
|
||||
|
||||
|
||||
def __exec(cmd):
|
||||
u'''
|
||||
Hack sale pour pas loader inutilement hptools.py
|
||||
Exécute une commande et retourne son status et son output.
|
||||
'''
|
||||
status, response = getstatusoutput(cmd)
|
||||
if status:
|
||||
response = response.replace('snmpget: ','')
|
||||
print 'Erreur : '+response+' : '+cmd
|
||||
return response
|
||||
|
||||
if __name__ == '__main__':
|
||||
switchs = sys.argv[1:]
|
||||
for switch in switchs:
|
||||
macs = liste_prises_macs(switch)
|
||||
|
||||
split = switch.replace('.adm.crans.org', '').split('-')
|
||||
bat, num_switch = split[0][-1], int(split[1][0])
|
||||
|
||||
if not os.path.isdir("bat%s/%d"%(bat, num_switch)):
|
||||
os.makedirs("bat%s/%d"%(bat, num_switch))
|
||||
|
||||
for chbre in macs:
|
||||
with open('bat%s/%d/%s%03d.macs'%(bat, num_switch, bat, prise), 'w') as f:
|
||||
f.writelines(sorted(macs[prise]))
|
Loading…
Add table
Add a link
Reference in a new issue