Vacant323
Vacant323

Reputation: 43

Get CPU/Memory percentage of a docker machine

I'd like to know how i can get the cpu and memory percentage of a docker machine remotely using a docker command. Typically a docker stats but with the infos of the machine and not the infos of containers.

Thanks a lot !

Upvotes: 0

Views: 1126

Answers (1)

adebasi
adebasi

Reputation: 3945

Using docker info you can get the information how many CPUs and how much memory the docker machine has. For Example:

...
CPUs: 4
Total Memory: 1.952 GiB
...

With docker stats -a you can see all containers and their usage of cpu and memory:

CONTAINER           CPU %               MEM USAGE / LIMIT       MEM %               NET I/O             BLOCK I/O           PIDS
43c7743b5df3        1.23%               30.6 MiB / 1.952 GiB    1.53%               9.36 kB / 1.21 kB   0 B / 6.44 MB       20
a821f9c87b2c        0.80%               88.31 MiB / 1.952 GiB   4.42%               10.9 kB / 1.21 kB   60 MB / 6.55 MB     20

But what you would like to have, when I got it right, is a sum of all container resources using a docker command. It looks like there is no such docker command, BUT...

I played a little bit around and got this (I'm not really a shell script dude, so maybe it can be written shorter):

docker stats --no-stream -a --format "{{.CPUPerc}}\t{{.MemPerc}}" | \
LC_NUMERIC="C" awk '{cpu+=substr($1,1,length($1)-1); mem+=substr($2,1,length($2)-1)} \
END {print "CPU",cpu,"%\tMem",mem,"%"}'

It produces this:

CPU 1.62 %  Mem 5.95 %

Upvotes: 4

Related Questions