Enhanced docker stats command with total amount of RAM and CPU
Asked Answered
F

3

19

I just want to share a small script that I made to enhance the docker stats command. I am not sure about the exactitude of this method.

Can I assume that the total amount of memory consumed by the complete Docker deployment is the sum of each container consumed memory ?

Please share your modifications and or corrections. This command is documented here: https://docs.docker.com/engine/reference/commandline/stats/

When running a docker stats The output looks like this:

$ docker stats --all --format "table {{.MemPerc}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.Name}}"
MEM %       CPU %       MEM USAGE / LIMIT       NAME              
0.50%       1.00%       77.85MiB / 15.57GiB     ecstatic_noether  
1.50%       3.50%       233.55MiB / 15.57GiB    stoic_goodall     
0.25%       0.50%       38.92MiB / 15.57GiB     drunk_visvesvaraya

My script will add the following line at the end:

2.25%       5.00%       350.32MiB / 15.57GiB    TOTAL

docker_stats.sh

#!/bin/bash

# This script is used to complete the output of the docker stats command.
# The docker stats command does not compute the total amount of resources (RAM or CPU)

# Get the total amount of RAM, assumes there are at least 1024*1024 KiB, therefore > 1 GiB
HOST_MEM_TOTAL=$(grep MemTotal /proc/meminfo | awk '{print $2/1024/1024}')

# Get the output of the docker stat command. Will be displayed at the end
# Without modifying the special variable IFS the ouput of the docker stats command won't have
# the new lines thus resulting in a failure when using awk to process each line
IFS=;
DOCKER_STATS_CMD=`docker stats --no-stream --format "table {{.MemPerc}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.Name}}"`

SUM_RAM=`echo $DOCKER_STATS_CMD | tail -n +2 | sed "s/%//g" | awk '{s+=$1} END {print s}'`
SUM_CPU=`echo $DOCKER_STATS_CMD | tail -n +2 | sed "s/%//g" | awk '{s+=$2} END {print s}'`
SUM_RAM_QUANTITY=`LC_NUMERIC=C printf %.2f $(echo "$SUM_RAM*$HOST_MEM_TOTAL*0.01" | bc)`

# Output the result
echo $DOCKER_STATS_CMD
echo -e "${SUM_RAM}%\t\t\t${SUM_CPU}%\t\t${SUM_RAM_QUANTITY}GiB / ${HOST_MEM_TOTAL}GiB\tTOTAL"
Foretaste answered 16/11, 2017 at 13:36 Comment(3)
Is there a problem? Something that does not work? What is your question?Db
I added the question in bold.Foretaste
Doesn't work if you have memory limits on containers, it will miscalculate the percentage and so screw up the memory percentageVilma
U
4

From the documentation that you have linked above,

The docker stats command returns a live data stream for running containers. To limit data to one or more specific containers, specify a list of container names or ids separated by a space. You can specify a stopped container but stopped containers do not return any data.

and then furthermore,

Note: On Linux, the Docker CLI reports memory usage by subtracting page 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 page cache so that clients can use the data as needed.

According to your question, it looks like you can assume so, but also do not forget it also factors in containers that exist but are not running.

Underneath answered 30/9, 2019 at 14:37 Comment(0)
E
1

Your docker_stats.sh does the job for me, thanks!

I had to add unset LC_ALL somewhere before LC_NUMERIC is used though as the former overrides the latter and otherwise I get this error: "Zeile 19: printf: 1.7989: Ungültige Zahl." This is probably due to my using a German locale.

There is also a discussion to add this feature to the "docker stats" command itself.

Euripus answered 2/7, 2020 at 6:24 Comment(0)
T
0

Thanks for sharing the script! I've updated it in a way it depends on DOCKER_MEM_TOTAL instead of HOST_MEM_TOTAL as docker has it's own memory limit which could differ from host total memory count.

Tavey answered 12/8, 2022 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.