petites modifications
darcs-hash:20060105184541-4ec08-eced1f790b9f0cc52e4707ae56db94490aef63bf.gz
This commit is contained in:
parent
30b2678a42
commit
072b4e7903
1 changed files with 136 additions and 64 deletions
|
@ -7,18 +7,28 @@ from pyPgSQL import PgSQL
|
||||||
sys.path.append('/usr/scripts/gestion/')
|
sys.path.append('/usr/scripts/gestion/')
|
||||||
from affich_tools import tableau_ng
|
from affich_tools import tableau_ng
|
||||||
|
|
||||||
def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, limit=10, heures=24) :
|
def stats (ip_crans=[], ip_ext=[], show=['ip_crans','ip_ext','port_crans','port_ext'], upload_mini=0, show_limit=10, begin_time=24, end_time=0):
|
||||||
"""
|
"""
|
||||||
Retourne une chaine de caratères formatée avec le tableau de statistiques
|
Retourne une chaine de caratères formatée avec le tableau de statistiques
|
||||||
d'upload de l'ip fourni
|
d'upload de l'ip fourni
|
||||||
|
|
||||||
ip_crans : ip de la machine
|
ip_crans : ips des machine
|
||||||
group : liste des champs à àfficher détailler
|
chaine de caratère si il a qu'une machine
|
||||||
parmi : ip_ext, port_crans, port_ext
|
liste si il y a plusieurs machines
|
||||||
|
view : liste des champs à afficher parmi :
|
||||||
|
ip_crans, ip_ext, port_crans, port_ext
|
||||||
upload_mini : n'affiche que les lignes avec plus d'upload que la valeur donnée
|
upload_mini : n'affiche que les lignes avec plus d'upload que la valeur donnée
|
||||||
limit : nombre max de lignes à afficher
|
show_limit : nombre max de lignes à afficher
|
||||||
|
begin : date de départ d'analyse (en heure avant maintenant)
|
||||||
|
end : date de fin d'analyse (en heure avant maintenant)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if type(ip_crans) == str:
|
||||||
|
ip_crans = [ip_crans]
|
||||||
|
|
||||||
|
if type(ip_ext) == str:
|
||||||
|
ip_ext = [ip_ext]
|
||||||
|
|
||||||
# variables pour la requete et l'affichage
|
# variables pour la requete et l'affichage
|
||||||
##########################################
|
##########################################
|
||||||
|
|
||||||
|
@ -28,27 +38,34 @@ def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, li
|
||||||
format = []
|
format = []
|
||||||
alignement = []
|
alignement = []
|
||||||
|
|
||||||
if 'ip_ext' in group :
|
if 'ip_crans' in show and len(ip_crans)!=1:
|
||||||
|
select.append('ip_crans')
|
||||||
|
largeur.append('10')
|
||||||
|
titre.append('machine crans')
|
||||||
|
format.append('s')
|
||||||
|
alignement.append('c')
|
||||||
|
|
||||||
|
if 'ip_ext' in show :
|
||||||
select.append('ip_ext')
|
select.append('ip_ext')
|
||||||
largeur.append('*')
|
largeur.append('*')
|
||||||
titre.append('machine ext')
|
titre.append('machine ext')
|
||||||
format.append('s')
|
format.append('s')
|
||||||
alignement.append('c')
|
alignement.append('c')
|
||||||
|
|
||||||
if 'port_ext' in group :
|
if 'port_ext' in show :
|
||||||
select.append('port_ext')
|
select.append('port_ext')
|
||||||
largeur.append(10)
|
largeur.append(10)
|
||||||
titre.append('port ext')
|
titre.append('port ext')
|
||||||
format.append('s')
|
format.append('s')
|
||||||
alignement.append('d')
|
alignement.append('d')
|
||||||
|
|
||||||
if 'port_crans' in group :
|
if 'port_crans' in show :
|
||||||
select.append('port_crans')
|
select.append('port_crans')
|
||||||
largeur.append(10)
|
largeur.append(10)
|
||||||
titre.append('port crans')
|
titre.append('port crans')
|
||||||
format.append('s')
|
format.append('s')
|
||||||
alignement.append('d')
|
alignement.append('d')
|
||||||
|
|
||||||
select += ['sum(download) as download','sum(upload) as upload']
|
select += ['sum(download) as download','sum(upload) as upload']
|
||||||
largeur += [10,10]
|
largeur += [10,10]
|
||||||
titre += ['download','upload']
|
titre += ['download','upload']
|
||||||
|
@ -57,7 +74,12 @@ def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, li
|
||||||
|
|
||||||
# requete dans la base
|
# requete dans la base
|
||||||
######################
|
######################
|
||||||
requete = "SELECT * from ( SELECT %s FROM upload WHERE ip_crans='%s' AND date > timestamp 'now' - interval '%d hours' GROUP BY %s ORDER BY upload DESC ) AS resultat_intemediaire WHERE upload>='%d' LIMIT %d;" % (','.join(select), ip_crans, heures, ','.join(group), upload_mini*1024*1024, limit)
|
ip_crans = ' OR '.join([ "ip_crans='%s'"%x for x in ip_crans ])
|
||||||
|
if not ip_crans: ip_crans='true'
|
||||||
|
ip_ext = ' OR '.join([ "ip_ext='%s'"%x for x in ip_ext ])
|
||||||
|
if not ip_ext: ip_ext='true'
|
||||||
|
|
||||||
|
requete = "SELECT * FROM ( SELECT %s FROM upload WHERE (%s) AND (%s) AND (date > timestamp 'now' - interval '%d hours') AND (date < timestamp 'now' - interval '%d hours') GROUP BY %s ORDER BY upload DESC ) AS resultat_intemediaire WHERE upload>='%d' LIMIT %d;" % (','.join(select), ip_crans, ip_ext, begin_time, end_time, ','.join(show), upload_mini*1024*1024, show_limit)
|
||||||
|
|
||||||
pgsql = PgSQL.connect(host='/var/run/postgresql', database='filtrage', user='crans')
|
pgsql = PgSQL.connect(host='/var/run/postgresql', database='filtrage', user='crans')
|
||||||
curseur = pgsql.cursor()
|
curseur = pgsql.cursor()
|
||||||
|
@ -75,7 +97,7 @@ def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, li
|
||||||
return ip
|
return ip
|
||||||
|
|
||||||
for champ in select :
|
for champ in select :
|
||||||
if champ == 'ip_ext' :
|
if champ in ['ip_ext','ip_crans']:
|
||||||
col = select.index(champ)
|
col = select.index(champ)
|
||||||
results = [ x[:col] + [nom_de_machine(x[col])] + x[col+1:] for x in results ]
|
results = [ x[:col] + [nom_de_machine(x[col])] + x[col+1:] for x in results ]
|
||||||
|
|
||||||
|
@ -84,16 +106,23 @@ def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, li
|
||||||
if __name__ == '__main__' :
|
if __name__ == '__main__' :
|
||||||
|
|
||||||
help = """Statistiques d'upload d'une machine du crans
|
help = """Statistiques d'upload d'une machine du crans
|
||||||
usage: analyse.py [options] machine
|
usage: analyse.py [options]
|
||||||
Option fait partie des options suivantes :
|
Option fait partie des options suivantes :
|
||||||
-l # : nombre de lignes à afficher
|
--ip-crans
|
||||||
par défaut : 10
|
ip de la machine crans
|
||||||
-g : champs à grouper (parmis ip_ext, port_crans, port_ext)
|
--ip-ext
|
||||||
par défaut : -p ip_ext -p port_crans -p port_ext
|
ip de la machine extérieure
|
||||||
-u # : ne prend quee les entrées ou l'upload est supérieur à #
|
--show
|
||||||
par défaut : 0
|
champs à afficher (parmi ip_crans, ip_ext, port_crans, port_ext)
|
||||||
-t # : nombre d'heures à analyser
|
--upload-mini
|
||||||
par défaut : 24
|
upload mini des lignes à afficher
|
||||||
|
--show-limit
|
||||||
|
nombre maximum de lignes à afficher
|
||||||
|
--begin-time
|
||||||
|
heure de départ de l'analyse (en heure depuis maintenant)
|
||||||
|
--end-time
|
||||||
|
heure de fin de l'analyse (en heure depuis maintenant)
|
||||||
|
|
||||||
Exemples :
|
Exemples :
|
||||||
sudo /usr/scripts/surveillance/analyse.py bilou.crans.org"""
|
sudo /usr/scripts/surveillance/analyse.py bilou.crans.org"""
|
||||||
|
|
||||||
|
@ -102,41 +131,87 @@ Exemples :
|
||||||
|
|
||||||
# aide
|
# aide
|
||||||
######
|
######
|
||||||
if '-h' in sys.argv :
|
if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv)==1:
|
||||||
print help
|
print help
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# parsage des arguments
|
# parsage des arguments
|
||||||
#######################
|
#######################
|
||||||
try :
|
try :
|
||||||
opts, args = getopt.getopt(sys.argv[1:],'g:l:u:t:')
|
opts, args = getopt.getopt(sys.argv[1:],'',['ip-crans=','ip-ext=','show=','upload-mini=','begin-time=','end-time=','show-limit='])
|
||||||
except getopt.GetoptError,message :
|
except getopt.GetoptError,message :
|
||||||
print message
|
print help
|
||||||
sys.exit(4)
|
sys.exit(4)
|
||||||
|
|
||||||
|
print "Statistiques d'upload"
|
||||||
|
|
||||||
# recherche de la machine crans
|
# recherche de la machine crans
|
||||||
###############################
|
###############################
|
||||||
if len(args) == 0 :
|
ip_crans = []
|
||||||
print 'Il faut spécifier une adresse'
|
ip_crans_nom = []
|
||||||
sys.exit(1)
|
for key,value in opts :
|
||||||
elif len(args) >= 2 :
|
if key == '--ip-crans' :
|
||||||
print 'On ne peut spécifier qu\'une machine à la fois'
|
try :
|
||||||
sys.exit(2)
|
ip_crans.append(socket.gethostbyaddr(value)[2][0])
|
||||||
|
ip_crans_nom.append(socket.gethostbyaddr(value)[0])
|
||||||
|
except socket.gaierror :
|
||||||
|
print "Hôte %s inconnu" % value
|
||||||
|
sys.exit(5)
|
||||||
|
if len(ip_crans_nom)==1:
|
||||||
|
print ' depuis la machine %s' % ip_crans_nom[0]
|
||||||
|
elif ip_crans_nom:
|
||||||
|
print ' depuis les machines %s' % ', '.join(ip_crans_nom)
|
||||||
|
|
||||||
|
# recherche de la machine ext
|
||||||
|
#############################
|
||||||
|
ip_ext = []
|
||||||
|
ip_ext_nom = []
|
||||||
|
for key,value in opts :
|
||||||
|
if key == '--ip-ext' :
|
||||||
|
# recherche de l'ip de la machine extérieur
|
||||||
|
try :
|
||||||
|
ip_ext.append(socket.gethostbyaddr(value)[2][0])
|
||||||
|
except socket.gaierror :
|
||||||
|
print "Hôte %s inconnu" % value
|
||||||
|
sys.exit(5)
|
||||||
|
|
||||||
|
# recherche du nom d'hote
|
||||||
|
try :
|
||||||
|
ip_ext_nom.append(socket.gethostbyaddr(value)[0])
|
||||||
|
except socket.gaierror :
|
||||||
|
ip_ext_nom.append(ip_ext[-1])
|
||||||
|
if len(ip_ext_nom)==1:
|
||||||
|
print ' vers la machine extérieure %s' % ip_ext_nom[0]
|
||||||
|
elif ip_ext_nom:
|
||||||
|
print ' vers les machines extérieures %s' % ', '.join(ip_ext_nom)
|
||||||
|
|
||||||
|
# limite d'affichage
|
||||||
|
####################
|
||||||
|
show = [ x[1] for x in opts if x[0] == '--show' and x[1] in ['ip_crans','ip_ext','port_crans','port_ext'] ]
|
||||||
|
if not show :
|
||||||
|
show = ['ip_crans','ip_ext','port_crans','port_ext']
|
||||||
else :
|
else :
|
||||||
try :
|
print ' affichage de %s' % ', '.join(show)
|
||||||
ip_crans = socket.gethostbyaddr(args[0])
|
|
||||||
except socket.gaierror :
|
|
||||||
print "Hôte %s inconnu" % args[0]
|
|
||||||
sys.exit(5)
|
|
||||||
|
|
||||||
print 'Statistiques pour la machine %s' % ip_crans[0]
|
|
||||||
ip_crans = ip_crans[2][0]
|
|
||||||
|
|
||||||
|
# upload mini à afficher
|
||||||
|
########################
|
||||||
|
upload_mini = 0
|
||||||
|
for key,value in opts :
|
||||||
|
if key == '--upload-mini' :
|
||||||
|
try :
|
||||||
|
upload_mini = int(value)
|
||||||
|
except :
|
||||||
|
print 'L\'upload mini doit être un entier (en MO)'
|
||||||
|
sys.exit(4)
|
||||||
|
break
|
||||||
|
if upload_mini:
|
||||||
|
print ' pour les traffics supérieurs à %d Mo' % upload_mini
|
||||||
|
|
||||||
# nombre limite d'enregristrements
|
# nombre limite d'enregristrements
|
||||||
##################################
|
##################################
|
||||||
limit = 10
|
limit = 10
|
||||||
for key,value in opts :
|
for key,value in opts :
|
||||||
if key == '-l' :
|
if key == 'show-limit' :
|
||||||
try :
|
try :
|
||||||
limit = int(value)
|
limit = int(value)
|
||||||
except :
|
except :
|
||||||
|
@ -144,37 +219,34 @@ Exemples :
|
||||||
sys.exit(3)
|
sys.exit(3)
|
||||||
break
|
break
|
||||||
|
|
||||||
# upload mini à afficher
|
# début de l'analyse
|
||||||
########################
|
####################
|
||||||
upload_mini = 0
|
begin_time = 24
|
||||||
for key,value in opts :
|
for key,value in opts :
|
||||||
if key == '-u' :
|
if key == '--begin-time' :
|
||||||
try :
|
try :
|
||||||
upload_mini = int(value)
|
begin_time = int(value)
|
||||||
except :
|
|
||||||
print 'L\'upload mini doit être un entier (en MO)'
|
|
||||||
sys.exit(4)
|
|
||||||
break
|
|
||||||
|
|
||||||
# durée pour laquelle on compte l'upload
|
|
||||||
########################################
|
|
||||||
heures = 24
|
|
||||||
for key,value in opts :
|
|
||||||
if key == '-t' :
|
|
||||||
try :
|
|
||||||
heures = int(value)
|
|
||||||
except :
|
except :
|
||||||
print 'Le nombre d\'heures doit être un entier'
|
print 'Le nombre d\'heures doit être un entier'
|
||||||
sys.exit(4)
|
sys.exit(4)
|
||||||
break
|
break
|
||||||
|
|
||||||
# groupements
|
# fin de l'analyse
|
||||||
#############
|
##################
|
||||||
group = [ x[1] for x in opts if x[0] == '-g' and x[1] in ['ip_ext','port_crans','port_ext'] ]
|
end_time = 0
|
||||||
if not group :
|
for key,value in opts :
|
||||||
group = ['ip_ext','port_crans','port_ext']
|
if key == '--end-time' :
|
||||||
group.append('ip_crans')
|
try :
|
||||||
|
end_time = int(value)
|
||||||
|
except :
|
||||||
|
print 'Le nombre d\'heures doit être un entier'
|
||||||
|
sys.exit(4)
|
||||||
|
break
|
||||||
|
if begin_time != 24 or end_time:
|
||||||
|
print ' entre il y a %d heures et il y a %d heures' % (begin_time,end_time)
|
||||||
|
|
||||||
# affichage du résultat
|
# affichage du résultat
|
||||||
#######################
|
#######################
|
||||||
print stats(ip_crans, group, upload_mini, limit, heures)
|
#print stats(ip_crans, group, upload_mini, limit, heures)
|
||||||
|
print stats (ip_crans, ip_ext, show, upload_mini, limit, begin_time, end_time)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue