Add simulator enhancements.
[sim/o1-interface.git] / ntsimulator / deploy / nts-manager / docker_stats.sh
1 #!/bin/bash
2 ################################################################################
3 #
4 # Copyright 2020 highstreet technologies GmbH and others
5 #
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
9
10 #     http://www.apache.org/licenses/LICENSE-2.0
11
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 ################################################################################
18
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)
21
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
24
25 CPU_CORES=`nproc`
26 # the new lines thus resulting in a failure when using awk to process each line
27 IFS=;
28 mapfile -t DOCKER_PS_RESULT < <(/usr/bin/docker ps --all --format "{{.ID}}" --filter "label=NTS_Manager=$1")
29
30 CONTAINERS=""
31
32 if [ ${#DOCKER_PS_RESULT[@]} -gt 0 ]
33 then
34
35         for container in "${DOCKER_PS_RESULT[@]}"
36         do
37                 CONTAINERS="$CONTAINERS $container"
38         done
39 fi
40
41 if [ -z "$CONTAINERS" ]
42 then
43         CPU_SCALED=0
44         SUM_RAM=0
45 else
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")
48
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}'`
51
52         CPU_SCALED=$(echo "scale=2; $SUM_CPU/$CPU_CORES" | bc)
53 fi
54
55 # Output the result
56 echo -e "CPU=${CPU_SCALED}%;RAM=${SUM_RAM}MiB"