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/')
|
||||
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
|
||||
d'upload de l'ip fourni
|
||||
|
||||
ip_crans : ip de la machine
|
||||
group : liste des champs à àfficher détailler
|
||||
parmi : ip_ext, port_crans, port_ext
|
||||
ip_crans : ips des machine
|
||||
chaine de caratère si il a qu'une machine
|
||||
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
|
||||
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
|
||||
##########################################
|
||||
|
||||
|
@ -28,27 +38,34 @@ def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, li
|
|||
format = []
|
||||
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')
|
||||
largeur.append('*')
|
||||
titre.append('machine ext')
|
||||
format.append('s')
|
||||
alignement.append('c')
|
||||
|
||||
if 'port_ext' in group :
|
||||
if 'port_ext' in show :
|
||||
select.append('port_ext')
|
||||
largeur.append(10)
|
||||
titre.append('port ext')
|
||||
format.append('s')
|
||||
alignement.append('d')
|
||||
|
||||
if 'port_crans' in group :
|
||||
if 'port_crans' in show :
|
||||
select.append('port_crans')
|
||||
largeur.append(10)
|
||||
titre.append('port crans')
|
||||
format.append('s')
|
||||
alignement.append('d')
|
||||
|
||||
|
||||
select += ['sum(download) as download','sum(upload) as upload']
|
||||
largeur += [10,10]
|
||||
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 = "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')
|
||||
curseur = pgsql.cursor()
|
||||
|
@ -75,7 +97,7 @@ def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, li
|
|||
return ip
|
||||
|
||||
for champ in select :
|
||||
if champ == 'ip_ext' :
|
||||
if champ in ['ip_ext','ip_crans']:
|
||||
col = select.index(champ)
|
||||
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__' :
|
||||
|
||||
help = """Statistiques d'upload d'une machine du crans
|
||||
usage: analyse.py [options] machine
|
||||
usage: analyse.py [options]
|
||||
Option fait partie des options suivantes :
|
||||
-l # : nombre de lignes à afficher
|
||||
par défaut : 10
|
||||
-g : champs à grouper (parmis ip_ext, port_crans, port_ext)
|
||||
par défaut : -p ip_ext -p port_crans -p port_ext
|
||||
-u # : ne prend quee les entrées ou l'upload est supérieur à #
|
||||
par défaut : 0
|
||||
-t # : nombre d'heures à analyser
|
||||
par défaut : 24
|
||||
--ip-crans
|
||||
ip de la machine crans
|
||||
--ip-ext
|
||||
ip de la machine extérieure
|
||||
--show
|
||||
champs à afficher (parmi ip_crans, ip_ext, port_crans, port_ext)
|
||||
--upload-mini
|
||||
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 :
|
||||
sudo /usr/scripts/surveillance/analyse.py bilou.crans.org"""
|
||||
|
||||
|
@ -102,41 +131,87 @@ Exemples :
|
|||
|
||||
# aide
|
||||
######
|
||||
if '-h' in sys.argv :
|
||||
if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv)==1:
|
||||
print help
|
||||
sys.exit(0)
|
||||
|
||||
# parsage des arguments
|
||||
#######################
|
||||
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 :
|
||||
print message
|
||||
print help
|
||||
sys.exit(4)
|
||||
|
||||
print "Statistiques d'upload"
|
||||
|
||||
# recherche de la machine crans
|
||||
###############################
|
||||
if len(args) == 0 :
|
||||
print 'Il faut spécifier une adresse'
|
||||
sys.exit(1)
|
||||
elif len(args) >= 2 :
|
||||
print 'On ne peut spécifier qu\'une machine à la fois'
|
||||
sys.exit(2)
|
||||
ip_crans = []
|
||||
ip_crans_nom = []
|
||||
for key,value in opts :
|
||||
if key == '--ip-crans' :
|
||||
try :
|
||||
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 :
|
||||
try :
|
||||
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]
|
||||
print ' affichage de %s' % ', '.join(show)
|
||||
|
||||
# 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
|
||||
##################################
|
||||
limit = 10
|
||||
for key,value in opts :
|
||||
if key == '-l' :
|
||||
if key == 'show-limit' :
|
||||
try :
|
||||
limit = int(value)
|
||||
except :
|
||||
|
@ -144,37 +219,34 @@ Exemples :
|
|||
sys.exit(3)
|
||||
break
|
||||
|
||||
# upload mini à afficher
|
||||
########################
|
||||
upload_mini = 0
|
||||
# début de l'analyse
|
||||
####################
|
||||
begin_time = 24
|
||||
for key,value in opts :
|
||||
if key == '-u' :
|
||||
if key == '--begin-time' :
|
||||
try :
|
||||
upload_mini = 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)
|
||||
begin_time = int(value)
|
||||
except :
|
||||
print 'Le nombre d\'heures doit être un entier'
|
||||
sys.exit(4)
|
||||
break
|
||||
|
||||
# groupements
|
||||
#############
|
||||
group = [ x[1] for x in opts if x[0] == '-g' and x[1] in ['ip_ext','port_crans','port_ext'] ]
|
||||
if not group :
|
||||
group = ['ip_ext','port_crans','port_ext']
|
||||
group.append('ip_crans')
|
||||
|
||||
|
||||
# fin de l'analyse
|
||||
##################
|
||||
end_time = 0
|
||||
for key,value in opts :
|
||||
if key == '--end-time' :
|
||||
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
|
||||
#######################
|
||||
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