X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=ntsimulator%2Fdeploy%2Fnts-manager%2Fdocker_stats.sh;fp=ntsimulator%2Fdeploy%2Fnts-manager%2Fdocker_stats.sh;h=317d3ce03a86d0f46339135bff9711e35386c28b;hb=7dbf479029ba8bc528fb61a40ab2647489da28e9;hp=0000000000000000000000000000000000000000;hpb=610985b805c2b5c730bbc247ccbf76dd624792d0;p=sim%2Fo1-interface.git diff --git a/ntsimulator/deploy/nts-manager/docker_stats.sh b/ntsimulator/deploy/nts-manager/docker_stats.sh new file mode 100755 index 0000000..317d3ce --- /dev/null +++ b/ntsimulator/deploy/nts-manager/docker_stats.sh @@ -0,0 +1,56 @@ +#!/bin/bash +################################################################################ +# +# Copyright 2020 highstreet technologies GmbH and others +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################ + +# This script is used to complete the output of the docker stats command. +# The docker stats command does not compute the total amount of resources (RAM or CPU) + +# Get the output of the docker stat command. Will be displayed at the end +# Without modifying the special variable IFS the ouput of the docker stats command won't have + +CPU_CORES=`nproc` +# the new lines thus resulting in a failure when using awk to process each line +IFS=; +mapfile -t DOCKER_PS_RESULT < <(/usr/bin/docker ps --all --format "{{.ID}}" --filter "label=NTS_Manager=$1") + +CONTAINERS="" + +if [ ${#DOCKER_PS_RESULT[@]} -gt 0 ] +then + + for container in "${DOCKER_PS_RESULT[@]}" + do + CONTAINERS="$CONTAINERS $container" + done +fi + +if [ -z "$CONTAINERS" ] +then + CPU_SCALED=0 + SUM_RAM=0 +else + DOCKER_STATS_COMMAND="/usr/bin/docker stats --no-stream --format \"table {{.CPUPerc}}\t{{.MemUsage}}\" ${CONTAINERS}" + DOCKER_STATS_COMMAND_RESULT=$(eval "$DOCKER_STATS_COMMAND") + + SUM_CPU=`echo $DOCKER_STATS_COMMAND_RESULT | tail -n +2 | sed "s/%//g" | awk '{s+=$1} END {print s}'` + SUM_RAM=`echo $DOCKER_STATS_COMMAND_RESULT | tail -n +2 | sed "s/%//g" | awk '{s+=$2} END {print s}'` + + CPU_SCALED=$(echo "scale=2; $SUM_CPU/$CPU_CORES" | bc) +fi + +# Output the result +echo -e "CPU=${CPU_SCALED}%;RAM=${SUM_RAM}MiB" \ No newline at end of file