->]'''
- missing=[]
- detected=[]
- for sw in Switch.objects.all():
- if(sw not in detected):
- missing.append(sw)
- for building in Building.objects.all():
- lignes.append(cluster.format(len(lignes),building))
- for switch in Switch.objects.filter(switchbay__building=building):
- lignes.append(node_fixe.format(switch.main_interface().domain.name,"Modèle",switch.model,"Nombre de ports",switch.number))
- for p in switch.ports.all().filter(related__isnull=False):
- lignes.append(node_ports.format(p.port,p.related.switch.main_interface().domain.name))
- lignes.append(end_table.format(building.id,switch.id))
- lignes.append("}")
- while(missing!=[]):
- lignes,new_detected=recursive_switchs(missing[0].ports.all().filter(related=None).first(),None,lignes,[missing[0]])
- missing=[i for i in missing if i not in new_detected]
- detected+=new_detected
- for switch in Switch.objects.all().filter(switchbay__isnull=True).exclude(ports__related__isnull=False):
- lignes.append(switch_alone.format(switch.id,switch.main_interface().domain.name))
- lignes.append("}")
- fichier = open("media/images/switchs.dot","w")
- for ligne in lignes:
- fichier.write(ligne+"\n")
- fichier.close()
- unflatten = Popen(["unflatten","-l", "3", "media/images/switchs.dot"], stdout=PIPE)
- image = Popen(["dot", "-Tpng", "-o", "media/images/switchs.png"], stdin=unflatten.stdout, stdout=PIPE)
+ dico = {
+ 'subs': [],
+ 'links' : [],
+ 'alone': [],
+ 'colors': {
+ 'head': "#7f0505", # Color parameters for the graph
+ 'back': "#b5adad",
+ 'texte': "#563d01",
+ 'border_bornes': "#02078e",
+ 'head_bornes': "#25771c",
+ 'head_server': "#1c3777"
+ }
+ }
+ missing = list(Switch.objects.all())
+ detected = []
+ for building in Building.objects.all(): # Visit all buildings
+
+ dico['subs'].append(
+ {
+ 'bat_id': building.id,
+ 'bat_name': building,
+ 'switchs': [],
+ 'bornes': [],
+ 'machines': []
+ }
+ )
+ # Visit all switchs in this building
+ for switch in Switch.objects.filter(switchbay__building=building):
+ dico['subs'][-1]['switchs'].append({
+ 'name': switch.main_interface().domain.name,
+ 'nombre': switch.number,
+ 'model': switch.model,
+ 'id': switch.id,
+ 'batiment': building,
+ 'ports': []
+ })
+ # visit all ports of this switch and add the switchs linked to it
+ for port in switch.ports.filter(related__isnull=False):
+ dico['subs'][-1]['switchs'][-1]['ports'].append({
+ 'numero': port.port,
+ 'related': port.related.switch.main_interface().domain.name
+ })
+
+ for ap in AccessPoint.all_ap_in(building):
+ 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
+ })
+ 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
+ })
+
+ # While the list of forgotten ones is not empty
+ while missing:
+ if missing[0].ports.count(): # The switch is not empty
+ links, new_detected = recursive_switchs(missing[0], None, [missing[0]])
+ for link in links:
+ dico['links'].append(link)
+ # Update the lists of missings and already detected switchs
+ missing=[i for i in missing if i not in new_detected]
+ detected += new_detected
+ else: # If the switch have no ports, don't explore it and hop to the next one
+ del missing[0]
+ # Switchs that are not connected or not in a building
+ for switch in Switch.objects.filter(switchbay__isnull=True).exclude(ports__related__isnull=False):
+ dico['alone'].append({
+ 'id': switch.id,
+ 'name': switch.main_interface().domain.name
+ })
-def recursive_switchs(port_start, switch_before, lignes,detected):
- """
- Parcour récursivement le switchs auquel appartient port_start pour trouver les ports suivants liés
- """
- l_ports=port_start.switch.ports.filter(related__isnull=False)
- for port in l_ports:
- if port.related.switch!=switch_before and port.related.switch!=port.switch:
- links=[]
- for sw in [switch for switch in [port_start.switch,port.related.switch]]:
- if(sw not in detected):
- detected.append(sw)
- if(sw.switchbay.building):
- links.append("\"{}_{}\"".format(sw.switchbay.building.id,sw.id))
- else:
- links.append("\"{}\"".format(sw.id))
- lignes.append(links[0]+" -> "+links[1])
- lignes, detected = recursive_switchs(port.related, port_start.switch, lignes, detected)
- return (lignes, detected)
+ dot_data=generate_dot(dico,'topologie/graph_switch.dot') # generate the dot file
+
+ f = tempfile.NamedTemporaryFile(mode='w+', encoding='utf-8', delete=False) # Create a temporary file to store the dot data
+ with f:
+ f.write(dot_data)
+ unflatten = Popen( # unflatten the graph to make it look better
+ ["unflatten","-l", "3", f.name],
+ stdout=PIPE
+ )
+ image = Popen( # pipe the result of the first command into the second
+ ["dot", "-Tpng", "-o", MEDIA_ROOT + "/images/switchs.png"],
+ stdin=unflatten.stdout,
+ stdout=PIPE
+ )
+
+def generate_dot(data,template):
+ """create the dot file
+ :param data: dictionary passed to the template
+ :param template: path to the dot template
+ :return: all the lines of the dot file"""
+ t = loader.get_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é."
+ "Cela peut mener à des erreurs de rendu."
+ "Vérifiez les paramètres")
+ c = Context(data).flatten()
+ dot = t.render(c)
+ return(dot)
+
+def recursive_switchs(switch_start, switch_before, detected):
+ """Visit the switch and travel to the switchs linked to it.
+ :param switch_start: the switch to begin the visit on
+ :param switch_before: the switch that you come from. None if switch_start is the first one
+ :param detected: list of all switchs already visited. None if switch_start is the first one
+ :return: A list of all the links found and a list of all the switchs visited"""
+ links_return=[] # list of dictionaries of the links to be detected
+ for port in switch_start.ports.filter(related__isnull=False): # Ports that are related to another switch
+ if port.related.switch != switch_before and port.related.switch != port.switch: # Not the switch that we come from, not the current switch
+ links = { # Dictionary of a link
+ 'depart':switch_start.id,
+ 'arrive':port.related.switch.id
+ }
+ if port.related.switch not in detected: # The switch at the end of this link has not been visited
+ links_down, detected = recursive_switchs(port.related.switch, switch_start, detected) # explore it and get the results
+ for link in links_down: # Add the non empty links to the current list
+ if link:
+ links_return.append(link)
+ links_return.append(links) # Add current and below levels links
+ detected.append(switch_start) # This switch is considered detected
+ return (links_return, detected)
+