Add License information in every source file or script.
[sim/o1-interface.git] / ntsimulator / scripts / docker_stats.sh
1 #!/bin/bash
2 ################################################################################
3 #
4 # Copyright 2019 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 CPU_CORES=`nproc`
25 # the new lines thus resulting in a failure when using awk to process each line
26 IFS=;
27 mapfile -t DOCKER_PS_RESULT < <(/usr/bin/docker ps --all --format "{{.ID}}" --filter "label=NTS")
28
29 CONTAINERS=""
30
31 if [ ${#DOCKER_PS_RESULT[@]} -gt 0 ]
32 then
33
34         for container in "${DOCKER_PS_RESULT[@]}"
35         do
36                 CONTAINERS="$CONTAINERS $container"
37         done
38 fi
39
40 if [ -z "$CONTAINERS" ]
41 then
42         CPU_SCALED=0
43         SUM_RAM=0
44 else
45         DOCKER_STATS_COMMAND="/usr/bin/docker stats --no-stream --format \"table {{.CPUPerc}}\t{{.MemUsage}}\" ${CONTAINERS}"
46         DOCKER_STATS_COMMAND_RESULT=$(eval "$DOCKER_STATS_COMMAND")
47
48         SUM_CPU=`echo $DOCKER_STATS_COMMAND_RESULT | tail -n +2 | sed "s/%//g" | awk '{s+=$1} END {print s}'`
49         SUM_RAM=`echo $DOCKER_STATS_COMMAND_RESULT | tail -n +2 | sed "s/%//g" | awk '{s+=$2} END {print s}'`
50
51         CPU_SCALED=$(echo "scale=2; $SUM_CPU/$CPU_CORES" | bc)
52 fi
53
54 # Output the result
55 echo -e "CPU=${CPU_SCALED}%;RAM=${SUM_RAM}MiB"