增强的docker stats命令,具有RAM和CPU的总量

问题描述 投票:6回答:1

我只想共享一个我用来增强docker stats命令的小脚本。我不确定这种方法的正确性。

我可以假设整个Docker部署消耗的内存总量是每个容器消耗的内存之和吗?

请分享您的修改和/或更正。此命令记录在这里:https://docs.docker.com/engine/reference/commandline/stats/

运行docker stats时,输出如下所示:

$ docker stats --all --format "table {{.MemPerc}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.Name}}"
MEM %       CPU %       MEM USAGE / LIMIT       NAME              
0.50%       1.00%       77.85MiB / 15.57GiB     ecstatic_noether  
1.50%       3.50%       233.55MiB / 15.57GiB    stoic_goodall     
0.25%       0.50%       38.92MiB / 15.57GiB     drunk_visvesvaraya

我的脚本将在末尾添加以下行:

2.25%       5.00%       350.32MiB / 15.57GiB    TOTAL

docker_stats.sh

#!/bin/bash

# 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 total amount of RAM, assumes there are at least 1024*1024 KiB, therefore > 1 GiB
HOST_MEM_TOTAL=$(grep MemTotal /proc/meminfo | awk '{print $2/1024/1024}')

# 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
# the new lines thus resulting in a failure when using awk to process each line
IFS=;
DOCKER_STATS_CMD=`docker stats --no-stream --format "table {{.MemPerc}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.Name}}"`

SUM_RAM=`echo $DOCKER_STATS_CMD | tail -n +2 | sed "s/%//g" | awk '{s+=$1} END {print s}'`
SUM_CPU=`echo $DOCKER_STATS_CMD | tail -n +2 | sed "s/%//g" | awk '{s+=$2} END {print s}'`
SUM_RAM_QUANTITY=`LC_NUMERIC=C printf %.2f $(echo "$SUM_RAM*$HOST_MEM_TOTAL*0.01" | bc)`

# Output the result
echo $DOCKER_STATS_CMD
echo -e "${SUM_RAM}%\t\t\t${SUM_CPU}%\t\t${SUM_RAM_QUANTITY}GiB / ${HOST_MEM_TOTAL}GiB\tTOTAL"
bash docker containers cpu ram
1个回答
0
投票

从上面链接的文档中,

docker stats命令返回正在运行的容器的实时数据流。要将数据限制到一个或多个特定容器,请指定一列用空格分隔的容器名称或ID。您可以指定一个停止的容器,但是停止的容器不返回任何数据。

然后是,

注意:在Linux上,Docker CLI通过从总内存使用量中减去页面缓存使用率来报告内存使用情况。该API不执行这种计算,而是提供总的内存使用量和页面缓存中的内存量,以便客户端可以根据需要使用数据。

根据您的问题,您似乎可以这样假设,但也请不要忘记,它还会影响存在但未运行的容器。

© www.soinside.com 2019 - 2024. All rights reserved.