57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Plugin pour visualiser l'utilisation des plages ip
|
|
|
|
fichier = '/var/lib/munin/tmp/stats-ip_'
|
|
|
|
import sys, os, string
|
|
sys.path.append('/usr/scripts/gestion')
|
|
|
|
# On prend l'argument pour définir la plage d'ip à analyser
|
|
SUBNET = sys.argv[0].split('_')[1]
|
|
|
|
try :
|
|
arg = sys.argv[1]
|
|
except :
|
|
arg = ''
|
|
|
|
if arg == "config" :
|
|
if len(SUBNET) == 1 : NOM = "Bâtiment %s" % string.upper(SUBNET)
|
|
else: NOM = SUBNET
|
|
|
|
print 'host_name adresses-ip'
|
|
print 'graph_title Statistiques adresses IP - %s' % NOM
|
|
print 'graph_args --base 1000 --lower-limit 0'
|
|
print 'graph_category network'
|
|
print "graph_vlabel nb d'ips"
|
|
print "utilisees.label IP utilisées"
|
|
print "utilisees.draw AREA"
|
|
print "total.label Total disponibles"
|
|
|
|
elif arg == "fichier" :
|
|
|
|
from ldap_crans import crans_ldap
|
|
from config import NETs
|
|
from iptools import AddrInNet
|
|
|
|
ips = [ x.ip() for x in crans_ldap().search('ip=*')['machine'] ]
|
|
|
|
file = open('%s%s.next' % (fichier, SUBNET), 'w')
|
|
|
|
total = 0
|
|
for net in NETs[SUBNET]:
|
|
total += 2 ** ( 32 - int( net.split('/')[1] ) )
|
|
# on ne prend pas en compte les adresses .0 et .255
|
|
total = total - int(total/128)
|
|
|
|
utilisees = len( [ ip for ip in ips if AddrInNet( ip, NETs[SUBNET] ) ] )
|
|
|
|
file.write("utilisees.value %d\n" % utilisees)
|
|
file.write("total.value %s\n" % total)
|
|
file.close()
|
|
|
|
os.system('mv %s%s.next %s%s' % (fichier, SUBNET, fichier, SUBNET))
|
|
|
|
else :
|
|
os.system('cat %s%s' % (fichier, SUBNET))
|