nouveau format de tableau, utilisation dans la gestion de la surveillance du
rseau darcs-hash:20051103235738-4ec08-4cbc92f43c8eb43da17d910d09e5d5597e9d781c.gz
This commit is contained in:
parent
ba62565f57
commit
92fa0abe2a
3 changed files with 132 additions and 78 deletions
|
@ -129,41 +129,47 @@ def tableau(largeurs,data) :
|
|||
f = f.replace(u'\n',s,1) # Insertion du séparateur entète - corps
|
||||
return f[:-1] # Supression du \n final
|
||||
|
||||
def tableau_ng(data,titres=None,largeurs=None,allignements=None,formats=None) :
|
||||
def tableau_ng(data,titre=None,largeur=None,alignement=None,format=None) :
|
||||
"""
|
||||
retourne une chaine formatée repésentant un tableau
|
||||
Retourne une chaine formatée repésentant un tableau
|
||||
|
||||
data : liste de listes, chacune contenant les valeurs d'une ligne
|
||||
data : liste de listes, chacune contenant les valeurs d'une ligne
|
||||
|
||||
titres : liste des titres
|
||||
Si none, n'affiche pas de ligne de titre
|
||||
titre : liste des titres
|
||||
Si none, n'affiche pas de ligne de titre
|
||||
|
||||
largeurs : liste des largeurs des colonnes, '*' met la plus grande
|
||||
largeur possible.
|
||||
Si None, réduit aux max chaque colonne
|
||||
largeur : liste des largeurs des colonnes, '*' met la plus grande
|
||||
largeur possible.
|
||||
Si None, réduit aux max chaque colonne
|
||||
|
||||
allignements: liste des alignements : c = centrer
|
||||
alignement : liste des alignements : c = centrer
|
||||
g = gauche
|
||||
d = droit
|
||||
Si None, met c pour chaque colonne
|
||||
Si None, met c pour chaque colonne
|
||||
|
||||
formats : liste des formats : s = string
|
||||
format : liste des formats : s = string
|
||||
o = octet
|
||||
Si None, s pour chaque colonne
|
||||
Si None, s pour chaque colonne
|
||||
"""
|
||||
sep_col = u'|'
|
||||
nbcols = len(data[0])
|
||||
if data :
|
||||
nbcols = len(data[0])
|
||||
elif titre :
|
||||
nbcols = len(titre)
|
||||
else :
|
||||
return u'Aucune donnée'
|
||||
|
||||
# Formats
|
||||
#########
|
||||
if not formats :
|
||||
formats = ['s'] * nbcols
|
||||
if not format :
|
||||
format = ['s'] * nbcols
|
||||
|
||||
def reformate (data, format) :
|
||||
if format == 's' :
|
||||
return str(data)
|
||||
|
||||
elif format == 'o' :
|
||||
data = int(data)
|
||||
if data > 0.8 * 1024**3 :
|
||||
return str(round(data/1024**3,1))+'Go'
|
||||
elif data > 0.8 * 1024**2 :
|
||||
|
@ -173,45 +179,74 @@ def tableau_ng(data,titres=None,largeurs=None,allignements=None,formats=None) :
|
|||
else :
|
||||
return str(round(data,1))+'o'
|
||||
|
||||
data = [ [ reformate(ligne[i],formats[i]) for i in range(nbcols) ] for ligne in data ]
|
||||
data = [ [ reformate(ligne[i],format[i]) for i in range(nbcols) ] for ligne in data ]
|
||||
|
||||
# Largeurs
|
||||
##########
|
||||
if not largeurs :
|
||||
largeurs = [ max([len(sre.sub('\x1b\[1;([0-9]|[0-9][0-9])m','',ligne[i])) for ligne in data]) for i in range(nbcols) ]
|
||||
elif '*' in largeurs:
|
||||
if not largeur :
|
||||
largeur = [ max([len(sre.sub('\x1b\[1;([0-9]|[0-9][0-9])m','',ligne[i])) for ligne in data]) for i in range(nbcols) ]
|
||||
elif '*' in largeur:
|
||||
rows, cols = get_screen_size()
|
||||
for i in range(nbcols) :
|
||||
if largeurs[i] in ['*',-1] :
|
||||
largeurs[i] = max(cols - sum([l for l in largeurs if l != '*']) - nbcols - 1, 3)
|
||||
if largeur[i] in ['*',-1] :
|
||||
largeur[i] = max(cols - sum([l for l in largeurs if l != '*']) - nbcols - 1, 3)
|
||||
break
|
||||
print largeurs
|
||||
|
||||
# Allignement
|
||||
#############
|
||||
if not allignements :
|
||||
allignements = ['c'] * nbcols
|
||||
|
||||
def alligne (data, allignement, largeur) :
|
||||
# Alignement
|
||||
############
|
||||
if not alignement :
|
||||
alignement = ['c'] * nbcols
|
||||
|
||||
def aligne (data, alignement, largeur) :
|
||||
# Longeur sans les chaines de formatage
|
||||
l = largeur - len(sre.sub('\x1b\[1;([0-9]|[0-9][0-9])m','',data))
|
||||
# Allignement
|
||||
if allignement == 'g' :
|
||||
return data + u' '*l
|
||||
elif allignement == 'd' :
|
||||
return u' '*l + data
|
||||
l = len(sre.sub('\x1b\[1;([0-9]|[0-9][0-9])m','',data))
|
||||
|
||||
# Alignement
|
||||
if l > largeur :
|
||||
# découpage d'une chaine trop longue
|
||||
regexp = sre.compile('\x1b\[1;([0-9]|[0-9][0-9])m')
|
||||
new_data = u''
|
||||
new_len = 0
|
||||
|
||||
# On laisse la mise en forme et on coupe les caratères affichés
|
||||
while True :
|
||||
s = regexp.search(data)
|
||||
if s and not s.start() :
|
||||
# c'est de la mise en forme
|
||||
new_data += data[:s.end()]
|
||||
data = data[s.end():]
|
||||
elif new_len < largeur - 1 :
|
||||
# c'est un caratère normal, et il y a la place
|
||||
new_data += data[0]
|
||||
data = data[1:]
|
||||
new_len += 1
|
||||
else :
|
||||
# c'est un caratère normal mais on a dépassé le max
|
||||
data = data[1:]
|
||||
if not data :
|
||||
return new_data + '*'
|
||||
|
||||
elif l == largeur :
|
||||
return data
|
||||
|
||||
elif alignement == 'g' :
|
||||
return u' ' + data + u' '*(largeur-l-1)
|
||||
|
||||
elif alignement == 'd' :
|
||||
return u' '*(largeur-l-1) + data + u' '
|
||||
|
||||
else :
|
||||
return u' '*(l/2) + data + u' '*((l+1)/2)
|
||||
return u' '*((largeur-l)/2) + data + u' '*((largeur-l+1)/2)
|
||||
|
||||
data = [ [ alligne(ligne[i],allignements[i],largeurs[i]) for i in range(nbcols) ] for ligne in data ]
|
||||
data = [ [ aligne(ligne[i],alignement[i],largeur[i]) for i in range(nbcols) ] for ligne in data ]
|
||||
|
||||
# Le titre
|
||||
##########
|
||||
if titres :
|
||||
if titre :
|
||||
# ligne de titre
|
||||
chaine = sep_col + sep_col.join([alligne(titres[i],'c',largeurs[i]) for i in range(nbcols)]) + sep_col + u'\n'
|
||||
chaine = sep_col + sep_col.join([aligne(titre[i],'c',largeur[i]) for i in range(nbcols)]) + sep_col + u'\n'
|
||||
# ligne de séparation
|
||||
chaine += sep_col + u'+'.join([u'-'*largeurs[i] for i in range(nbcols)]) + sep_col + u'\n'
|
||||
chaine += sep_col + u'+'.join([u'-'*largeur[i] for i in range(nbcols)]) + sep_col + u'\n'
|
||||
else :
|
||||
chaine = u''
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import socket
|
|||
import sys
|
||||
from pyPgSQL import PgSQL
|
||||
sys.path.append('/usr/scripts/gestion/')
|
||||
from affich_tools import tableau
|
||||
from affich_tools import tableau_ng
|
||||
|
||||
def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, limit=10) :
|
||||
"""
|
||||
|
@ -23,27 +23,37 @@ def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, li
|
|||
##########################################
|
||||
|
||||
select = []
|
||||
largeurs = []
|
||||
titres = []
|
||||
|
||||
largeur = []
|
||||
titre = []
|
||||
format = []
|
||||
alignement = []
|
||||
|
||||
if 'ip_ext' in group :
|
||||
select.append('ip_ext')
|
||||
largeurs.append('*')
|
||||
titres.append('machine ext')
|
||||
largeur.append(30)
|
||||
titre.append('machine ext')
|
||||
format.append('s')
|
||||
alignement.append('c')
|
||||
|
||||
if 'port_ext' in group :
|
||||
select.append('port_ext')
|
||||
largeurs.append(10)
|
||||
titres.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')
|
||||
largeurs.append(10)
|
||||
titres.append('port crans')
|
||||
largeur.append(10)
|
||||
titre.append('port crans')
|
||||
format.append('s')
|
||||
alignement.append('d')
|
||||
|
||||
select += ['floor(sum(download)/1024/1024) as download','floor(sum(upload)/1024/1024) as upload']
|
||||
largeurs += [8,8]
|
||||
titres += ['download','upload']
|
||||
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
|
||||
######################
|
||||
|
@ -54,7 +64,7 @@ def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, li
|
|||
curseur.execute(requete)
|
||||
results = curseur.fetchall()
|
||||
|
||||
# on transforme tout en chaine
|
||||
# on transforme tout en chaine
|
||||
results = [ [ str(x) for x in line ] for line in results ]
|
||||
|
||||
# on modifie les ip en noms de machine
|
||||
|
@ -69,7 +79,7 @@ def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, li
|
|||
col = select.index(champ)
|
||||
results = [ x[:col] + [nom_de_machine(x[col])] + x[col+1:] for x in results ]
|
||||
|
||||
return tableau(largeurs, [titres] + results )
|
||||
return tableau_ng(results, titre=titre, largeur=largeur, alignement=alignement, format=format)
|
||||
|
||||
if __name__ == '__main__' :
|
||||
|
||||
|
|
|
@ -16,10 +16,12 @@ import commands
|
|||
import sys
|
||||
from pyPgSQL import PgSQL
|
||||
sys.path.append('/usr/scripts/gestion')
|
||||
sys.path.append('/usr/scripts/surveillance')
|
||||
import config
|
||||
import smtplib
|
||||
import socket
|
||||
from analyse import stats
|
||||
from affich_tools import tableau_ng
|
||||
|
||||
######################
|
||||
# Fonctions à la con #
|
||||
|
@ -50,34 +52,29 @@ mail = smtplib.SMTP('localhost')
|
|||
|
||||
# Adhérents avertis :
|
||||
#####################
|
||||
requete = "SELECT ip_crans,sum(upload) as somme,sum(download) FROM upload WHERE ip_crans IN (SELECT ip_crans FROM avertis_upload where hard='1' or soft='1') and date > timestamp 'now' - interval '1 day' GROUP BY ip_crans order by somme"
|
||||
requete = "SELECT ip_crans,sum(upload) as somme,sum(download) FROM upload WHERE ip_crans IN (SELECT ip_crans FROM avertis_upload where hard='1' or soft='1') and date > timestamp 'now' - interval '1 day' GROUP BY ip_crans ORDER BY somme DESC"
|
||||
curseur.execute(requete)
|
||||
hard = curseur.fetchall()
|
||||
liste_upload = '| upload | download | machine |\n'
|
||||
liste_upload += '|----------|----------|------------------------------|\n'
|
||||
for IP,upload,download in hard:
|
||||
hostname = socket.gethostbyaddr("%s"%IP)[0]
|
||||
liste_upload += '| %7so | %7so | %28s |\n' % (humanise(upload),humanise(download),hostname)
|
||||
liste_upload = tableau_ng( data = [ [l[1], l[2], socket.gethostbyaddr(str(l[0]))[0]] for l in curseur.fetchall() ],
|
||||
titre = ['upload','download','machine'],
|
||||
largeur = [10, 10, 30],
|
||||
format = ['o','o','s'],
|
||||
alignement = ['d','d','c']).encode('iso-8859-15')
|
||||
|
||||
|
||||
# Upload exemptés :
|
||||
###################
|
||||
requete = "SELECT ip_crans,sum(upload) AS somme , sum(download) FROM upload WHERE ip_crans IN ( SELECT ip_crans from exemptes) and date > timestamp 'now' - interval '1 day' GROUP BY ip_crans order by somme"
|
||||
requete = "SELECT ip_crans,sum(upload) AS somme , sum(download) FROM upload WHERE ip_crans IN ( SELECT ip_crans from exemptes) and date > timestamp 'now' - interval '1 day' GROUP BY ip_crans ORDER BY somme DESC"
|
||||
curseur.execute(requete)
|
||||
exemptes = curseur.fetchall()
|
||||
liste_exemptes = '| upload | download | machine |\n'
|
||||
liste_exemptes += '|----------|----------|------------------------------|\n'
|
||||
for IP,upload,download in exemptes:
|
||||
if upload < config.upload.soft :
|
||||
continue
|
||||
hostname = socket.gethostbyaddr("%s"%IP)[0]
|
||||
liste_exemptes += '| %7so | %7so | %28s |\n' % (humanise(upload),humanise(download),hostname)
|
||||
liste_exemptes = tableau_ng( data = [ [l[1],l[2],socket.gethostbyaddr(str(l[0]))[0] ] for l in curseur.fetchall() ],
|
||||
titre = ['upload','download','machine'],
|
||||
largeur = [10, 10, 30],
|
||||
format = ['o','o','s'],
|
||||
alignement = ['d','d','c']).encode('iso-8859-15')
|
||||
|
||||
|
||||
# Upload des serveurs :
|
||||
#######################
|
||||
liste_serveurs = '| upload | download | machine |\n'
|
||||
liste_serveurs += '|----------|----------|------------------------------|\n'
|
||||
liste_serveurs = []
|
||||
for i in (10,1,3,4,7,8,9,11):
|
||||
IP='138.231.136.%d' % i
|
||||
hostname = socket.gethostbyaddr("%s"%IP)[0]
|
||||
|
@ -86,7 +83,13 @@ for i in (10,1,3,4,7,8,9,11):
|
|||
traffic=curseur.fetchone()
|
||||
if traffic == [None, None] :
|
||||
continue
|
||||
liste_serveurs += '| %7so | %7so | %28s |\n' % (humanise(traffic[0]),humanise(traffic[1]),str(hostname))
|
||||
liste_serveurs.append([traffic[0],traffic[1],hostname])
|
||||
|
||||
liste_serveurs = tableau_ng( data = liste_serveurs,
|
||||
titre = ['upload','download','serveur'],
|
||||
largeur = [10, 10, 30],
|
||||
format = ['o','o','s'],
|
||||
alignement = ['d','d','c']).encode('iso-8859-15')
|
||||
|
||||
|
||||
# statistiques des gros uploads depuis les serveurs
|
||||
|
@ -116,27 +119,33 @@ for serveur in serveurs :
|
|||
requete = "SELECT DISTINCT ip_crans FROM avertis_virus"
|
||||
curseur.execute(requete)
|
||||
infections = [ x[0] for x in curseur.fetchall() ]
|
||||
liste_virus=""
|
||||
liste_virus=[]
|
||||
for IP in infections:
|
||||
try:
|
||||
hostname = socket.gethostbyaddr("%s"%IP)[0]
|
||||
except socket.herror:
|
||||
continue
|
||||
liste_virus+="%s\n"%(str(hostname))
|
||||
liste_virus.append(["%s\n"%(str(hostname))])
|
||||
|
||||
liste_virus = tableau_ng(liste_virus, titre=['machine'], largeur=[30]).encode('iso-8859-15')
|
||||
|
||||
|
||||
# Machines utilisant des protocoles P2P :
|
||||
#########################################
|
||||
requete = "SELECT ip_crans,protocole FROM avertis_p2p WHERE date>timestamp 'now' - interval '1 day'"
|
||||
curseur.execute(requete)
|
||||
liste_p2p = '|protocole | machine | nombre |\n'
|
||||
liste_p2p += '|----------|------------------------------|----------|\n'
|
||||
liste_p2p = []
|
||||
for IP, protocole in curseur.fetchall():
|
||||
hostname = socket.gethostbyaddr(IP)[0]
|
||||
requete = "SELECT COUNT(ip_src) from p2p where date > timestamp 'now' - interval '1 day' and ip_src='%s'"%IP
|
||||
curseur.execute(requete)
|
||||
paquets=curseur.fetchone()[0]
|
||||
liste_p2p += '| %8s | %28s | %8s |\n' % (protocole,hostname,str(paquets))
|
||||
liste_p2p.append([protocole,hostname,str(paquets)])
|
||||
|
||||
liste_p2p = tableau_ng( data = liste_p2p,
|
||||
titre = ['protocole','machine','nombre'],
|
||||
largeur = [12, 30, 8],
|
||||
alignement = ['g','c','d'] ).encode('iso-8859-15')
|
||||
|
||||
|
||||
#############
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue