def plot(network, show=False, save=True): """ Plot the network """ try: import matplotlib.pyplot as plt except ImportError: LOG.warn("Matplotlib not found, not plotting using Matplotlib") return try: import numpy except ImportError: LOG.warn("Matplotlib plotting requires numpy for graph layout") return plot_dir = config.plot_dir if not os.path.isdir(plot_dir): os.mkdir(plot_dir) graph = network.graph pos=nx.spring_layout(graph) # Different node color for each AS. Use heatmap based on ASN plot_graph(graph, title="Network", pos=pos, show=show, save=save, node_color=cmap_index(network, graph)) graph = ank.get_ebgp_graph(network) labels = dict( (n, network.label(n)) for n in graph) plot_graph(graph, title="eBGP", pos=pos, labels=labels, show=show, save=save) graph = ank.get_ibgp_graph(network) labels = dict( (n, network.label(n)) for n in graph) plot_graph(graph, title="iBGP", pos=pos, labels=labels, show=show, save=save) graph = ank.get_dns_graph(network) labels = dict( (n, network.label(n)) for n in graph) plot_graph(graph, title="DNS", pos=pos, labels=labels, show=show, save=save)
def plot(network, show=False, save=True): """ Plot the network """ try: import matplotlib.pyplot as plt except ImportError: LOG.warn("Matplotlib not found, not plotting using Matplotlib") return try: import numpy except ImportError: LOG.warn("Matplotlib plotting requires numpy for graph layout") return plot_dir = config.plot_dir if not os.path.isdir(plot_dir): os.mkdir(plot_dir) graph = network.graph try: #Extract co-ordinates to normalize (needed for PathDrawer, desired for neatness in plots) x, y = zip(*[(d['x'], d['y']) for n, d in network.graph.nodes(data=True)]) x = numpy.asarray(x, dtype=float) y = numpy.asarray(y, dtype=float) #TODO: combine these two operations together x -= x.min() x *= 1.0/x.max() y -= y.min() y *= -1.0/y.max() # invert y += 1 # rescale from 0->1 not 1->0 #TODO: see if can use reshape-type commands here co_ords = zip(list(x), list(y)) co_ords = [numpy.array([x, y]) for x, y in co_ords] nodes = [n for n in network.graph.nodes()] pos = dict( zip(nodes, co_ords)) except: pos=nx.spring_layout(graph) # Different node color for each AS. Use heatmap based on ASN paths = [] #paths.append( nx.shortest_path(network.graph, network.find("1a.AS1"), network.find("1c.AS1"))) #paths.append( nx.shortest_path(network.graph, network.find("1b.AS1"), network.find("1c.AS1"))) #paths.append(nx.shortest_path(network.graph, network.find("1a.AS1"), network.find("2c.AS2"))) #paths.append( nx.shortest_path(network.graph, network.find("as100r3.AS100"), network.find("as300r1.AS300"))) #paths.append(nx.shortest_path(network.graph, network.find("as100r2.AS100"), network.find("as30r1.AS30"))) #Node colors legend = { 'shapes': [], 'labels': [], } colormap = cm.jet unique_asn = sorted(list(set(d.asn for d in network.devices()))) asn_norm = colors.normalize(0, len(unique_asn)) asn_colors = dict.fromkeys(unique_asn) for index, asn in enumerate(asn_colors.keys()): asn_color = colormap(asn_norm(index)) #allocate based on index position asn_colors[asn] = asn_color legend['shapes'].append( plt.Rectangle((0, 0), 0.51, 0.51, fc = asn_color)) legend['labels'].append( asn) node_colors = [asn_colors[device.asn] for device in network.devices()] plot_graph(graph, title="Network", pos=pos, show=show, save=save, node_color=node_colors) plot_graph(graph, title="Paths", pos=pos, show=show, save=save, legend_data = legend, paths = paths, node_color=node_colors) graph = ank.get_ebgp_graph(network) labels = dict( (n, network.label(n)) for n in graph) plot_graph(graph, title="eBGP", pos=pos, labels=labels, show=show, save=save) graph = ank.get_ibgp_graph(network) labels = dict( (n, network.label(n)) for n in graph) plot_graph(graph, title="iBGP", pos=pos, labels=labels, show=show, save=save) graph = ank.get_dns_graph(network) labels = dict( (n, network.label(n)) for n in graph) plot_graph(graph, title="DNS", pos=pos, labels=labels, show=show, save=save)
def jsplot(network): """ Plot the network """ plot_dir = config.plot_dir if not os.path.isdir(plot_dir): os.mkdir(plot_dir) jsplot_dir = os.path.join(plot_dir, "jsplot") if not os.path.isdir(jsplot_dir): os.mkdir(jsplot_dir) js_template = lookup.get_template("arborjs/main_js.mako") css_template = lookup.get_template("arborjs/style_css.mako") html_template = lookup.get_template("arborjs/index_html.mako") ank_css_template = lookup.get_template("autonetkit/style_css.mako") #TODO: tidy these up with the embedded network in node name node_list = ( (node.fqdn, {'label': network.fqdn(node)}) for node in network.graph.nodes()) edge_list = list( (src.fqdn, dst.fqdn, {}) for (src, dst, data) in network.graph.edges(data=True)) js_files = [] timestamp = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()) js_filename = os.path.join(jsplot_dir, "main.js") js_files.append("main.js") with open( js_filename, 'w') as f_js: f_js.write( js_template.render( node_list = node_list, edge_list = edge_list, physical_graph = True, )) js_filename = os.path.join(jsplot_dir, "ip.js") js_files.append("ip.js") node_list = [] for node in network.graph.nodes(): # Set label to be FQDN, so don't have multiple "Router A" nodes etc data = { 'label': "%s %s" % (ank.fqdn(network, node), network.lo_ip(node).ip)} node_list.append( (node.fqdn, data)) edge_list = list( (src.fqdn, dst.fqdn, data['sn']) for (src, dst, data) in network.graph.edges(data=True)) with open( js_filename, 'w') as f_js: f_js.write( js_template.render( node_list = node_list, edge_list = edge_list, physical_graph = True, )) js_filename = os.path.join(jsplot_dir, "igp.js") js_files.append("igp.js") node_list = [] for node in network.graph.nodes(): if not node.igp_link_count: # no IGP links, don't add to plot continue # Set label to be FQDN, so don't have multiple "Router A" nodes etc data = { 'label': "%s" % node.fqdn} node_list.append( (node.fqdn, data)) edge_list = list( (src.fqdn, dst.fqdn, data.get('weight')) for (src, dst, data) in network.graph.edges(data=True) if src.asn == dst.asn) with open( js_filename, 'w') as f_js: f_js.write( js_template.render( node_list = node_list, edge_list = edge_list, physical_graph = True, )) #TODO: work out how to do multiple on one page ebgp_graph = ank.get_ebgp_graph(network) labels = dict( (n, network.label(n)) for n in ebgp_graph) nx.relabel_nodes(ebgp_graph, labels, copy=False) ebgp_filename = os.path.join(jsplot_dir, "ebgp.js") js_files.append("ebgp.js") with open( ebgp_filename, 'w') as f_js: f_js.write( js_template.render( node_list = ebgp_graph.nodes(data=True), edge_list = ebgp_graph.edges(data=True), overlay_graph = True, )) ibgp_graph = ank.get_ibgp_graph(network) node_list = [] for node in ibgp_graph.nodes(): # Set label to be FQDN, so don't have multiple "Router A" nodes etc data = { 'label': "%s (%s)" % (node.fqdn, network.graph.node[node].get("ibgp_level"))} node_list.append( (node.fqdn, data)) edge_list = ibgp_graph.edges(data=True) edge_list = list( (src.fqdn, dst.fqdn, data.get("rr_dir")) for (src, dst, data) in edge_list) ibgp_filename = os.path.join(jsplot_dir, "ibgp.js") js_files.append("ibgp.js") with open( ibgp_filename, 'w') as f_js: f_js.write( js_template.render( node_list = node_list, edge_list = edge_list, physical_graph = True, )) #TODO: clarify difference of physical_graph and overlay_graph #TODO: see if js_files ever used dns_graph = ank.get_dns_graph(network) node_list = [] for node in dns_graph.nodes(): # Set label to be FQDN, so don't have multiple "Router A" nodes etc data = { 'label': "%s (%s)" % (node.fqdn, dns_graph.node[node].get("level"))} node_list.append( (node.fqdn, data)) dns_filename = os.path.join(jsplot_dir, "dns.js") edge_list = dns_graph.edges(data=True) #edge_list = list( (src.fqdn, dst.fqdn, data.get('dns_dir')) for (src, dst, data) in edge_list) edge_list = list( (src.fqdn, dst.fqdn, '') for (src, dst, data) in edge_list) js_files.append("dns.js") with open( dns_filename, 'w') as f_js: f_js.write( js_template.render( node_list = node_list, edge_list = edge_list, physical_graph = True, )) dns_auth_graph = ank.get_dns_auth_graph(network) node_list = [] for node in dns_auth_graph.nodes(): # Set label to be FQDN, so don't have multiple "Router A" nodes etc data = { 'label': "%s" % node.label} node_list.append( (node.fqdn, data)) dns_filename = os.path.join(jsplot_dir, "dns_auth.js") edge_list = dns_auth_graph.edges(data=True) edge_list = list( (src.fqdn, dst.fqdn, data) for (src, dst, data) in edge_list) js_files.append("dns_auth.js") with open( dns_filename, 'w') as f_js: f_js.write( js_template.render( node_list = node_list, edge_list = edge_list, physical_graph = True, )) #TODO: add timestamps to plots # put html file in main plot directory #TODO: parameterised/variable the css location plot_width = config.settings['Plotting']['jsplot width'] plot_height = config.settings['Plotting']['jsplot height'] html_filename = os.path.join(plot_dir, "plot.html") with open( html_filename, 'w') as f_html: f_html.write( html_template.render( js_file = "main.js", timestamp=timestamp, plot_width = plot_width, plot_height = plot_height, title = "Physical Network", css_filename = "./ank_style.css",)) html_filename = os.path.join(plot_dir, "ip.html") with open( html_filename, 'w') as f_html: f_html.write( html_template.render( js_file = "ip.js", timestamp=timestamp, plot_width = plot_width, plot_height = plot_height, title = "IP", css_filename = "./ank_style.css",)) html_filename = os.path.join(plot_dir, "igp.html") with open( html_filename, 'w') as f_html: f_html.write( html_template.render( js_file = "igp.js", timestamp=timestamp, plot_width = plot_width, plot_height = plot_height, title = "IGP", css_filename = "./ank_style.css",)) html_filename = os.path.join(plot_dir, "ibgp.html") with open( html_filename, 'w') as f_html: f_html.write( html_template.render( js_file = "ibgp.js", timestamp=timestamp, plot_width = plot_width, plot_height = plot_height, title = "iBGP", css_filename = "./ank_style.css",)) html_filename = os.path.join(plot_dir, "ebgp.html") with open( html_filename, 'w') as f_html: f_html.write( html_template.render( js_file = "ebgp.js", timestamp=timestamp, plot_width = plot_width, plot_height = plot_height, title = "eBGP", css_filename = "./ank_style.css",)) html_filename = os.path.join(plot_dir, "dns.html") with open( html_filename, 'w') as f_html: f_html.write( html_template.render( js_file = "dns.js", timestamp=timestamp, plot_width = plot_width, plot_height = plot_height, title = "DNS Hierarchy", css_filename = "./ank_style.css",)) html_filename = os.path.join(plot_dir, "dns_auth.html") with open( html_filename, 'w') as f_html: f_html.write( html_template.render( js_file = "dns_auth.js", timestamp=timestamp, plot_width = plot_width, plot_height = plot_height, title = "DNS Authority", css_filename = "./ank_style.css",)) css_filename = os.path.join(jsplot_dir, "style.css") with open( css_filename, 'w') as f_css: f_css.write( css_template.render()) # and ank css_template css_filename = os.path.join(plot_dir, "ank_style.css") with open( css_filename, 'w') as f_css: f_css.write( ank_css_template.render()) arbor_js_src_filename = os.path.join(template_dir, "arborjs", "arbor.js") arbor_js_dst_filename = os.path.join(jsplot_dir, "arbor.js") shutil.copy(arbor_js_src_filename, arbor_js_dst_filename)