#!/usr/bin/env python # -*- coding: utf-8 -*- # Gestion des prises dans les batiments # Pour le moment on ne compte pas les prises CRANS import sys,os,string sys.path.append('/usr/scripts/gestion') from annuaires_pg import reverse, chbre_prises, bat_switchs, all_switchs, uplink_prises from hptools import hpswitch from ldap_crans import crans_ldap # Le fichier de nombre d'adhérent par batiments stats_batiments = "/var/lib/munin/tmp/batiments" BAT = sys.argv[0].split('_')[1] # Pour les batiments non manageables on indique le nb de prises BAT_CHBRES = {} def switch_prise(bat, prise) : ''' Renvoie le switch et la prise associé au numéro de la prise ''' switch = "bat%s-%i" % (bat, prise/100) prise = prise % 100 return (switch, prise) multiprise = map(lambda x : len(x[1])>1 and x[0], reverse(BAT).items()) while False in multiprise : multiprise.remove(False) try : arg = sys.argv[1] except : arg = '' if arg == "config" : print 'host_name adherents' print 'graph_title Batiment %s' % string.upper(BAT) print 'graph_args --base 1000 --lower-limit 0' # print 'graph_order activ enabled fixes reste prises chbres' print "graph_vlabel nb de chambres" print 'enabled.label Prises activées' print 'enabled.draw AREA' print 'fixes.label Prises fixes' print 'fixes.draw STACK' print 'reste.label Prises inutilisées' print 'reste.draw STACK' print 'crans.label Prises crans' print 'crans.draw STACK' print 'activ.label Prises actives' print 'adherent.label Nb adhérents' print 'prises.label Nb de prises' print 'chbres.label Nb de chambres' if BAT in BAT_CHBRES : print 'connues.label Nb de chbres enreg.' # print 'reste.warning 5:' # print 'reste.critical 0:' else : PRISES_ENABLE = 0 for switch in all_switchs(BAT) : try: PRISES_ENABLE += hpswitch(switch).is_enable('all') except: PRISES_ENABLE += 0 # On élimine les prises "CRANS" stockés dans uplink_prises for prise in uplink_prises[BAT].keys() : switch, prise = switch_prise(BAT, prise) try: if hpswitch(switch).is_enable(prise) : PRISES_ENABLE -= 1 except: PRISES_ENABLE -= 0 print "enabled.value %d" % PRISES_ENABLE PRISES_ACTIVES = 0 for switch in all_switchs(BAT) : try: PRISES_ACTIVES += hpswitch(switch).is_up('all') except: PRISES_ACTIVES += 0 # On élimine les prises "CRANS" stockés dans uplink_prises for prise in uplink_prises[BAT].keys() : switch, prise = switch_prise(BAT, prise) try: if hpswitch(switch).is_up(prise) : PRISES_ACTIVES -= 1 except: PRISES_ACTIVES -= 0 if multiprise != [] : PRISES_ACTIVES -= len(multiprise) for prise in multiprise : PRISES_ACTIVES += len(reverse(BAT)[prise]) print "activ.value %d" % PRISES_ACTIVES PRISES_FIXES = 0 if multiprise != [] : PRISES_FIXES = - len(multiprise) for prise in multiprise : PRISES_FIXES += len(reverse(BAT)[prise]) print "fixes.value %d" % PRISES_FIXES PRISES_CRANS = len(uplink_prises[BAT].keys()) print "crans.value %d" % PRISES_CRANS NB_PRISES = 0 for switch in all_switchs(BAT) : try: NB_PRISES += hpswitch(switch).nb_prises() except: NB_PRISES += 0 NB_PRISES += PRISES_FIXES print "prises.value %d" % NB_PRISES print "reste.value %d" % int(NB_PRISES - PRISES_ENABLE - PRISES_FIXES - PRISES_CRANS) fichier = open(stats_batiments, 'r') #NB_ADHERENT = 0 # Plus génant qu'autre chose for line in fichier.readlines(): if line[3:4] == BAT and line[:3] == "bat" : NB_ADHERENT = line[11:].strip() print "adherent.value %s" % NB_ADHERENT if BAT in BAT_CHBRES.keys() : print "chbres.value %d" % BAT_CHBRES[BAT] print "connues.value %d" % len(chbre_prises(BAT).keys()) else : print "chbres.value %d" % len(chbre_prises(BAT).keys())