From 6a51e0c52099bd29a5da6389ec99b57cfe32d405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Elliott=20B=C3=A9cue?= Date: Sun, 27 Sep 2015 14:00:01 +0200 Subject: [PATCH] Patch pour batb-5.crans.org --- surveillance/mac_prises/mac_prise.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/surveillance/mac_prises/mac_prise.py b/surveillance/mac_prises/mac_prise.py index 0da02adf..962b0108 100755 --- a/surveillance/mac_prises/mac_prise.py +++ b/surveillance/mac_prises/mac_prise.py @@ -9,9 +9,12 @@ # celle-ci, démerdez-vous avec un des responsables de # l'association pour les fioritures. -import os, sys +import os +import sys import sys import re +import socket + from commands import getstatusoutput # 1 avril 2012 - PEB : @@ -21,6 +24,13 @@ from commands import getstatusoutput # (plante lamentablement quand j'essaye avec mon compte sur vo, sous # ipython. Mais si je sudo ipython, ça marche... +def host_exists(hostname): + """Vérifie si hostname résout bien, sinon retourne false""" + try: + socket.gethostbyname(hostname) + except socket.gaierror: + return False + def liste_chambres_macs(switch, annuaire): u''' Fonction générant un dictionnaire (macs) contenant pour chaque prise une @@ -29,11 +39,15 @@ def liste_chambres_macs(switch, annuaire): multiples connexions sql pour rien. ''' 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: return {} + if not host_exists(switch): + switch = switch.replace('adm.crans.org', 'crans.org') + if not host_exists(switch): + return {} data = walk(switch, 'STATISTICS-MIB::hpSwitchPortFdbAddress') liste_chbres = [] @@ -84,7 +98,6 @@ def walk(host, oid): 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]) - if not result.has_key(port): result[port] = [mac] else: @@ -92,7 +105,7 @@ def walk(host, oid): except: print "Ligne moisie : %s de l'hôte : %s" % (ligne, host) return result - + def __exec(cmd): u'''