2018-08-31 04:50:58 +02:00
|
|
|
#!/usr/bin/python3
|
2018-11-27 07:22:25 +01:00
|
|
|
|
2016-09-29 23:48:22 +02:00
|
|
|
import os
|
2018-08-31 04:50:58 +02:00
|
|
|
import argparse
|
2018-08-31 06:21:48 +02:00
|
|
|
import random
|
2018-11-27 07:22:25 +01:00
|
|
|
import docker
|
|
|
|
import typing
|
|
|
|
from dataclasses import dataclass
|
2016-09-29 23:48:22 +02:00
|
|
|
from graphviz import Graph
|
2018-11-27 07:22:25 +01:00
|
|
|
from graphviz.backend import FORMATS
|
2016-09-29 23:48:22 +02:00
|
|
|
|
2018-08-31 13:07:32 +02:00
|
|
|
# colorlover.scales["12"]["qual"]["Paired"] converted to hex strings
|
2023-06-17 03:08:59 +02:00
|
|
|
COLORS = [
|
|
|
|
"#1f78b4",
|
|
|
|
"#33a02c",
|
|
|
|
"#e31a1c",
|
|
|
|
"#ff7f00",
|
|
|
|
"#6a3d9a",
|
|
|
|
"#b15928",
|
|
|
|
"#a6cee3",
|
|
|
|
"#b2df8a",
|
|
|
|
"#fdbf6f",
|
|
|
|
"#cab2d6",
|
|
|
|
"#ffff99",
|
|
|
|
]
|
2018-08-31 13:07:32 +02:00
|
|
|
i = 0
|
|
|
|
|
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
@dataclass
|
|
|
|
class Network:
|
|
|
|
name: str
|
|
|
|
gateway: str
|
|
|
|
internal: bool
|
|
|
|
isolated: bool
|
|
|
|
color: str
|
2018-08-31 13:07:32 +02:00
|
|
|
|
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
@dataclass
|
|
|
|
class Interface:
|
|
|
|
endpoint_id: str
|
|
|
|
address: str
|
2020-06-05 01:01:30 +02:00
|
|
|
aliases: typing.List[str]
|
2018-08-31 04:50:58 +02:00
|
|
|
|
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
@dataclass
|
|
|
|
class Container:
|
|
|
|
container_id: str
|
|
|
|
name: str
|
|
|
|
interfaces: typing.List[Interface]
|
2018-08-31 04:50:58 +02:00
|
|
|
|
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
@dataclass
|
|
|
|
class Link:
|
|
|
|
container_id: str
|
|
|
|
endpoint_id: str
|
|
|
|
network_name: str
|
2018-08-31 04:50:58 +02:00
|
|
|
|
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
def get_unique_color() -> str:
|
|
|
|
global i
|
2018-08-31 04:50:58 +02:00
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
if i < len(COLORS):
|
|
|
|
c = COLORS[i]
|
|
|
|
i += 1
|
|
|
|
else:
|
|
|
|
# Generate random color if we've already used the 12 preset ones
|
2023-06-17 03:13:04 +02:00
|
|
|
c = '#'.join([f"{random.randint(0, 255):02x}" for _ in range(3)])
|
2018-08-31 04:50:58 +02:00
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
return c
|
2018-08-31 04:50:58 +02:00
|
|
|
|
|
|
|
|
2023-06-17 03:08:59 +02:00
|
|
|
def get_networks(
|
|
|
|
client: docker.DockerClient, verbose: bool
|
|
|
|
) -> typing.Dict[str, Network]:
|
2018-11-27 07:22:25 +01:00
|
|
|
networks: typing.Dict[str, Network] = {}
|
2018-08-31 04:50:58 +02:00
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
for net in sorted(client.networks.list(), key=lambda k: k.name):
|
2018-08-31 04:50:58 +02:00
|
|
|
try:
|
2020-07-01 15:46:24 +02:00
|
|
|
gateway = net.attrs["IPAM"]["Config"][0]["Subnet"]
|
2018-09-14 18:07:24 +02:00
|
|
|
except (KeyError, IndexError):
|
2018-09-10 18:45:36 +02:00
|
|
|
# This network doesn't seem to be used, skip it
|
|
|
|
continue
|
2018-08-31 04:50:58 +02:00
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
internal = False
|
2018-08-31 04:50:58 +02:00
|
|
|
try:
|
2018-11-27 07:22:25 +01:00
|
|
|
if net.attrs["Internal"]:
|
|
|
|
internal = True
|
2018-09-10 18:45:36 +02:00
|
|
|
except KeyError:
|
2018-08-31 21:34:24 +02:00
|
|
|
pass
|
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
isolated = False
|
2018-08-31 21:34:24 +02:00
|
|
|
try:
|
2018-11-27 07:22:25 +01:00
|
|
|
if net.attrs["Options"]["com.docker.network.bridge.enable_icc"] == "false":
|
|
|
|
isolated = True
|
2018-08-31 21:34:24 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2018-08-31 04:50:58 +02:00
|
|
|
|
|
|
|
if verbose:
|
2023-06-17 03:08:59 +02:00
|
|
|
print(
|
|
|
|
f"Network: {net.name} {'internal' if internal else ''} {'isolated' if isolated else ''} gw:{gateway}"
|
|
|
|
)
|
2018-08-31 04:50:58 +02:00
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
color = get_unique_color()
|
|
|
|
networks[net.name] = Network(net.name, gateway, internal, isolated, color)
|
|
|
|
|
2020-06-04 23:24:50 +02:00
|
|
|
networks["host"] = Network("host", "0.0.0.0", False, False, "#808080")
|
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
return networks
|
2018-08-31 04:50:58 +02:00
|
|
|
|
|
|
|
|
2023-06-17 03:08:59 +02:00
|
|
|
def get_containers(
|
|
|
|
client: docker.DockerClient, verbose: bool
|
|
|
|
) -> (typing.List[Container], typing.List[Link]):
|
2018-11-27 07:22:25 +01:00
|
|
|
containers: typing.List[Container] = []
|
|
|
|
links: typing.List[Link] = []
|
2018-08-31 04:50:58 +02:00
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
for container in client.containers.list():
|
|
|
|
interfaces: typing.List[Interface] = []
|
2018-08-31 04:50:58 +02:00
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
# Iterate over container interfaces
|
2023-06-17 03:08:59 +02:00
|
|
|
for net_name, net_info in container.attrs["NetworkSettings"][
|
|
|
|
"Networks"
|
|
|
|
].items():
|
2018-11-27 07:22:25 +01:00
|
|
|
endpoint_id = net_info["EndpointID"]
|
2018-08-31 04:50:58 +02:00
|
|
|
|
2020-06-05 01:01:30 +02:00
|
|
|
aliases = []
|
|
|
|
if net_info["Aliases"]:
|
|
|
|
for alias in net_info["Aliases"]:
|
|
|
|
# The aliases always contain the shortened container id and container name
|
|
|
|
if alias != container.id[:12] and alias != container.name:
|
|
|
|
aliases.append(alias)
|
|
|
|
|
2023-06-17 03:08:59 +02:00
|
|
|
interfaces.append(Interface(endpoint_id, net_info["IPAddress"], aliases))
|
2018-11-27 07:22:25 +01:00
|
|
|
links.append(Link(container.id, endpoint_id, net_name))
|
2018-08-31 04:50:58 +02:00
|
|
|
|
2018-11-27 07:22:25 +01:00
|
|
|
if verbose:
|
2023-06-17 03:08:59 +02:00
|
|
|
print(
|
|
|
|
f"Container: {container.name} {''.join([iface.address for iface in interfaces])}"
|
|
|
|
)
|
2018-11-27 07:22:25 +01:00
|
|
|
|
|
|
|
containers.append(Container(container.id, container.name, interfaces))
|
|
|
|
|
|
|
|
return containers, links
|
|
|
|
|
|
|
|
|
|
|
|
def draw_network(g: Graph, net: Network):
|
|
|
|
label = f"{{<gw_iface> {net.gateway} | {net.name}"
|
|
|
|
if net.internal:
|
|
|
|
label += " | Internal"
|
|
|
|
if net.isolated:
|
|
|
|
label += " | Containers isolated"
|
|
|
|
label += "}"
|
|
|
|
|
2023-06-17 03:08:59 +02:00
|
|
|
g.node(
|
|
|
|
f"network_{net.name}",
|
|
|
|
shape="record",
|
|
|
|
label=label,
|
|
|
|
fillcolor=net.color,
|
|
|
|
style="filled",
|
|
|
|
)
|
2018-11-27 07:22:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def draw_container(g: Graph, c: Container):
|
2020-06-05 01:01:30 +02:00
|
|
|
iface_labels = []
|
|
|
|
|
|
|
|
for iface in c.interfaces:
|
|
|
|
iface_label = "{"
|
|
|
|
|
|
|
|
for alias in iface.aliases:
|
|
|
|
iface_label += f" {alias} |"
|
|
|
|
|
|
|
|
iface_label += f"<{iface.endpoint_id}> {iface.address} }}"
|
|
|
|
iface_labels.append(iface_label)
|
2018-11-27 07:22:25 +01:00
|
|
|
|
2020-06-05 01:01:30 +02:00
|
|
|
label = f"{{ {c.name} | {{ {' | '.join(iface_labels)} }} }}"
|
2018-11-27 07:22:25 +01:00
|
|
|
|
2023-06-17 03:08:59 +02:00
|
|
|
g.node(
|
|
|
|
f"container_{c.container_id}",
|
|
|
|
shape="record",
|
|
|
|
label=label,
|
|
|
|
fillcolor="#ff9999",
|
|
|
|
style="filled",
|
|
|
|
)
|
2018-11-27 07:22:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def draw_link(g: Graph, networks: typing.Dict[str, Network], link: Link):
|
2023-06-17 03:08:59 +02:00
|
|
|
g.edge(
|
|
|
|
f"container_{link.container_id}:{link.endpoint_id}",
|
|
|
|
f"network_{link.network_name}",
|
|
|
|
color=networks[link.network_name].color,
|
|
|
|
)
|
2018-11-27 07:22:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def generate_graph(verbose: bool, file: str):
|
|
|
|
docker_client = docker.from_env()
|
|
|
|
|
|
|
|
networks = get_networks(docker_client, verbose)
|
|
|
|
containers, links = get_containers(docker_client, verbose)
|
|
|
|
|
|
|
|
if file:
|
|
|
|
base, ext = os.path.splitext(file)
|
2023-06-17 03:08:59 +02:00
|
|
|
g = Graph(
|
|
|
|
comment="Docker Network Graph",
|
|
|
|
engine="sfdp",
|
|
|
|
format=ext[1:],
|
|
|
|
graph_attr=dict(splines="true"),
|
|
|
|
)
|
2018-11-27 07:22:25 +01:00
|
|
|
else:
|
2023-06-17 03:08:59 +02:00
|
|
|
g = Graph(
|
|
|
|
comment="Docker Network Graph",
|
|
|
|
engine="sfdp",
|
|
|
|
graph_attr=dict(splines="true"),
|
|
|
|
)
|
2018-11-27 07:22:25 +01:00
|
|
|
|
|
|
|
for _, network in networks.items():
|
|
|
|
draw_network(g, network)
|
|
|
|
|
|
|
|
for container in containers:
|
|
|
|
draw_container(g, container)
|
|
|
|
|
|
|
|
for link in links:
|
2020-06-04 23:24:50 +02:00
|
|
|
if link.network_name != "none":
|
|
|
|
draw_link(g, networks, link)
|
2018-08-31 04:50:58 +02:00
|
|
|
|
|
|
|
if file:
|
2018-11-27 07:22:25 +01:00
|
|
|
g.render(base)
|
|
|
|
else:
|
|
|
|
print(g.source)
|
|
|
|
|
|
|
|
|
|
|
|
def graphviz_output_file(filename: str):
|
|
|
|
ext = os.path.splitext(filename)[1][1:]
|
|
|
|
if ext.lower() not in FORMATS:
|
|
|
|
raise argparse.ArgumentTypeError("Must be valid graphviz output format")
|
|
|
|
return filename
|
2018-08-31 04:50:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-11-27 07:37:31 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Visualize docker networks.")
|
2018-08-31 04:50:58 +02:00
|
|
|
parser.add_argument("-v", "--verbose", help="Verbose output", action="store_true")
|
2023-06-17 03:08:59 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"-o", "--out", help="Write output to file", type=graphviz_output_file
|
|
|
|
)
|
2018-08-31 04:50:58 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
generate_graph(args.verbose, args.out)
|