docker-network-graph/docker-net-graph.py

81 lines
2.1 KiB
Python
Raw Normal View History

2016-09-29 23:48:22 +02:00
from docker import Client
import os
import pprint
import json
from graphviz import Graph
dot = Graph(comment='Docker Network Graph',
graph_attr=dict( rankdir="TB", packmode='graph', pack='true')
2016-09-29 23:48:22 +02:00
)
docker_client = Client(os.environ.get("DOCKER_HOST", "unix:///var/run/docker.sock"))
def dump_json(obj):
2018-08-31 03:34:04 +02:00
print(json.dumps(obj, indent=4))
2016-09-29 23:48:22 +02:00
for c in sorted(docker_client.containers()):
name = c['Names'][0]
container_id = c['Id']
node_id = 'container_%s' % container_id
iface_labels = []
2018-08-31 03:34:04 +02:00
for net_name, net_info in c['NetworkSettings']['Networks'].items():
label_iface = "<%s> %s" % (net_info['EndpointID'], net_info['IPAddress'])
iface_labels.append(label_iface)
2018-08-31 03:34:04 +02:00
print('|'.join(iface_labels))
dot.node(node_id,
shape='record',
label="{ %s | { %s } }" % (name, '|'.join(iface_labels)),
fillcolor='#ff9999',
style='filled')
2018-08-31 03:34:04 +02:00
for net in docker_client.networks():
2016-09-29 23:48:22 +02:00
net_name = net['Name']
try:
gateway = net['IPAM']['Config'][0]['Gateway']
except IndexError:
gateway = None
try:
subnet = net['IPAM']['Config'][0]['Subnet']
except IndexError:
subnet = None
2018-08-31 03:34:04 +02:00
print("Network: %s %s gw:%s" % ( net_name, subnet,gateway))
2016-09-29 23:48:22 +02:00
net_node_id = "net_%s" % (net_name,)
net_label_html = '<br/>'.join([s for s in ['<font color="#777777"><i>network</i></font>', net_name, subnet, gateway] if s != None])
2016-09-29 23:48:22 +02:00
dot.node(net_node_id,
shape='record',
label="{<gw_iface> %s| %s }" % (gateway, net_name),
fillcolor='#99ff99',
style='filled')
2016-09-29 23:48:22 +02:00
2018-08-31 03:34:04 +02:00
for container_id, container in sorted(net['Containers'].items()):
dump_json(container)
2018-08-31 03:34:04 +02:00
print(" * ", container['Name'], container['IPv4Address'], container['IPv6Address'])
2016-09-29 23:48:22 +02:00
container_node_id = 'container_%s' % container_id
container_iface_ref = "%s:%s" % (container_node_id, container['EndpointID'])
2016-09-29 23:48:22 +02:00
dot.edge(container_iface_ref, net_node_id+":gw_iface")
2016-09-29 23:48:22 +02:00
2018-08-31 03:34:04 +02:00
print(dot.source)
2016-09-29 23:48:22 +02:00
dot.render('dng.gv')