visualization of ports

This commit is contained in:
MuratovAS 2024-01-14 03:18:42 +05:00
parent 212a082af3
commit 2492ba0646
No known key found for this signature in database
GPG key ID: CE09BE03367711AD
3 changed files with 36 additions and 11 deletions

View file

@ -8,10 +8,11 @@ Changes:
- Improved design - Improved design
- Added the ability to generate url - Added the ability to generate url
- Added display of connections with the host - Added display of connections with the host
- Visualization of ports
- Github package - Github package
## Example ## Example
![example graph](./example.png) ![example graph](./example.svg)
## Usage ## Usage
```bash ```bash

View file

@ -23,6 +23,14 @@ services:
- network_b - network_b
- network_c - network_c
- no_gateway - no_gateway
service_4:
container_name: service_4
image: leoverto/dummy-image
ports:
- "1234:1234"
- "0.0.0.0:5678:5678/udp"
networks:
- internet
host_service: host_service:
container_name: host_service container_name: host_service
image: leoverto/dummy-image image: leoverto/dummy-image
@ -33,6 +41,9 @@ services:
network_mode: none network_mode: none
networks: networks:
internet:
driver_opts:
com.docker.network.bridge.enable_icc: "false"
network_a: network_a:
network_b: network_b:
network_c: network_c:

View file

@ -50,12 +50,16 @@ class Interface:
address: str address: str
aliases: typing.List[str] aliases: typing.List[str]
@dataclass
class Port:
port: str
@dataclass @dataclass
class Container: class Container:
container_id: str container_id: str
name: str name: str
interfaces: typing.List[Interface] interfaces: typing.List[Interface]
ports: typing.List[Interface]
@dataclass @dataclass
@ -125,11 +129,13 @@ def get_containers(
for container in client.containers.list(): for container in client.containers.list():
interfaces: typing.List[Interface] = [] interfaces: typing.List[Interface] = []
ports: typing.List[Port] = []
for port_name, port_info in container.attrs["NetworkSettings"]["Ports"].items():
ports.append(Port(port_name))
# Iterate over container interfaces # Iterate over container interfaces
for net_name, net_info in container.attrs["NetworkSettings"][ for net_name, net_info in container.attrs["NetworkSettings"]["Networks"].items():
"Networks"
].items():
endpoint_id = net_info["EndpointID"] endpoint_id = net_info["EndpointID"]
aliases = [] aliases = []
@ -143,11 +149,9 @@ def get_containers(
links.append(Link(container.id, endpoint_id, net_name)) links.append(Link(container.id, endpoint_id, net_name))
if verbose: if verbose:
print( print(f"Container: {container.name} {ports} {''.join([iface.address for iface in interfaces])}")
f"Container: {container.name} {''.join([iface.address for iface in interfaces])}"
)
containers.append(Container(container.id, container.name, interfaces)) containers.append(Container(container.id, container.name, interfaces, ports))
return containers, links return containers, links
@ -172,6 +176,12 @@ def draw_network(g: Graph, net: Network):
def draw_container(g: Graph, c: Container): def draw_container(g: Graph, c: Container):
iface_labels = [] iface_labels = []
port_labels = []
for iport in c.ports:
port_label = "{"
port_label += f"{iport.port} }}"
port_labels.append(port_label)
for iface in c.interfaces: for iface in c.interfaces:
iface_label = "{" iface_label = "{"
@ -182,7 +192,10 @@ def draw_container(g: Graph, c: Container):
iface_label += f"<{iface.endpoint_id}> {iface.address} }}" iface_label += f"<{iface.endpoint_id}> {iface.address} }}"
iface_labels.append(iface_label) iface_labels.append(iface_label)
label = f"{{ {c.name} | {{ {' | '.join(iface_labels)} }} }}" label =f"{{ {c.name} "
if port_labels:
label = label + f"| {{ {' | '.join(port_labels)} }} "
label = label + f"| {{ {' | '.join(iface_labels)} }} }}"
g.node( g.node(
f"container_{c.container_id}", f"container_{c.container_id}",