3 # This script is used to complete the output of the docker stats command.
4 # The docker stats command does not compute the total amount of resources (RAM or CPU)
6 # Get the output of the docker stat command. Will be displayed at the end
7 # Without modifying the special variable IFS the ouput of the docker stats command won't have
9 # the new lines thus resulting in a failure when using awk to process each line
11 mapfile -t DOCKER_PS_RESULT < <(/usr/bin/docker ps --all --format "{{.ID}}" --filter "label=NTS")
15 if [ ${#DOCKER_PS_RESULT[@]} -gt 0 ]
18 for container in "${DOCKER_PS_RESULT[@]}"
20 CONTAINERS="$CONTAINERS $container"
24 if [ -z "$CONTAINERS" ]
29 DOCKER_STATS_COMMAND="/usr/bin/docker stats --no-stream --format \"table {{.CPUPerc}}\t{{.MemUsage}}\" ${CONTAINERS}"
30 DOCKER_STATS_COMMAND_RESULT=$(eval "$DOCKER_STATS_COMMAND")
32 SUM_CPU=`echo $DOCKER_STATS_COMMAND_RESULT | tail -n +2 | sed "s/%//g" | awk '{s+=$1} END {print s}'`
33 SUM_RAM=`echo $DOCKER_STATS_COMMAND_RESULT | tail -n +2 | sed "s/%//g" | awk '{s+=$2} END {print s}'`
35 CPU_SCALED=$(echo "scale=2; $SUM_CPU/$CPU_CORES" | bc)
39 echo -e "CPU=${CPU_SCALED}%;RAM=${SUM_RAM}MiB"