Petites modification pour l'affichage,
Si on veut on peut remplacer le 'upload_massive_from_serveurs' par un truc du genre : print stats(ip_crans='138.231.136.3',group=['ip_ext'],upload_mini=20,limit=20) Ca fait pas tout a fait le meme rsultat, l'avantage c'est que ca utilise la base de donnes de rouge, et on peut l'integrer au mail journalier de xabi. darcs-hash:20051027135526-4ec08-4c9ec3f48d8672da9f5f485e0768db92260c0e07.gz
This commit is contained in:
parent
a741800743
commit
33196e37b6
1 changed files with 135 additions and 63 deletions
|
@ -1,84 +1,156 @@
|
|||
#! /usr/bin/env python
|
||||
# -*- coding: iso8859-15 -*-
|
||||
|
||||
help = """Statistiques d'upload d'une machine du crans
|
||||
import socket
|
||||
import sys
|
||||
from pyPgSQL import PgSQL
|
||||
sys.path.append('/usr/scripts/gestion/')
|
||||
from affich_tools import tableau
|
||||
|
||||
def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, limit=10) :
|
||||
"""
|
||||
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
|
||||
upload_mini : n'affiche que les lignes avec plus d'upload que la valeur donnée
|
||||
limit : nombre max de lignes à afficher
|
||||
"""
|
||||
|
||||
# variables pour la requete et l'affichage
|
||||
##########################################
|
||||
|
||||
select = []
|
||||
largeurs = []
|
||||
titres = []
|
||||
|
||||
if 'ip_ext' in group :
|
||||
select.append('ip_ext')
|
||||
largeurs.append('*')
|
||||
titres.append('machine ext')
|
||||
|
||||
if 'port_ext' in group :
|
||||
select.append('port_ext')
|
||||
largeurs.append(10)
|
||||
titres.append('port ext')
|
||||
|
||||
if 'port_crans' in group :
|
||||
select.append('port_crans')
|
||||
largeurs.append(10)
|
||||
titres.append('port crans')
|
||||
|
||||
select += ['floor(sum(download)/1024/1024) as download','floor(sum(upload)/1024/1024) as upload']
|
||||
largeurs += [8,8]
|
||||
titres += ['download','upload']
|
||||
|
||||
# requete dans la base
|
||||
######################
|
||||
requete = "SELECT * from ( SELECT %s FROM upload WHERE ip_crans='%s' AND date > timestamp 'now' - interval '1 day' GROUP BY %s ORDER BY upload DESC ) AS resultat_intemediaire WHERE upload>='%d' LIMIT %d;" % (','.join(select), ip_crans, ','.join(group), upload_mini, limit)
|
||||
|
||||
pgsql = PgSQL.connect(host='/var/run/postgresql', database='filtrage', user='crans')
|
||||
curseur = pgsql.cursor()
|
||||
curseur.execute(requete)
|
||||
results = curseur.fetchall()
|
||||
|
||||
# on transforme tout en chaine
|
||||
results = [ [ str(x) for x in line ] for line in results ]
|
||||
|
||||
# on modifie les ip en noms de machine
|
||||
def nom_de_machine (ip) :
|
||||
try :
|
||||
return socket.gethostbyaddr(ip)[0]
|
||||
except :
|
||||
return ip
|
||||
|
||||
for champ in select :
|
||||
if champ == 'ip_ext' :
|
||||
col = select.index(champ)
|
||||
results = [ x[:col] + [nom_de_machine(x[col])] + x[col+1:] for x in results ]
|
||||
|
||||
return tableau(largeurs, [titres] + results )
|
||||
|
||||
if __name__ == '__main__' :
|
||||
|
||||
help = """Statistiques d'upload d'une machine du crans
|
||||
usage: analyse.py [options] machine
|
||||
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
|
||||
Exemples :
|
||||
sudo /usr/scripts/surveillance/analyse.py bilou.crans.org"""
|
||||
|
||||
# import des modules
|
||||
import getopt
|
||||
from socket import gethostbyaddr
|
||||
import sys
|
||||
# import des modules
|
||||
import getopt
|
||||
|
||||
# aide
|
||||
######
|
||||
if '-h' in sys.argv :
|
||||
print help
|
||||
sys.exit(0)
|
||||
# aide
|
||||
######
|
||||
if '-h' in sys.argv :
|
||||
print help
|
||||
sys.exit(0)
|
||||
|
||||
# parsage des arguments
|
||||
#######################
|
||||
try :
|
||||
opts, args = getopt.getopt(sys.argv[1:],'g:l:')
|
||||
except getopt.GetoptError,message :
|
||||
print message
|
||||
sys.exit(4)
|
||||
# parsage des arguments
|
||||
#######################
|
||||
try :
|
||||
opts, args = getopt.getopt(sys.argv[1:],'g:l:u:')
|
||||
except getopt.GetoptError,message :
|
||||
print message
|
||||
sys.exit(4)
|
||||
|
||||
# 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)
|
||||
else :
|
||||
try :
|
||||
ip_crans = socket.gethostbyaddr(args[0])
|
||||
except socket.gaierror :
|
||||
print "Hôte %s inconnu" % args[0]
|
||||
sys.exit(5)
|
||||
|
||||
# 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)
|
||||
else :
|
||||
ip_crans = gethostbyaddr(args[0])
|
||||
print 'Statistiques pour la machine %s' % ip_crans[0]
|
||||
ip_crans = ip_crans[2][0]
|
||||
|
||||
# nombre limite d'enregristrements
|
||||
##################################
|
||||
limit = 10
|
||||
for key,value in opts :
|
||||
if key == '-l' :
|
||||
try :
|
||||
limit = int(value)
|
||||
except :
|
||||
print 'Le le nombre limite n\'est pas un entier'
|
||||
sys.exit(3)
|
||||
break
|
||||
# nombre limite d'enregristrements
|
||||
##################################
|
||||
limit = 10
|
||||
for key,value in opts :
|
||||
if key == '-l' :
|
||||
try :
|
||||
limit = int(value)
|
||||
except :
|
||||
print 'Le nombre limite n\'est pas un entier'
|
||||
sys.exit(3)
|
||||
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')
|
||||
# upload mini à afficher
|
||||
########################
|
||||
upload_mini = 0
|
||||
for key,value in opts :
|
||||
if key == '-u' :
|
||||
try :
|
||||
upload_mini = int(value)
|
||||
except :
|
||||
print 'L\'upload mini doit être un entier (en MO)'
|
||||
sys.exit(4)
|
||||
break
|
||||
|
||||
# selection
|
||||
###########
|
||||
select = []
|
||||
if 'ip_ext' in group :
|
||||
select.append('ip_ext')
|
||||
if 'port_ext' in group :
|
||||
select.append('port_ext')
|
||||
if 'port_crans' in group :
|
||||
select.append('port_crans')
|
||||
select += ['floor(sum(download)/1024/1024) as download','floor(sum(upload)/1024/1024) as upload']
|
||||
# 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')
|
||||
|
||||
# requete dans la base
|
||||
######################
|
||||
requete = "SELECT %s FROM upload WHERE ip_crans='%s' AND date > timestamp 'now' - interval '1 day' GROUP BY %s ORDER BY upload DESC LIMIT %d;" % (','.join(select), ip_crans, ','.join(group), limit)
|
||||
|
||||
# on utilise pas le module python, car cette fonction fait toutes seules la mise en page
|
||||
from commands import getoutput
|
||||
commande = "psql filtrage -U crans -c \"%s\"" % requete
|
||||
print getoutput(commande)
|
||||
# affichage du résultat
|
||||
#######################
|
||||
print stats(ip_crans, group, upload_mini, limit)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue