ualex73 / monitor_docker

Monitor Docker containers from Home Assistant
Apache License 2.0
267 stars 34 forks source link

Debian 11 will not show any stats #67

Closed ranrinc closed 2 years ago

ranrinc commented 2 years ago

After the upgrade, my os from Debian 10 to Debian 11 all my stats disappear from both portainer and monitor_docker. Is there a solution for this? Especially since HA will soon be upgrading its supervised version to Debian 11.

Debian 11 Screen Shot 2021-08-29 at 9 20 41 PM

Screen Shot 2021-08-29 at 9 20 59 PM

basically due to the new cgroup v2 on Debian 11 create error on monitor Docker

ranrinc commented 2 years ago

Just found out that the problem is being Debian 11 is implementing a new cgroup from v1 to v2 which creates changes in getting stats. Will the add-on fixed the problem too?

KalleDK commented 2 years ago

I had same problems, as a tmp quick fix I simple changed the following in helpers.py

        # Gather memory information
        memory_stats = {}
        try:
            # Memory is in Bytes, convert to MBytes
            memory_stats["usage"] = toMB(
                raw["memory_stats"]["usage"] - raw["memory_stats"]["stats"]["inactive_file"]
            )
            memory_stats["limit"] = toMB(raw["memory_stats"]["limit"])
            # memory_stats["max_usage"] = toMB(raw["memory_stats"]["max_usage"])
            memory_stats["usage_percent"] = round(
                float(memory_stats["usage"]) / float(memory_stats["limit"]) * 100.0,
                PRECISION,
            )
KalleDK commented 2 years ago

From https://docs.docker.com/engine/reference/commandline/stats/

On Linux, the Docker CLI reports memory usage by subtracting cache usage from the total memory usage. The API does not perform such a calculation but rather provides the total memory usage and the amount from the cache so that clients can use the data as needed. The cache usage is defined as the value of total_inactive_file field in the memory.stat file on cgroup v1 hosts.

On Docker 19.03 and older, the cache usage was defined as the value of cache field. On cgroup v2 hosts, the cache usage is defined as the value of inactive_file field.

KalleDK commented 2 years ago

Also https://docs.docker.com/engine/api/v1.41/#operation/ContainerStats

ranrinc commented 2 years ago
raw["memory_stats"]["usage"] - raw["memory_stats"]["stats"]["inactive_file"]

yay it works.. thanks, man.

nickrout commented 2 years ago

How about a PR then?

ranrinc commented 2 years ago

How about a PR then?

The credit goes to @KalleDK ... else I already do a PR hahahaha...

KalleDK commented 2 years ago

I don't know how to test if their are cgroup1 or 2 which would be needed in order not to break the older

ranrinc commented 2 years ago

I don't know how to test if there are cgroup1 or 2 which would be needed in order not to break the older

you are correct on that issue. I know how to check using docker command, but that is as far as getting info

docker info | grep -i cgroup

ualex73 commented 2 years ago

Just applied the workaround was mentioned above in the master. Can anybody test the new "helpers.py" out and let me know?

File modified: https://github.com/ualex73/monitor_docker/blob/master/custom_components/monitor_docker/helpers.py

KalleDK commented 2 years ago

Shouldn't it be

if "cache" in raw["memory_stats"]["stats"]:
if "cache" in raw:

Also this wont work in the new cgroup, as I read there is no substitute for it. Ref

memory_stats["max_usage"] = toMB(raw["memory_stats"]["max_usage"])

But I don't see it used anywhere else in the code... So can it just be removed? Or "deprecated" by moving it to the Debian 10 section.. So if you are on 10 you can still use it, but on 11 it's missing

KalleDK commented 2 years ago

https://github.com/ualex73/monitor_docker/pull/68

ualex73 commented 2 years ago

@KalleDK thanks it is merged. I will do a test tomorrow/friday and then release it as a version (then it shows up in HACS)

ranrinc commented 2 years ago

@KalleDK thanks it is merged. I will do a test tomorrow/friday and then release it as a version (then it shows up in HACS)

great.. can wait till it's up. Since the latest HA already making Debian 11 official

ualex73 commented 2 years ago

It is merged a while ago, so I will close this one.

ualex73 commented 2 years ago

I just created 1.11-beta, where I tweaked the memory code a bit more. Now it checks the docker version first, before making a decision which field it should pick.