2 ################################################################################
4 # Copyright 2020 highstreet technologies GmbH and others
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 ################################################################################
19 # This script is used to complete the output of the docker stats command.
20 # The docker stats command does not compute the total amount of resources (RAM or CPU)
22 # Get the output of the docker stat command. Will be displayed at the end
23 # Without modifying the special variable IFS the ouput of the docker stats command won't have
26 # the new lines thus resulting in a failure when using awk to process each line
28 mapfile -t DOCKER_PS_RESULT < <(/usr/bin/docker ps --all --format "{{.ID}}" --filter "label=NTS_Manager=$1")
32 if [ ${#DOCKER_PS_RESULT[@]} -gt 0 ]
35 for container in "${DOCKER_PS_RESULT[@]}"
37 CONTAINERS="$CONTAINERS $container"
41 if [ -z "$CONTAINERS" ]
46 DOCKER_STATS_COMMAND="/usr/bin/docker stats --no-stream --format \"table {{.CPUPerc}}\t{{.MemUsage}}\" ${CONTAINERS}"
47 DOCKER_STATS_COMMAND_RESULT=$(eval "$DOCKER_STATS_COMMAND")
49 SUM_CPU=`echo $DOCKER_STATS_COMMAND_RESULT | tail -n +2 | sed "s/%//g" | awk '{s+=$1} END {print s}'`
50 SUM_RAM=`echo $DOCKER_STATS_COMMAND_RESULT | tail -n +2 | sed "s/%//g" | awk '{s+=$2} END {print s}'`
52 CPU_SCALED=$(echo "scale=2; $SUM_CPU/$CPU_CORES" | bc)
56 echo -e "CPU=${CPU_SCALED}%;RAM=${SUM_RAM}MiB"