@ -75,7 +75,8 @@ from .models import (
ConstructorSwitch ,
ConstructorSwitch ,
AccessPoint ,
AccessPoint ,
SwitchBay ,
SwitchBay ,
Building
Building ,
Server
)
)
from . forms import (
from . forms import (
EditPortForm ,
EditPortForm ,
@ -116,6 +117,9 @@ def index(request):
)
)
pagination_number = GeneralOption . get_cached_value ( ' pagination_number ' )
pagination_number = GeneralOption . get_cached_value ( ' pagination_number ' )
switch_list = re2o_paginator ( request , switch_list , pagination_number )
switch_list = re2o_paginator ( request , switch_list , pagination_number )
# make_machine_graph()
return render (
return render (
request ,
request ,
' topologie/index.html ' ,
' topologie/index.html ' ,
@ -944,45 +948,36 @@ def make_machine_graph():
"""
"""
Crée le fichier dot et l ' image du graph des Switchs
Crée le fichier dot et l ' image du graph des Switchs
"""
"""
dico = { ' subs ' : [ ] , ' links ' : [ ] , ' alone ' : [ ] , ' bornes ' : [ ] }
dico = { ' subs ' : [ ] , ' links ' : [ ] , ' alone ' : [ ] , ' colors ' : { ' head ' : " #7f0505 " , ' back ' : " #b5adad " , ' texte ' : " #563d01 " , ' border_ bornes' : " #02078e " , ' head_bornes ' : " #25771c " , ' head_server ' : " #1c3777 " } }
missing = [ ]
missing = [ ]
detected = [ ]
detected = [ ]
for sw in Switch . objects . all ( ) :
for sw in Switch . objects . all ( ) :
if ( sw not in detected ) :
if ( sw not in detected ) :
missing . append ( sw )
missing . append ( sw )
for building in Building . objects . all ( ) : #Parcour tous les batiments
for building in Building . objects . all ( ) : #Parcour tous les batiments
dico [ ' subs ' ] . append ( { ' bat_id ' : building . id , ' bat_name ' : building , ' switchs ' : [ ] , ' bornes ' : [ ] , ' machines ' : [ ] } )
dico [ ' subs ' ] . append ( { ' bat_id ' : building . id , ' bat_name ' : building , ' switchs ' : [ ] , ' bornes ' : [ ] } )
for switch in Switch . objects . filter ( switchbay__building = building ) : #Parcour tous les switchs de ce batiment
for switch in Switch . objects . filter ( switchbay__building = building ) : #Parcour tous les switchs de ce batiment
dico [ ' subs ' ] [ - 1 ] [ ' switchs ' ] . append ( { ' name ' : switch . main_interface ( ) . domain . name , ' nombre ' : switch . number , ' model ' : switch . model , ' id ' : switch . id , ' batiment ' : building , ' ports ' : [ ] } )
dico [ ' subs ' ] [ - 1 ] [ ' switchs ' ] . append ( { ' name ' : switch . main_interface ( ) . domain . name , ' nombre ' : switch . number , ' model ' : switch . model , ' id ' : switch . id , ' batiment ' : building , ' ports ' : [ ] } )
for p in switch . ports . all ( ) . filter ( related__isnull = False ) : #Parcour tout les ports liés de ce switch
for p in switch . ports . all ( ) . filter ( related__isnull = False ) : #Parcour tout les ports liés de ce switch
dico [ ' subs ' ] [ - 1 ] [ ' switchs ' ] [ - 1 ] [ ' ports ' ] . append ( { ' numero ' : p . port , ' related ' : p . related . switch . main_interface ( ) . domain . name } )
dico [ ' subs ' ] [ - 1 ] [ ' switchs ' ] [ - 1 ] [ ' ports ' ] . append ( { ' numero ' : p . port , ' related ' : p . related . switch . main_interface ( ) . domain . name } )
for ap in AccessPoint . all_ap_in ( building ) :
for ap in AccessPoint . all_ap_in ( building ) :
dico [ ' subs ' ] [ - 1 ] [ ' bornes ' ] . append ( { ' name ' : ap . short_name , ' id ' : ap . id } )
dico [ ' subs ' ] [ - 1 ] [ ' bornes ' ] . append ( { ' name ' : ap . short_name , ' switch ' : ap . switch ( ) [ 0 ] . main_interface ( ) . domain . name , ' port ' : ap . switch ( ) [ 0 ] . ports . filter ( machine_interface__machine = ap ) [ 0 ] . port } )
dico [ ' links ' ] . append ( { ' depart ' : ap . switch ( ) [ 0 ] . id , ' arrive ' : ap . id } )
for server in Server . all_server_in ( building ) :
dico [ ' subs ' ] [ - 1 ] [ ' machines ' ] . append ( { ' name ' : server . short_name , ' switch ' : server . switch ( ) [ 0 ] . main_interface ( ) . domain . name , ' port ' : Port . objects . filter ( machine_interface__machine = server ) [ 0 ] . port } )
# for ma in Allmachines_in(building):
# dico['subs'][-1]['machines'].append({'name':ma.short_name,'id':ma.id,'switch':ma.switch()[0].main_interface().domain.name,'port':ma.switch()[0].ports.filter(machine_interface__machine=ap)[0].port})
for ap in AccessPoint . objects . all ( ) :
dico [ ' bornes ' ] . append ( { ' name ' : str ( ap ) } )
while ( missing != [ ] ) : #Tant que la liste des oubliés n'est pas vide i.e on les a pas tous passer
while ( missing != [ ] ) : #Tant que la liste des oubliés n'est pas vide i.e on les a pas tous passer
links , new_detected = recursive_switchs ( missing [ 0 ] . ports . all ( ) . filter ( related = None ) . first ( ) , None , [ missing [ 0 ] ] )
links , new_detected = recursive_switchs ( missing [ 0 ] . ports . all ( ) . filter ( related = None ) . first ( ) , None , [ missing [ 0 ] ] )
for link in links :
for link in links :
dico [ ' links ' ] . append ( link )
dico [ ' links ' ] . append ( link )
missing = [ i for i in missing if i not in new_detected ] #on recrée la liste des oubliés et des detectés
#on recrée la liste des oubliés et des detectés
missing = [ i for i in missing if i not in new_detected ]
detected + = new_detected
detected + = new_detected
for switch in Switch . objects . all ( ) . filter ( switchbay__isnull = True ) . exclude ( ports__related__isnull = False ) : #Tous ceux qui ne sont ni connectés ni dans un batiment
for switch in Switch . objects . all ( ) . filter ( switchbay__isnull = True ) . exclude ( ports__related__isnull = False ) : #Tous ceux qui ne sont ni connectés ni dans un batiment
dico [ ' alone ' ] . append ( { ' id ' : switch . id , ' name ' : switch . main_interface ( ) . domain . name } )
dico [ ' alone ' ] . append ( { ' id ' : switch . id , ' name ' : switch . main_interface ( ) . domain . name } )
dot_data = generate_image ( dico )
dot_data = generate_image ( dico )
fichier = open ( " media/images/switchs.dot " , " w " )
fichier = open ( " media/images/switchs.dot " , " w " )
fichier . write ( dot_data )
fichier . write ( dot_data )
@ -991,18 +986,13 @@ def make_machine_graph():
image = Popen ( [ " dot " , " -Tpng " , " -o " , " media/images/switchs.png " ] , stdin = unflatten . stdout , stdout = PIPE )
image = Popen ( [ " dot " , " -Tpng " , " -o " , " media/images/switchs.png " ] , stdin = unflatten . stdout , stdout = PIPE )
def generate_image ( data , template = ' topologie/graph_switch.dot ' ) :
def generate_image ( data , template = ' topologie/graph_switch.dot ' ) :
t = loader . get_template ( template )
t = loader . get_template ( template )
if not isinstance ( t , Template ) and not ( hasattr ( t , ' template ' ) and isinstance ( t . template , Template ) ) :
if not isinstance ( t , Template ) and not ( hasattr ( t , ' template ' ) and isinstance ( t . template , Template ) ) :
raise Exception ( " Le template par défaut de Django n ' est pas utilisé. "
raise Exception ( " Le template par défaut de Django n ' est pas utilisé. "
" Cela peut mener à des erreurs de rendu. "
" Cela peut mener à des erreurs de rendu. "
" Vérifiez les paramètres " )
" Vérifiez les paramètres " )
c = Context ( data ) . flatten ( )
c = Context ( data ) . flatten ( )
dot = t . render ( c )
dot = t . render ( c )
return ( dot )
return ( dot )
def recursive_switchs ( port_start , switch_before , detected ) :
def recursive_switchs ( port_start , switch_before , detected ) :
@ -1010,23 +1000,16 @@ def recursive_switchs(port_start, switch_before, detected):
Parcour récursivement le switchs auquel appartient port_start pour trouver les ports suivants liés
Parcour récursivement le switchs auquel appartient port_start pour trouver les ports suivants liés
"""
"""
links_return = [ ] #Liste de dictionaires qui stockes les nouveaux liens détéctés
links_return = [ ] #Liste de dictionaires qui stockes les nouveaux liens détéctés
l_ports = port_start . switch . ports . filter ( related__isnull = False ) #Liste des ports dont le related est non null
l_ports = port_start . switch . ports . filter ( related__isnull = False ) #Liste des ports dont le related est non null
for port in l_ports :
for port in l_ports :
if port . related . switch != switch_before and port . related . switch != port . switch : #Pas le switch dont on vient, pas le switch actuel
if port . related . switch != switch_before and port . related . switch != port . switch : #Pas le switch dont on vient, pas le switch actuel
detected . append ( port_start . switch )
detected . append ( port_start . switch )
detected . append ( port . related . switch )
detected . append ( port . related . switch )
links = { ' depart ' : port_start . switch . id , ' arrive ' : port . related . switch . id }
links = { ' depart ' : port_start . switch . id , ' arrive ' : port . related . switch . id }
links_down , detected = recursive_switchs ( port . related , port_start . switch , detected )
links_down , detected = recursive_switchs ( port . related , port_start . switch , detected )
links_return . append ( links )
links_return . append ( links )
for link in links_down :
for link in links_down :
if ( link != [ ] ) :
if ( link != [ ] ) :
links_return . append ( link )
links_return . append ( link )
return ( links_return , detected )
return ( links_return , detected )