Created
June 23, 2021 12:11
-
-
Save jpralves/a3578eae0546f6cd15ed7b4db30524e6 to your computer and use it in GitHub Desktop.
get all docker constraints in variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# getdockerenv.sh v1.0 | |
# 2021-06-23 - João Alves | |
# This script sets a group of variables that can be useful in a containerized environment | |
# It is specially tuned for docker but might (not tested) work in other environments | |
# Exported variables: | |
# CGROUPS - v1, v2, none | |
# DOCKER_CID - current id of docker container running or none | |
# DOCKER_CPUCONSTRAINT - 0 if not constrained, 1 if constrained | |
# DOCKER_CPU - Calculated value of cpu allocation (can have decimal part) | |
# DOCKER_CPU_INT - Calculated value of CPU allocation - only the integer part. | |
# DOCKER_MEMCONSTRAINT - | |
# DOCKER_MEMTOTAL - Calculated value of total memory (can have decimal part) in MB | |
# DOCKER_MEMTOTAL_INT - Calculated value of total memory in MB - only the integer part | |
# DOCKER_MEMFREE - Calculated value of current free memory (can have decimal part) in MB | |
# DOCKER_MEMFREE_INT - Calculated value of current free memory in MB - only the integer part | |
# DOCKER_MEMPERCFREE - Percentage of free mem | |
# Tested environments: | |
# - Not containerized | |
# - Docker 20.0 | |
# - Cgroups v1 and v2 | |
# Can be tested with: | |
# Not containerized: | |
# sh getdockerenv.sh | |
# Inside container: | |
# docker run -it -v $PWD/getdockerenv.sh:/getdockerenv.sh busybox:latest sh /getdockerenv.sh | |
# docker run -it --cpus 1.5 -v $PWD/getdockerenv.sh:/getdockerenv.sh busybox:latest sh /getdockerenv.sh | |
# docker run -it --memory 200M -v $PWD/getdockerenv.sh:/getdockerenv.sh busybox:latest sh /getdockerenv.sh | |
# docker run -it --cpus 1.5 --memory 200M -v $PWD/getdockerenv.sh:/getdockerenv.sh busybox:latest sh /getdockerenv.sh | |
# If any arguments are passed to the command no echo will be produced. | |
set -e | |
# Cgroups and docker info | |
export CGROUPS | |
CGROUPS=v1 | |
export DOCKER_CID | |
DOCKER_CID=$(grep '^/docker' /proc/1/cpuset | sed 's,/docker/\([0-9a-f]*\),\1,g') | |
if [ -z "$DOCKER_CID" ]; then | |
DOCKER_CID=$(sed '/hostname/!d; s,.*/containers/\([^/]*\)/hostname.*,\1,g' /proc/self/mountinfo) | |
CGROUPS=v2 | |
fi | |
if [ -z "$DOCKER_CID" ]; then | |
DOCKER_CID=none | |
CGROUPS=none | |
fi | |
[ "$#" -gt 0 ] || echo "CGROUPS=$CGROUPS" | |
[ "$#" -gt 0 ] || echo "DOCKER_CID=$DOCKER_CID" | |
# CPU: | |
export DOCKER_CPUCONSTRAINT | |
DOCKER_CPUCONSTRAINT=0 | |
docker_image_cpus=$(nproc) | |
docker_cpu_quota=$docker_image_cpus | |
docker_cpu_period=1 | |
# cgroupsv1 | |
if [ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us ] && [ -f /sys/fs/cgroup/cpu/cpu.cfs_period_us ]; then | |
docker_cpu_quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) | |
docker_cpu_period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us) | |
DOCKER_CPUCONSTRAINT=1 | |
if [ "$docker_cpu_quota" == "-1" ]; then | |
docker_cpu_quota=$(echo "scale=1; $(nproc)*$docker_cpu_period" | bc) | |
DOCKER_CPUCONSTRAINT=0 | |
fi | |
fi | |
# cgroupsv2 | |
if [ -f /sys/fs/cgroup/cpu.max ]; then | |
docker_cpu_quota=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max) | |
docker_cpu_period=$(cut -d' ' -f2 /sys/fs/cgroup/cpu.max) | |
DOCKER_CPUCONSTRAINT=1 | |
if [ "$docker_cpu_quota" == "max" ]; then | |
docker_cpu_quota=$(echo "scale=1; $(nproc)*$docker_cpu_period" | bc) | |
DOCKER_CPUCONSTRAINT=0 | |
fi | |
fi | |
docker_image_cpus=$(echo "scale=1; c=$docker_cpu_quota/$docker_cpu_period; if(c<1 && c>0) print 0; c" | bc) | |
export DOCKER_CPU | |
DOCKER_CPU="$docker_image_cpus" | |
export DOCKER_CPU_INT | |
DOCKER_CPU_INT="$(echo "$docker_image_cpus" | sed 's,^\([0-9]*\).*$,\1,g')" | |
[ "$#" -gt 0 ] || echo "DOCKER_CPUCONSTRAINT=$DOCKER_CPUCONSTRAINT" | |
[ "$#" -gt 0 ] || echo "DOCKER_CPU=$DOCKER_CPU" | |
[ "$#" -gt 0 ] || echo "DOCKER_CPU_INT=$DOCKER_CPU_INT" | |
# MEMORY: | |
export DOCKER_MEMCONSTRAINT | |
DOCKER_MEMCONSTRAINT=0 | |
docker_image_memory_total=$(echo "scale=1; $(sed '/^MemTotal/!d; s,^.*:\s*\([0-9]*\)\s*.*,\1,g' /proc/meminfo)/1024" | bc) | |
docker_image_memory_free=$(echo "scale=1; $(sed '/^MemFree/!d; s,^.*:\s*\([0-9]*\)\s*.*,\1,g' /proc/meminfo)/1024" | bc) | |
# cgroupsv1 | |
if [ -f /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then | |
if [ -f /sys/fs/cgroup/memory/memory.limit_in_bytes ] && [ -f /sys/fs/cgroup/memory/memory.usage_in_bytes ]; then | |
docker_memory_total=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) | |
docker_memory_usage=$(cat /sys/fs/cgroup/memory/memory.usage_in_bytes) | |
if [ "$docker_memory_total" -lt 8000000000000 ]; then | |
docker_image_memory_total=$(echo "scale=1; $docker_memory_total/1024/1024" | bc) | |
docker_image_memory_free=$(echo "scale=1; ($docker_memory_total-$docker_memory_usage)/1024/1024" | bc) | |
DOCKER_MEMCONSTRAINT=1 | |
fi | |
fi | |
fi | |
# cgroupsv2 | |
if [ -f /sys/fs/cgroup/memory.max ]; then | |
docker_memory_total=$(cat /sys/fs/cgroup/memory.max) | |
if [ "$docker_memory_total" != "max" ]; then | |
docker_image_memory_total=$(echo "scale=1; $docker_memory_total/1024/1024" | bc) | |
docker_memory_usage=$(cat /sys/fs/cgroup/memory.current) | |
docker_image_memory_free=$(echo "scale=1; ($docker_memory_total-$docker_memory_usage)/1024/1024" | bc) | |
DOCKER_MEMCONSTRAINT=1 | |
fi | |
fi | |
docker_image_memory_percent=$(echo "scale=1; ($docker_image_memory_free)*100/$docker_image_memory_total" | bc) | |
export DOCKER_MEMTOTAL | |
DOCKER_MEMTOTAL="$docker_image_memory_total" | |
export DOCKER_MEMTOTAL_INT | |
DOCKER_MEMTOTAL_INT="$(echo "$docker_image_memory_total" | sed 's,^\([0-9]*\).*$,\1,g')" | |
export DOCKER_MEMFREE | |
DOCKER_MEMFREE="$docker_image_memory_free" | |
export DOCKER_MEMFREE_INT | |
DOCKER_MEMFREE_INT="$(echo "$docker_image_memory_free" | sed 's,^\([0-9]*\).*$,\1,g')" | |
export DOCKER_MEMPERCFREE | |
DOCKER_MEMPERCFREE="$docker_image_memory_percent" | |
[ "$#" -gt 0 ] || echo "DOCKER_MEMCONSTRAINT=$DOCKER_MEMCONSTRAINT" | |
[ "$#" -gt 0 ] || echo "DOCKER_MEMTOTAL=$DOCKER_MEMTOTAL" | |
[ "$#" -gt 0 ] || echo "DOCKER_MEMTOTAL_INT=$DOCKER_MEMTOTAL_INT" | |
[ "$#" -gt 0 ] || echo "DOCKER_MEMFREE=$DOCKER_MEMFREE" | |
[ "$#" -gt 0 ] || echo "DOCKER_MEMFREE_INT=$DOCKER_MEMFREE_INT" | |
[ "$#" -gt 0 ] || echo "DOCKER_MEMPERCFREE=$DOCKER_MEMPERCFREE" | |
#for v in CGROUPS DOCKER_CID DOCKER_CPUCONSTRAINT DOCKER_CPU DOCKER_CPU_INT DOCKER_MEMCONSTRAINT DOCKER_MEMTOTAL DOCKER_MEMTOTAL_INT DOCKER_MEMFREE DOCKER_MEMFREE_INT DOCKER_MEMPERCFREE ; do | |
# echo $v=${!v} | |
#done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment