Someone using my netsim-tools sent me an intriguing question: “Would it be possible to get network topology graphs out of the tool?”
I did something similar a long while ago for a simple network automation project (and numerous networking engineers built really interesting stuff while attending the Building Network Automation Solutions course), so it seemed like a no-brainer. As always, things aren’t as easy as they look.
Creating a data structure that describes the network as a graph of nodes and links is trivial; the hard part is auto-placement – how do you position the nodes to make the graph look decent. While there are several tools that can take graph description in JSON or YAML format and even use networking icons to draw switches and routers, I haven’t found one (in that category) that would be able to solve the placement problem, so I had go back to the granddaddy of graphing tools: graphviz.
I used graphviz earlier, and used Jinja2 templates to generate graph descriptions in DOT language. It was a royal pain. This time, I decided to do it with Python (much easier) and created a whole framework within netsim-tools in case you want to expand the functionality (and hopefully contribute it back to the project).
So far, netsim-tools can create two types of graphs:
- Topology (nodes and links), optionally clustering nodes into autonomous systems;
- BGP autonomous systems and sessions.
To create the graphs you have to:
- Install graphviz
- Run netsim-tools to create a graph description (.dot file)
- Run one of the graphviz commands to create a graph in desired output format (they support over a dozen formats)
- Enjoy the end results.
For example, I was using the network topology from one of my BGP examples to create a simple network topology graph:
netlab create -o graph dot graph.dot -T png -o netsim-graph-topo.png
- netlab create uses topology.yml as the default lab topology description and creates graph.dot as the default graph description. You can change both with CLI options.
- dot is one of the graphviz graph drawing commands. You have to specify the graph description (graph.dot), output format (png) and output file name.
Changing the graph type to BGP (by using graph:bgp CLI parameter with netlab create) creates a graph of IBGP and EBGP sessions:
netlab create -o graph:bgp dot graph.dot -T png -o netsim-graph-topo.png