#!/usr/bin/env python
# -*- coding: utf-8 -*-

fichier = '/var/lib/munin/tmp/batiments'

import sys,os

try :
    arg = sys.argv[1]
except :
    arg = ''

if arg == "config" :
    print 'host_name adherents'
    print 'graph_title Adherents par batiment'
    print "graph_args --base 1000 --lower-limit 0"
    print "graph_vlabel nb d'adhérents"
    print 'bata.label Bâtiment A'
    print 'bata.draw AREA'
    print 'batb.label Bâtiment B'
    print 'batb.draw STACK'
    print 'batc.label Bâtiment C'
    print 'batc.draw STACK'
    print 'batg.label Bâtiment G'
    print 'batg.draw STACK'
    print 'bath.label Bâtiment H'
    print 'bath.draw STACK'
    print 'bati.label Bâtiment I'
    print 'bati.draw STACK'
    print 'batj.label Bâtiment J'
    print 'batj.draw STACK'
    print 'batm.label Bâtiment M'
    print 'batm.draw STACK'
    print 'bato.label Bâtiment O'
    print 'batm.draw STACK'
    print 'batp.label PdJ'
    print 'batp.draw STACK'
    print 'wifi.label Wifi'
    print 'wifi.draw STACK'
    print 'total.label Total'
elif arg == "fichier" :

    file = open('%s.next' % fichier,'w')

    sys.path.append('/usr/scripts/gestion')
    from ldap_crans import crans_ldap
    db = crans_ldap()
    def count(filtre) :
        data = db.search(filtre)
        return len(data['adherent']) + len(data['club'])

    def count_adh():
        data = db.get_adherents()
        return len(data['adherent']) + len(data['club'])

    total = 0
    for bat in 'abcghijmpo' :
        nb = count_adh()
        total += nb
        file.write("bat%s.value %d\n" % (bat, nb))

    nb = count_adh()
    total += nb
    file.write("wifi.value %d\n" % nb)

    file.write("total.value %d\n" % total)

    file.close()
    os.system('mv %s.next %s' % (fichier, fichier))
else :
    os.system('cat %s' % fichier)
