From d99bdc7601495314b3e53d98c15c09b624b6a450 Mon Sep 17 00:00:00 2001 From: Valentin Samir Date: Tue, 22 Oct 2013 20:22:19 +0200 Subject: [PATCH] =?UTF-8?q?[analyse,=20deconnexion]=20Dump=20des=20logs=20?= =?UTF-8?q?d'upload=20quand=20un=20utilisateur=20est=20brid=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Les fichiers sont mis sans /usr/scripts/var/analyse/$ip-$time.txt --- surveillance/analyse.py | 72 ++++++++++++++++++++++++++++--------- surveillance/deconnexion.py | 2 ++ 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/surveillance/analyse.py b/surveillance/analyse.py index b26d4f97..6890e337 100755 --- a/surveillance/analyse.py +++ b/surveillance/analyse.py @@ -126,14 +126,15 @@ def stats(ip_crans=[], ip_ext=[], # on transforme tout en chaine results = [ [ str(x) for x in line ] for line in results ] + headers="" try: upload = 0 download = 0 for line in results: upload+=int(line[4]) download+=int(line[3]) - print " upload: %sMo" % (upload/1024/1024) - print " download: %sMo" % (download/1024/1024) + headers+=" upload: %sMo\n" % (upload/1024/1024) + headers+=" download: %sMo\n" % (download/1024/1024) except IndexError: pass @@ -168,9 +169,32 @@ def stats(ip_crans=[], ip_ext=[], results = [x[:col] + [port_to_service.get(x[col],x[col])] + x[col+1:] for x in results] - return tableau(results, titre=titre, largeur=largeur, + return headers + tableau(results, titre=titre, largeur=largeur, alignement=alignement, format=format) + +def stats_fork(stub='', ip_crans=[], + show=['ip_crans', 'ip_ext', 'port_crans', 'port_ext'], + upload_mini=0, show_limit=10, begin_time=24, end_time=0, + show_download=False,resolve_dns=False): + if '/' in stub: + return + import subprocess + args=[] + if show_download: + args.append('--show-download') + args.extend(['--show-limit','%s' % show_limit]) + args.extend(['--begin-time', '%s' % begin_time]) + args.extend(['--end-time', '%s' % end_time]) + if not resolve_dns: + args.append('-n') + for ip in ip_crans: + subprocess.Popen( + ["/usr/scripts/surveillance/analyse.py", + "--file","/usr/scripts/var/analyse/%s%s.txt" % (ip,stub), + "--ip-crans", "%s" % ip + ] + args) + if __name__ == '__main__' : help = """Statistiques d'upload d'une machine du crans @@ -198,6 +222,8 @@ Option fait partie des options suivantes : Exemple : sudo /usr/scripts/surveillance/analyse.py bilou.crans.org""" + headers="" + # import des modules import getopt @@ -212,12 +238,11 @@ Exemple : try : opts, args = getopt.getopt(sys.argv[1:], 'dn', ['show-download', 'ip-crans=', 'ip-ext=', 'show=', 'upload-mini=', - 'begin-time=', 'end-time=', 'show-limit=']) + 'begin-time=', 'end-time=', 'show-limit=', 'file=']) except getopt.GetoptError,message : print help sys.exit(4) - # affichage des stats de download ################################# show_download = False @@ -225,9 +250,9 @@ Exemple : if key == '-d' or key == '--show-download': show_download = True if show_download: - print "Statistiques de download" + headers+=u"Statistiques de download\n" else: - print "Statistiques d'upload" + headers+=u"Statistiques d'upload\n" # recherche de la machine crans ############################### @@ -245,9 +270,9 @@ Exemple : print "Hôte %s inconnu" % value ip_crans.append(value) if len(ip_crans_nom)==1: - print ' depuis la machine %s' % ip_crans_nom[0] + headers+=u' depuis la machine %s\n' % ip_crans_nom[0] elif ip_crans_nom: - print ' depuis les machines %s' % ', '.join(ip_crans_nom) + headers+=u' depuis les machines %s\n' % ', '.join(ip_crans_nom) # recherche de la machine ext ############################# @@ -272,9 +297,9 @@ Exemple : 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] + headers+=u' vers la machine extérieure %s\n' % ip_ext_nom[0] elif ip_ext_nom: - print ' vers les machines extérieures %s' % ', '.join(ip_ext_nom) + headers+=u' vers les machines extérieures %s\n' % ', '.join(ip_ext_nom) # limite d'affichage #################### @@ -284,7 +309,7 @@ Exemple : if not show : show = ['ip_crans', 'ip_ext', 'port_crans', 'port_ext'] else : - print ' affichage de %s' % ', '.join(show) + headers+=u' affichage de %s\n' % ', '.join(show) # upload mini à afficher ######################## @@ -298,7 +323,7 @@ Exemple : sys.exit(4) break if upload_mini: - print ' pour les traffics supérieurs à %d Mo' % upload_mini + headers+=u' pour les traffics supérieurs à %d Mo\n' % upload_mini # nombre limite d'enregristrements ################################## @@ -307,7 +332,7 @@ Exemple : if key == '--show-limit' : try : limit = int(value) - print ' affichage des %d premiers résultats' % limit + headers+=u' affichage des %d premiers résultats\n' % limit except : print 'Le nombre limite n\'est pas un entier' sys.exit(3) @@ -341,11 +366,24 @@ Exemple : sys.exit(4) break if begin_time != 24 or end_time: - print (' entre il y a %d heure(s) et il y a %d heure(s)' - % (begin_time,end_time)) + headers+=u' entre il y a %d heure(s) et il y a %d heure(s)\n' % (begin_time,end_time) + + # fichier de sortie + ################### + file=None + for key,value in opts : + if key == '--file' : + file = value + # affichage du résultat ####################### - print stats(ip_crans, ip_ext, show, upload_mini, limit, begin_time, + if not file: + print headers + stats(ip_crans, ip_ext, show, upload_mini, limit, begin_time, end_time, show_download,resolve_dns) + else: + with open(file, 'w') as f: + f.write(headers.encode('utf-8')) + f.write(stats(ip_crans, ip_ext, show, upload_mini, limit, begin_time, + end_time, show_download,resolve_dns)) diff --git a/surveillance/deconnexion.py b/surveillance/deconnexion.py index d3c0ce41..6bd2b3fe 100755 --- a/surveillance/deconnexion.py +++ b/surveillance/deconnexion.py @@ -34,6 +34,7 @@ import lock sys.path.append('/usr/scripts/surveillance/fiche_deconnexion') from generate import generate_ps from affich_tools import tableau +import analyse import mail as mail_module @@ -231,6 +232,7 @@ for elupload, eltype, elid in uploadeurs: # On inscrit l'instance dans la table des avertis_hard ###################################################### curseur.execute("INSERT INTO avertis_upload_hard (type,id,date) VALUES ('%s','%d','now')"%(eltype,elid)) + analyse.stats_fork('-%s' % debut, ip_crans=[m.ip() for m in proprio.machine()], show_limit=500) except Exception as error: sys.stderr.write("Blacklist de id=%s pour %s Mo échoué, %s\n" % (proprio.id(), elupload, error)) continue