From 4d6052e79e3b70bf1307b97934811e8fdef6ec77 Mon Sep 17 00:00:00 2001 From: Hyunseok Lim Date: Mon, 19 Jun 2023 17:46:41 +0900 Subject: [PATCH 1/6] Add TODO --- glances/plugins/containers/engines/docker.py | 8 ++++---- glances/plugins/containers/model.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/glances/plugins/containers/engines/docker.py b/glances/plugins/containers/engines/docker.py index bfa8bd19c2..90129de97b 100644 --- a/glances/plugins/containers/engines/docker.py +++ b/glances/plugins/containers/engines/docker.py @@ -215,7 +215,7 @@ class DockerContainersExtension: CONTAINER_ACTIVE_STATUS = ['running', 'paused'] - def __init__(self): + def __init__(self, server_url:str = 'unix://var/run/docker.sock'): if import_docker_error_tag: raise Exception("Missing libs required to run Docker Extension (Containers) ") @@ -223,14 +223,14 @@ def __init__(self): self.ext_name = "containers (Docker)" self.stats_fetchers = {} - self.connect() + self.connect(server_url) - def connect(self): + def connect(self, base_url:str = 'unix://var/run/docker.sock'): """Connect to the Docker server.""" # Init the Docker API Client try: # Do not use the timeout option (see issue #1878) - self.client = docker.from_env() + self.client = docker.DockerClient(base_url) except Exception as e: logger.error("{} plugin - Can't connect to Docker ({})".format(self.ext_name, e)) self.client = None diff --git a/glances/plugins/containers/model.py b/glances/plugins/containers/model.py index 85255209a5..4f030cc7d4 100644 --- a/glances/plugins/containers/model.py +++ b/glances/plugins/containers/model.py @@ -70,7 +70,7 @@ def __init__(self, args=None, config=None): self.args = args # Default config keys - self.config = config + self.config = config # TODO: need to debug this point # We want to display the stat in the curse interface self.display_curse = True @@ -82,7 +82,7 @@ def __init__(self, args=None, config=None): if import_podman_error_tag: self.podman_client = None else: - self.podman_client = PodmanContainersExtension(podman_sock=self._podman_sock()) + self.podman_client = PodmanContainersExtension(podman_sock=self._podman_sock()) # TODO: podman also # Sort key self.sort_key = None @@ -157,7 +157,7 @@ def update(self): if self.input_method == 'local': # Update stats - stats_docker = self.update_docker() if self.docker_extension else {} + stats_docker = self.update_docker() if self.docker_extension else {} # TODO: need to concat all docker client stats_podman = self.update_podman() if self.podman_client else {} stats = { 'version': stats_docker.get('version', {}), From bfc90dea3dd7deab950230832891c1c6c2a8bff6 Mon Sep 17 00:00:00 2001 From: Hyunseok Lim Date: Fri, 23 Jun 2023 02:23:47 +0900 Subject: [PATCH 2/6] Implement multi clients --- glances/plugins/containers/engines/docker.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/glances/plugins/containers/engines/docker.py b/glances/plugins/containers/engines/docker.py index 90129de97b..783be7781c 100644 --- a/glances/plugins/containers/engines/docker.py +++ b/glances/plugins/containers/engines/docker.py @@ -215,25 +215,26 @@ class DockerContainersExtension: CONTAINER_ACTIVE_STATUS = ['running', 'paused'] - def __init__(self, server_url:str = 'unix://var/run/docker.sock'): + def __init__(self, server_urls:str|list = 'unix://var/run/docker.sock'): if import_docker_error_tag: raise Exception("Missing libs required to run Docker Extension (Containers) ") - self.client = None + self.clients = None self.ext_name = "containers (Docker)" self.stats_fetchers = {} - self.connect(server_url) + self.connect(server_urls) - def connect(self, base_url:str = 'unix://var/run/docker.sock'): + def connect(self, base_urls:str|list = 'unix://var/run/docker.sock'): """Connect to the Docker server.""" # Init the Docker API Client + base_urls = [base_urls] if isinstance(base_urls, str) else base_urls try: # Do not use the timeout option (see issue #1878) - self.client = docker.DockerClient(base_url) + self.clients = [docker.DockerClient(url) for url in base_urls] except Exception as e: logger.error("{} plugin - Can't connect to Docker ({})".format(self.ext_name, e)) - self.client = None + self.clients = None def update_version(self): # Long and not useful anymore because the information is no more displayed in UIs @@ -248,7 +249,7 @@ def stop(self): def update(self, all_tag): """Update Docker stats using the input method.""" - if not self.client: + if not self.clients: return {}, [] version_stats = self.update_version() @@ -257,7 +258,8 @@ def update(self, all_tag): try: # Issue #1152: Docker module doesn't export details about stopped containers # The Containers/all key of the configuration file should be set to True - containers = self.client.containers.list(all=all_tag) + containers = [] + [[containers.append(container_per_client) for container_per_client in client.containers.list(all=all_tag)] for client in self.clients] except Exception as e: logger.error("{} plugin - Can't get containers list ({})".format(self.ext_name, e)) return version_stats, [] From f26668df3ca7813685f2c0e4394bc02325982e10 Mon Sep 17 00:00:00 2001 From: Hyunseok Lim Date: Fri, 23 Jun 2023 02:35:32 +0900 Subject: [PATCH 3/6] Glances can receive multiple docker host urls --- glances/plugins/containers/model.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/glances/plugins/containers/model.py b/glances/plugins/containers/model.py index 4f030cc7d4..4724b6d706 100644 --- a/glances/plugins/containers/model.py +++ b/glances/plugins/containers/model.py @@ -70,13 +70,13 @@ def __init__(self, args=None, config=None): self.args = args # Default config keys - self.config = config # TODO: need to debug this point + self.config = config # We want to display the stat in the curse interface self.display_curse = True # Init the Docker API - self.docker_extension = DockerContainersExtension() if not import_docker_error_tag else None + self.docker_extension = DockerContainersExtension(server_urls=self._docker_sock()) if not import_docker_error_tag else None # Init the Podman API if import_podman_error_tag: @@ -91,6 +91,16 @@ def __init__(self, args=None, config=None): self.update() self.refresh_timer.set(0) + def _docker_sock(self): + """Return the docker socks. + Default value: unix://var/run/docker.sock + """ + conf_docker_sock = self.get_conf_value('docker_sock') + if len(conf_docker_sock) == 0: + return "unix://var/run/docker.sock" + else: + return conf_docker_sock + def _podman_sock(self): """Return the podman sock. Could be desfined in the [docker] section thanks to the podman_sock option. @@ -157,7 +167,7 @@ def update(self): if self.input_method == 'local': # Update stats - stats_docker = self.update_docker() if self.docker_extension else {} # TODO: need to concat all docker client + stats_docker = self.update_docker() if self.docker_extension else {} stats_podman = self.update_podman() if self.podman_client else {} stats = { 'version': stats_docker.get('version', {}), From 2bdc252d1d237cb88a5af33fb33f5d8935d145df Mon Sep 17 00:00:00 2001 From: Hyunseok Lim Date: Fri, 23 Jun 2023 02:46:31 +0900 Subject: [PATCH 4/6] Add docker_sock --- docker-compose/glances.conf | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose/glances.conf b/docker-compose/glances.conf index ad22d4ae19..041f95f923 100644 --- a/docker-compose/glances.conf +++ b/docker-compose/glances.conf @@ -424,6 +424,8 @@ max_name_size=20 all=False # Define Podman sock #podman_sock=unix:///run/user/1000/podman/podman.sock +# Define Docker sock +#docker_sock=unix://var/run/docker.sock [amps] # AMPs configuration are defined in the bottom of this file From 00cd27f53d5e24da3fef38b0971067de73d4b327 Mon Sep 17 00:00:00 2001 From: Hyunseok Lim Date: Fri, 23 Jun 2023 09:19:04 +0900 Subject: [PATCH 5/6] Fixed unix url format --- docker-compose/glances.conf | 2 +- glances/plugins/containers/engines/docker.py | 4 ++-- glances/plugins/containers/model.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docker-compose/glances.conf b/docker-compose/glances.conf index 041f95f923..450bdf8362 100644 --- a/docker-compose/glances.conf +++ b/docker-compose/glances.conf @@ -425,7 +425,7 @@ all=False # Define Podman sock #podman_sock=unix:///run/user/1000/podman/podman.sock # Define Docker sock -#docker_sock=unix://var/run/docker.sock +docker_sock=unix:///var/run/docker.sock [amps] # AMPs configuration are defined in the bottom of this file diff --git a/glances/plugins/containers/engines/docker.py b/glances/plugins/containers/engines/docker.py index 783be7781c..3254320b6b 100644 --- a/glances/plugins/containers/engines/docker.py +++ b/glances/plugins/containers/engines/docker.py @@ -215,7 +215,7 @@ class DockerContainersExtension: CONTAINER_ACTIVE_STATUS = ['running', 'paused'] - def __init__(self, server_urls:str|list = 'unix://var/run/docker.sock'): + def __init__(self, server_urls:str|list = 'unix:///var/run/docker.sock'): if import_docker_error_tag: raise Exception("Missing libs required to run Docker Extension (Containers) ") @@ -225,7 +225,7 @@ def __init__(self, server_urls:str|list = 'unix://var/run/docker.sock'): self.connect(server_urls) - def connect(self, base_urls:str|list = 'unix://var/run/docker.sock'): + def connect(self, base_urls:str|list = 'unix:///var/run/docker.sock'): """Connect to the Docker server.""" # Init the Docker API Client base_urls = [base_urls] if isinstance(base_urls, str) else base_urls diff --git a/glances/plugins/containers/model.py b/glances/plugins/containers/model.py index 4724b6d706..d3e66f5799 100644 --- a/glances/plugins/containers/model.py +++ b/glances/plugins/containers/model.py @@ -93,11 +93,11 @@ def __init__(self, args=None, config=None): def _docker_sock(self): """Return the docker socks. - Default value: unix://var/run/docker.sock + Default value: unix:///var/run/docker.sock """ conf_docker_sock = self.get_conf_value('docker_sock') if len(conf_docker_sock) == 0: - return "unix://var/run/docker.sock" + return "unix:///var/run/docker.sock" else: return conf_docker_sock From 72cfaf718a41af59d7c295f875cec52b3342b1d3 Mon Sep 17 00:00:00 2001 From: Hyunseok Lim Date: Fri, 23 Jun 2023 10:34:34 +0900 Subject: [PATCH 6/6] Add Socket URL to stats --- glances/plugins/containers/engines/docker.py | 1 + glances/plugins/containers/model.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/glances/plugins/containers/engines/docker.py b/glances/plugins/containers/engines/docker.py index 3254320b6b..6de5a8d534 100644 --- a/glances/plugins/containers/engines/docker.py +++ b/glances/plugins/containers/engines/docker.py @@ -301,6 +301,7 @@ def generate_stats(self, container): # Container Status (from attrs) 'Status': container.attrs['State']['Status'], 'Created': container.attrs['Created'], + 'Socket_URL': container.client.api.base_url, 'Command': [], } diff --git a/glances/plugins/containers/model.py b/glances/plugins/containers/model.py index d3e66f5799..5fb3df8fbd 100644 --- a/glances/plugins/containers/model.py +++ b/glances/plugins/containers/model.py @@ -307,6 +307,8 @@ def msg_curse(self, args=None, max_width=None): ret.append(self.curse_add_line(msg)) msg = ' {:<7}'.format('Tx/s') ret.append(self.curse_add_line(msg)) + msg = '{:<30}'.format('Socket URL') + ret.append(self.curse_add_line(msg)) msg = ' {:8}'.format('Command') ret.append(self.curse_add_line(msg)) @@ -391,6 +393,12 @@ def msg_curse(self, args=None, max_width=None): except KeyError: msg = ' {:<7}'.format('_') ret.append(self.curse_add_line(msg)) + # Socket URL + if container['Socket_URL'] is not None: + msg = '{:<30}'.format(container['Socket_URL']) + else: + msg = '{:<30}'.format('_') + ret.append(self.curse_add_line(msg, splittable=True)) # Command if container['Command'] is not None: msg = ' {}'.format(' '.join(container['Command']))