From 98b38d11ac6bc75da5c5d69db7c5ca57a375a233 Mon Sep 17 00:00:00 2001 From: LeoVerto Date: Fri, 31 Aug 2018 06:21:48 +0200 Subject: [PATCH] Use fdp with splines and random network colours --- .gitignore | 1 + docker-net-graph.py | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index a89052c..b4c3f56 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.gv *.pdf +*.png .idea diff --git a/docker-net-graph.py b/docker-net-graph.py index 3ccca07..961fd18 100755 --- a/docker-net-graph.py +++ b/docker-net-graph.py @@ -2,14 +2,13 @@ import os import json import argparse +import random from docker import Client from graphviz import Graph def generate_graph(verbose: bool, file: str): - dot = Graph(comment='Docker Network Graph', - graph_attr=dict(rankdir="TB", packmode='graph', pack='true') - ) + g = Graph(comment='Docker Network Graph', engine="fdp", format='png', graph_attr=dict(splines="true")) docker_client = Client(os.environ.get("DOCKER_HOST", "unix:///var/run/docker.sock")) @@ -32,14 +31,15 @@ def generate_graph(verbose: bool, file: str): if verbose: print('|'.join(iface_labels)) - dot.node(node_id, - shape='record', - label="{ %s | { %s } }" % (name, '|'.join(iface_labels)), - fillcolor='#ff9999', - style='filled') + g.node(node_id, + shape='record', + label="{ %s | { %s } }" % (name, '|'.join(iface_labels)), + fillcolor='#ff9999', + style='filled') for net in docker_client.networks(): net_name = net['Name'] + color = "#%06x" % random.randint(0, 0xFFFFFF) try: gateway = net['IPAM']['Config'][0]['Gateway'] @@ -58,11 +58,11 @@ def generate_graph(verbose: bool, file: str): net_label_html = '
'.join([s for s in ['network', net_name, subnet, gateway] if s is not None]) - dot.node(net_node_id, - shape='record', - label="{ %s| %s }" % (gateway, net_name), - fillcolor='#99ff99', - style='filled') + g.node(net_node_id, + shape='record', + label="{ %s| %s }" % (gateway, net_name), + fillcolor=color, + style='filled') for container_id, container in sorted(net['Containers'].items()): if verbose: @@ -74,11 +74,11 @@ def generate_graph(verbose: bool, file: str): container_iface_ref = "%s:%s" % (container_node_id, container['EndpointID']) - dot.edge(container_iface_ref, net_node_id+":gw_iface") + g.edge(container_iface_ref, net_node_id+":gw_iface", color=color) - print(dot.source) + print(g.source) if file: - dot.render(file) + g.render(file) if __name__ == "__main__":