Mic*_*l L 6 bash cpu ram containers docker
我只想分享一个我用来增强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
Run Code Online (Sandbox Code Playgroud)
我的脚本将在末尾添加以下行:
2.25% 5.00% 350.32MiB / 15.57GiB TOTAL
Run Code Online (Sandbox Code Playgroud)
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"
Run Code Online (Sandbox Code Playgroud)
从您上面链接的文档中,
docker stats 命令返回正在运行的容器的实时数据流。要将数据限制到一个或多个特定容器,请指定以空格分隔的容器名称或 ID 列表。您可以指定停止的容器,但停止的容器不会返回任何数据。
然后此外,
注意:在 Linux 上,Docker CLI 通过从总内存使用量中减去页面缓存使用量来报告内存使用量。API 不执行此类计算,而是提供总内存使用量和页面缓存的量,以便客户端可以根据需要使用数据。
根据您的问题,您似乎可以这样假设,但也不要忘记它也会影响存在但未运行的容器。
| 归档时间: |
|
| 查看次数: |
1643 次 |
| 最近记录: |