From c8a5f62a69242cfdec08c957c3006bf9cb4ec4db Mon Sep 17 00:00:00 2001 From: Victor Vostrikov <1998617+gorynychzmey@users.noreply.github.com> Date: Tue, 18 Jan 2022 20:48:08 +0100 Subject: [PATCH 1/2] CPU Stats compatibility for Podman with cgroup v2 Podman's realisation of Docker API uses cpuacct for per_cpu and online_cpus, which are only available with cgroup v1. --- custom_components/monitor_docker/helpers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/custom_components/monitor_docker/helpers.py b/custom_components/monitor_docker/helpers.py index d0be2b4..f05aa17 100644 --- a/custom_components/monitor_docker/helpers.py +++ b/custom_components/monitor_docker/helpers.py @@ -874,10 +874,12 @@ async def _run_container_stats(self): # Compatibility wih older Docker API if "online_cpus" in raw["cpu_stats"]: cpu_stats["online_cpus"] = raw["cpu_stats"]["online_cpus"] - else: + elif "percpu_usage" in raw["cpu_stats"]["cpu_usage"]: cpu_stats["online_cpus"] = len( raw["cpu_stats"]["cpu_usage"]["percpu_usage"] or [] ) + else: + cpu_stats["online_cpus"] = 1 # Calculate cpu usage, but first iteration we don't know it if self._cpu_old: From 40c88241211b487a9bc4bfeea53b0ab55c422737 Mon Sep 17 00:00:00 2001 From: Victor Vostrikov <1998617+gorynychzmey@users.noreply.github.com> Date: Tue, 18 Jan 2022 22:20:48 +0100 Subject: [PATCH 2/2] Update helpers.py --- custom_components/monitor_docker/helpers.py | 29 +++++++++++---------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/custom_components/monitor_docker/helpers.py b/custom_components/monitor_docker/helpers.py index f05aa17..646fb3d 100644 --- a/custom_components/monitor_docker/helpers.py +++ b/custom_components/monitor_docker/helpers.py @@ -938,20 +938,21 @@ async def _run_container_stats(self): cache = 0 # https://docs.docker.com/engine/reference/commandline/stats/ - if self._version1904: - # Version is 19.04 or higher, don't use "cache" - if "total_inactive_file" in raw["memory_stats"]["stats"]: - cache = raw["memory_stats"]["stats"]["total_inactive_file"] - elif "inactive_file" in raw["memory_stats"]["stats"]: - cache = raw["memory_stats"]["stats"]["inactive_file"] - else: - # Version is 19.03 and lower, use "cache" - if "cache" in raw["memory_stats"]["stats"]: - cache = raw["memory_stats"]["stats"]["cache"] - elif "total_inactive_file" in raw["memory_stats"]["stats"]: - cache = raw["memory_stats"]["stats"]["total_inactive_file"] - elif "inactive_file" in raw["memory_stats"]["stats"]: - cache = raw["memory_stats"]["stats"]["inactive_file"] + if "stats" in raw["memory_stats"]: + if self._version1904: + # Version is 19.04 or higher, don't use "cache" + if "total_inactive_file" in raw["memory_stats"]["stats"]: + cache = raw["memory_stats"]["stats"]["total_inactive_file"] + elif "inactive_file" in raw["memory_stats"]["stats"]: + cache = raw["memory_stats"]["stats"]["inactive_file"] + else: + # Version is 19.03 and lower, use "cache" + if "cache" in raw["memory_stats"]["stats"]: + cache = raw["memory_stats"]["stats"]["cache"] + elif "total_inactive_file" in raw["memory_stats"]["stats"]: + cache = raw["memory_stats"]["stats"]["total_inactive_file"] + elif "inactive_file" in raw["memory_stats"]["stats"]: + cache = raw["memory_stats"]["stats"]["inactive_file"] memory_stats["usage"] = toMB( raw["memory_stats"]["usage"] - cache,