
(machines extrieures) darcs-hash:20051117230227-4ec08-5aa64fb5369ad62946c29e50e8a3ae2e395223bd.gz
166 lines
5 KiB
Python
Executable file
166 lines
5 KiB
Python
Executable file
#! /usr/bin/env python
|
|
# -*- coding: iso8859-15 -*-
|
|
|
|
import socket
|
|
import sys
|
|
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) :
|
|
"""
|
|
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 = []
|
|
largeur = []
|
|
titre = []
|
|
format = []
|
|
alignement = []
|
|
|
|
if 'ip_ext' in group :
|
|
select.append('ip_ext')
|
|
largeur.append('*')
|
|
titre.append('machine ext')
|
|
format.append('s')
|
|
alignement.append('c')
|
|
|
|
if 'port_ext' in group :
|
|
select.append('port_ext')
|
|
largeur.append(10)
|
|
titre.append('port ext')
|
|
format.append('s')
|
|
alignement.append('d')
|
|
|
|
if 'port_crans' in group :
|
|
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']
|
|
format += ['o','o']
|
|
alignement += ['d','d']
|
|
|
|
# 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*1024*1024, 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_ng(results, titre=titre, largeur=largeur, alignement=alignement, format=format)
|
|
|
|
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
|
|
|
|
# aide
|
|
######
|
|
if '-h' in sys.argv :
|
|
print help
|
|
sys.exit(0)
|
|
|
|
# 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)
|
|
|
|
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 nombre limite n\'est pas un entier'
|
|
sys.exit(3)
|
|
break
|
|
|
|
# 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
|
|
|
|
# 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')
|
|
|
|
# affichage du résultat
|
|
#######################
|
|
print stats(ip_crans, group, upload_mini, limit)
|