Initial commit of the O1 simulator framework.
[sim/o1-interface.git] / ntsimulator / scripts / docker_stats.sh
1 #!/bin/bash
2
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)
5
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
8 CPU_CORES=`nproc`
9 # the new lines thus resulting in a failure when using awk to process each line
10 IFS=;
11 mapfile -t DOCKER_PS_RESULT < <(/usr/bin/docker ps --all --format "{{.ID}}" --filter "label=NTS")
12
13 CONTAINERS=""
14
15 if [ ${#DOCKER_PS_RESULT[@]} -gt 0 ]
16 then
17
18         for container in "${DOCKER_PS_RESULT[@]}"
19         do
20                 CONTAINERS="$CONTAINERS $container"
21         done
22 fi
23
24 if [ -z "$CONTAINERS" ]
25 then
26         CPU_SCALED=0
27         SUM_RAM=0
28 else
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")
31
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}'`
34
35         CPU_SCALED=$(echo "scale=2; $SUM_CPU/$CPU_CORES" | bc)
36 fi
37
38 # Output the result
39 echo -e "CPU=${CPU_SCALED}%;RAM=${SUM_RAM}MiB"