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
|
f = f.replace(u'\n',s,1) # Insertion du séparateur entète - corps
|
||||||
return f[:-1] # Supression du \n final
|
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
|
titre : liste des titres
|
||||||
Si none, n'affiche pas de ligne de titre
|
Si none, n'affiche pas de ligne de titre
|
||||||
|
|
||||||
largeurs : liste des largeurs des colonnes, '*' met la plus grande
|
largeur : liste des largeurs des colonnes, '*' met la plus grande
|
||||||
largeur possible.
|
largeur possible.
|
||||||
Si None, réduit aux max chaque colonne
|
Si None, réduit aux max chaque colonne
|
||||||
|
|
||||||
allignements: liste des alignements : c = centrer
|
alignement : liste des alignements : c = centrer
|
||||||
g = gauche
|
g = gauche
|
||||||
d = droit
|
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
|
o = octet
|
||||||
Si None, s pour chaque colonne
|
Si None, s pour chaque colonne
|
||||||
"""
|
"""
|
||||||
sep_col = u'|'
|
sep_col = u'|'
|
||||||
|
if data :
|
||||||
nbcols = len(data[0])
|
nbcols = len(data[0])
|
||||||
|
elif titre :
|
||||||
|
nbcols = len(titre)
|
||||||
|
else :
|
||||||
|
return u'Aucune donnée'
|
||||||
|
|
||||||
# Formats
|
# Formats
|
||||||
#########
|
#########
|
||||||
if not formats :
|
if not format :
|
||||||
formats = ['s'] * nbcols
|
format = ['s'] * nbcols
|
||||||
|
|
||||||
def reformate (data, format) :
|
def reformate (data, format) :
|
||||||
if format == 's' :
|
if format == 's' :
|
||||||
return str(data)
|
return str(data)
|
||||||
|
|
||||||
elif format == 'o' :
|
elif format == 'o' :
|
||||||
|
data = int(data)
|
||||||
if data > 0.8 * 1024**3 :
|
if data > 0.8 * 1024**3 :
|
||||||
return str(round(data/1024**3,1))+'Go'
|
return str(round(data/1024**3,1))+'Go'
|
||||||
elif data > 0.8 * 1024**2 :
|
elif data > 0.8 * 1024**2 :
|
||||||
|
@ -173,45 +179,74 @@ def tableau_ng(data,titres=None,largeurs=None,allignements=None,formats=None) :
|
||||||
else :
|
else :
|
||||||
return str(round(data,1))+'o'
|
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
|
# Largeurs
|
||||||
##########
|
##########
|
||||||
if not largeurs :
|
if not largeur :
|
||||||
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) ]
|
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 largeurs:
|
elif '*' in largeur:
|
||||||
rows, cols = get_screen_size()
|
rows, cols = get_screen_size()
|
||||||
for i in range(nbcols) :
|
for i in range(nbcols) :
|
||||||
if largeurs[i] in ['*',-1] :
|
if largeur[i] in ['*',-1] :
|
||||||
largeurs[i] = max(cols - sum([l for l in largeurs if l != '*']) - nbcols - 1, 3)
|
largeur[i] = max(cols - sum([l for l in largeurs if l != '*']) - nbcols - 1, 3)
|
||||||
break
|
break
|
||||||
print largeurs
|
|
||||||
|
|
||||||
# Allignement
|
# Alignement
|
||||||
#############
|
############
|
||||||
if not allignements :
|
if not alignement :
|
||||||
allignements = ['c'] * nbcols
|
alignement = ['c'] * nbcols
|
||||||
|
|
||||||
def alligne (data, allignement, largeur) :
|
def aligne (data, alignement, largeur) :
|
||||||
# Longeur sans les chaines de formatage
|
# Longeur sans les chaines de formatage
|
||||||
l = largeur - len(sre.sub('\x1b\[1;([0-9]|[0-9][0-9])m','',data))
|
l = 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
|
|
||||||
else :
|
|
||||||
return u' '*(l/2) + data + u' '*((l+1)/2)
|
|
||||||
|
|
||||||
data = [ [ alligne(ligne[i],allignements[i],largeurs[i]) for i in range(nbcols) ] for ligne in 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' '*((largeur-l)/2) + data + u' '*((largeur-l+1)/2)
|
||||||
|
|
||||||
|
data = [ [ aligne(ligne[i],alignement[i],largeur[i]) for i in range(nbcols) ] for ligne in data ]
|
||||||
|
|
||||||
# Le titre
|
# Le titre
|
||||||
##########
|
##########
|
||||||
if titres :
|
if titre :
|
||||||
# ligne de 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
|
# 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 :
|
else :
|
||||||
chaine = u''
|
chaine = u''
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import socket
|
||||||
import sys
|
import sys
|
||||||
from pyPgSQL import PgSQL
|
from pyPgSQL import PgSQL
|
||||||
sys.path.append('/usr/scripts/gestion/')
|
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) :
|
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 = []
|
select = []
|
||||||
largeurs = []
|
largeur = []
|
||||||
titres = []
|
titre = []
|
||||||
|
format = []
|
||||||
|
alignement = []
|
||||||
|
|
||||||
if 'ip_ext' in group :
|
if 'ip_ext' in group :
|
||||||
select.append('ip_ext')
|
select.append('ip_ext')
|
||||||
largeurs.append('*')
|
largeur.append(30)
|
||||||
titres.append('machine ext')
|
titre.append('machine ext')
|
||||||
|
format.append('s')
|
||||||
|
alignement.append('c')
|
||||||
|
|
||||||
if 'port_ext' in group :
|
if 'port_ext' in group :
|
||||||
select.append('port_ext')
|
select.append('port_ext')
|
||||||
largeurs.append(10)
|
largeur.append(10)
|
||||||
titres.append('port ext')
|
titre.append('port ext')
|
||||||
|
format.append('s')
|
||||||
|
alignement.append('d')
|
||||||
|
|
||||||
if 'port_crans' in group :
|
if 'port_crans' in group :
|
||||||
select.append('port_crans')
|
select.append('port_crans')
|
||||||
largeurs.append(10)
|
largeur.append(10)
|
||||||
titres.append('port crans')
|
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']
|
select += ['sum(download) as download','sum(upload) as upload']
|
||||||
largeurs += [8,8]
|
largeur += [10,10]
|
||||||
titres += ['download','upload']
|
titre += ['download','upload']
|
||||||
|
format += ['o','o']
|
||||||
|
alignement += ['d','d']
|
||||||
|
|
||||||
# requete dans la base
|
# requete dans la base
|
||||||
######################
|
######################
|
||||||
|
@ -69,7 +79,7 @@ def stats (ip_crans, group=['ip_ext','port_crans','port_ext'], upload_mini=0, li
|
||||||
col = select.index(champ)
|
col = select.index(champ)
|
||||||
results = [ x[:col] + [nom_de_machine(x[col])] + x[col+1:] for x in results ]
|
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__' :
|
if __name__ == '__main__' :
|
||||||
|
|
||||||
|
|
|
@ -16,10 +16,12 @@ import commands
|
||||||
import sys
|
import sys
|
||||||
from pyPgSQL import PgSQL
|
from pyPgSQL import PgSQL
|
||||||
sys.path.append('/usr/scripts/gestion')
|
sys.path.append('/usr/scripts/gestion')
|
||||||
|
sys.path.append('/usr/scripts/surveillance')
|
||||||
import config
|
import config
|
||||||
import smtplib
|
import smtplib
|
||||||
import socket
|
import socket
|
||||||
from analyse import stats
|
from analyse import stats
|
||||||
|
from affich_tools import tableau_ng
|
||||||
|
|
||||||
######################
|
######################
|
||||||
# Fonctions à la con #
|
# Fonctions à la con #
|
||||||
|
@ -50,34 +52,29 @@ mail = smtplib.SMTP('localhost')
|
||||||
|
|
||||||
# Adhérents avertis :
|
# 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)
|
curseur.execute(requete)
|
||||||
hard = curseur.fetchall()
|
liste_upload = tableau_ng( data = [ [l[1], l[2], socket.gethostbyaddr(str(l[0]))[0]] for l in curseur.fetchall() ],
|
||||||
liste_upload = '| upload | download | machine |\n'
|
titre = ['upload','download','machine'],
|
||||||
liste_upload += '|----------|----------|------------------------------|\n'
|
largeur = [10, 10, 30],
|
||||||
for IP,upload,download in hard:
|
format = ['o','o','s'],
|
||||||
hostname = socket.gethostbyaddr("%s"%IP)[0]
|
alignement = ['d','d','c']).encode('iso-8859-15')
|
||||||
liste_upload += '| %7so | %7so | %28s |\n' % (humanise(upload),humanise(download),hostname)
|
|
||||||
|
|
||||||
|
|
||||||
# Upload exemptés :
|
# 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)
|
curseur.execute(requete)
|
||||||
exemptes = curseur.fetchall()
|
liste_exemptes = tableau_ng( data = [ [l[1],l[2],socket.gethostbyaddr(str(l[0]))[0] ] for l in curseur.fetchall() ],
|
||||||
liste_exemptes = '| upload | download | machine |\n'
|
titre = ['upload','download','machine'],
|
||||||
liste_exemptes += '|----------|----------|------------------------------|\n'
|
largeur = [10, 10, 30],
|
||||||
for IP,upload,download in exemptes:
|
format = ['o','o','s'],
|
||||||
if upload < config.upload.soft :
|
alignement = ['d','d','c']).encode('iso-8859-15')
|
||||||
continue
|
|
||||||
hostname = socket.gethostbyaddr("%s"%IP)[0]
|
|
||||||
liste_exemptes += '| %7so | %7so | %28s |\n' % (humanise(upload),humanise(download),hostname)
|
|
||||||
|
|
||||||
|
|
||||||
# Upload des serveurs :
|
# Upload des serveurs :
|
||||||
#######################
|
#######################
|
||||||
liste_serveurs = '| upload | download | machine |\n'
|
liste_serveurs = []
|
||||||
liste_serveurs += '|----------|----------|------------------------------|\n'
|
|
||||||
for i in (10,1,3,4,7,8,9,11):
|
for i in (10,1,3,4,7,8,9,11):
|
||||||
IP='138.231.136.%d' % i
|
IP='138.231.136.%d' % i
|
||||||
hostname = socket.gethostbyaddr("%s"%IP)[0]
|
hostname = socket.gethostbyaddr("%s"%IP)[0]
|
||||||
|
@ -86,7 +83,13 @@ for i in (10,1,3,4,7,8,9,11):
|
||||||
traffic=curseur.fetchone()
|
traffic=curseur.fetchone()
|
||||||
if traffic == [None, None] :
|
if traffic == [None, None] :
|
||||||
continue
|
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
|
# statistiques des gros uploads depuis les serveurs
|
||||||
|
@ -116,27 +119,33 @@ for serveur in serveurs :
|
||||||
requete = "SELECT DISTINCT ip_crans FROM avertis_virus"
|
requete = "SELECT DISTINCT ip_crans FROM avertis_virus"
|
||||||
curseur.execute(requete)
|
curseur.execute(requete)
|
||||||
infections = [ x[0] for x in curseur.fetchall() ]
|
infections = [ x[0] for x in curseur.fetchall() ]
|
||||||
liste_virus=""
|
liste_virus=[]
|
||||||
for IP in infections:
|
for IP in infections:
|
||||||
try:
|
try:
|
||||||
hostname = socket.gethostbyaddr("%s"%IP)[0]
|
hostname = socket.gethostbyaddr("%s"%IP)[0]
|
||||||
except socket.herror:
|
except socket.herror:
|
||||||
continue
|
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 :
|
# Machines utilisant des protocoles P2P :
|
||||||
#########################################
|
#########################################
|
||||||
requete = "SELECT ip_crans,protocole FROM avertis_p2p WHERE date>timestamp 'now' - interval '1 day'"
|
requete = "SELECT ip_crans,protocole FROM avertis_p2p WHERE date>timestamp 'now' - interval '1 day'"
|
||||||
curseur.execute(requete)
|
curseur.execute(requete)
|
||||||
liste_p2p = '|protocole | machine | nombre |\n'
|
liste_p2p = []
|
||||||
liste_p2p += '|----------|------------------------------|----------|\n'
|
|
||||||
for IP, protocole in curseur.fetchall():
|
for IP, protocole in curseur.fetchall():
|
||||||
hostname = socket.gethostbyaddr(IP)[0]
|
hostname = socket.gethostbyaddr(IP)[0]
|
||||||
requete = "SELECT COUNT(ip_src) from p2p where date > timestamp 'now' - interval '1 day' and ip_src='%s'"%IP
|
requete = "SELECT COUNT(ip_src) from p2p where date > timestamp 'now' - interval '1 day' and ip_src='%s'"%IP
|
||||||
curseur.execute(requete)
|
curseur.execute(requete)
|
||||||
paquets=curseur.fetchone()[0]
|
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