
flemme de regarder comment elle est construite) C'est un dbut amliorer si quelqu'un est motiv... darcs-hash:20051026104743-4ec08-a3301f9088cda8a1a918c96198fc2afa8f9bfb91.gz
84 lines
2.3 KiB
Python
Executable file
84 lines
2.3 KiB
Python
Executable file
#! /usr/bin/env python
|
|
# -*- coding: iso8859-15 -*-
|
|
|
|
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
|
|
|
|
Exemples :
|
|
sudo /usr/scripts/surveillance/analyse.py bilou.crans.org"""
|
|
|
|
# import des modules
|
|
import getopt
|
|
from socket import gethostbyaddr
|
|
import sys
|
|
|
|
# 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)
|
|
|
|
# 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
|
|
|
|
# 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')
|
|
|
|
# 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']
|
|
|
|
# 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)
|