From de1d39175adec93f87e1f119d56cd90ca97e4fa5 Mon Sep 17 00:00:00 2001 From: Eugene Agafonov Date: Fri, 30 Sep 2016 01:58:34 +0300 Subject: [PATCH] record-style output in color http://graphviz.org/doc/info/shapes.html#record --- docker-net-graph.py | 56 +++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/docker-net-graph.py b/docker-net-graph.py index 1c69c1a..0e783a0 100644 --- a/docker-net-graph.py +++ b/docker-net-graph.py @@ -7,15 +7,38 @@ import json from graphviz import Graph dot = Graph(comment='Docker Network Graph', - graph_attr=dict( rankdir="LR") + graph_attr=dict( rankdir="TB", packmode='graph', pack='true') ) docker_client = Client(os.environ.get("DOCKER_HOST", "unix:///var/run/docker.sock")) -#pprint.pprint(docker_client.networks()) def dump_json(obj): print json.dumps(obj, indent=4) +for c in sorted(docker_client.containers()): + name = c['Names'][0] + container_id = c['Id'] + + node_id = 'container_%s' % container_id + + iface_labels = [] + + for net_name, net_info in c['NetworkSettings']['Networks'].iteritems(): + label_iface = "<%s> %s" % (net_info['EndpointID'], net_info['IPAddress']) + + iface_labels.append(label_iface) + + print '|'.join(iface_labels) + + + dot.node(node_id, + shape='record', + label="{ %s | { %s } }" % (name, '|'.join(iface_labels)), + fillcolor='#ff9999', + style='filled') + + + for net in sorted(docker_client.networks()): #print "Net" net_name = net['Name'] @@ -38,35 +61,24 @@ for net in sorted(docker_client.networks()): net_node_id = "net_%s" % (net_name,) - net_label_html = '
'.join([s for s in ['network', net_name, subnet, gateway] if s != None]) + net_label_html = '
'.join([s for s in ['network', net_name, subnet, gateway] if s != None]) - dot.node(net_node_id, label="<%s>" % net_label_html, shape='note') + dot.node(net_node_id, + shape='record', + label="{ %s| %s }" % (gateway, net_name), + fillcolor='#99ff99', + style='filled') for container_id, container in sorted(net['Containers'].iteritems()): + dump_json(container) print " * ", container['Name'], container['IPv4Address'], container['IPv6Address'] container_node_id = 'container_%s' % container_id - label_html = "container
%s" % container['Name'] - - dot.node(container_node_id, - label="<%s>" % label_html, - shape='component') - - label_html = "%s
%s" % (container['IPv4Address'].split('/')[0], container['IPv6Address']) - - container_iface_id = "iface_%s" % container['IPv4Address'] + container_iface_ref = "%s:%s" % (container_node_id, container['EndpointID']) - dot.node(container_iface_id, - label="<%s>" % label_html, - shape='box') - - - - - dot.edge(container_node_id, container_iface_id) - dot.edge(container_iface_id, net_node_id) + dot.edge(container_iface_ref, net_node_id+":gw_iface") print dot.source dot.render('dng.gv')