Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enhanced docker stats command with total amount of RAM and CPU

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"
like image 945
Michel L Avatar asked Nov 16 '17 13:11

Michel L


2 Answers

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.

like image 156
Thomas Avatar answered Nov 17 '22 16:11

Thomas


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.

like image 21
Ronino Avatar answered Nov 17 '22 17:11

Ronino