I've been using this great Golang library to collect CPU percentage of the container (not the host like top command retrieves) but the repo seems to be inactive and can only collect cgroup v1 cpu percentage. https://github.com/HobbyBear/cmetric
For cgroup v2, I managed to calculate the percent usage for memory but I cannot get the CPU percentage correct.
Memory percentage for cgroup v2:
- work out if cgroup v2 is used - below should equal cgroup2fs
stat -fc %T /sys/fs/cgroup/
- Divide the current memory by the max memory. Note the memory limit in docker needs to be set.
cat /sys/fs/cgroup/memory.max
cat /sys/fs/cgroup/memory.current
Any advice on how to calculate the CPU percentage would be appreciated. :)