Created
July 3, 2024 04:34
-
-
Save edr3x/756f168ee9c619dae8c0a7989dee1e55 to your computer and use it in GitHub Desktop.
Get absolute CPU usae of container
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
#!/usr/bin/env bash | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <container-id>" | |
exit 1 | |
fi | |
CONTAINER_ID=$1 | |
CPU_PERCENT=$(docker stats $CONTAINER_ID --no-stream --format "{{.CPUPerc}}" | tr -d '%') | |
echo "CPU_PERCENT: $CPU_PERCENT" | |
if [ -z "$CPU_PERCENT" ]; then | |
echo "Could not retrieve CPU usage for container $CONTAINER_ID" | |
exit 1 | |
fi | |
TOTAL_CORES=$(nproc) | |
echo "TOTAL_CORES: $TOTAL_CORES" | |
TOTAL_CAPACITY=$(echo "$TOTAL_CORES * 100" | bc) | |
echo "TOTAL_CAPACITY: $TOTAL_CAPACITY" | |
ABSOLUTE_CPU_USAGE=$(echo "scale=2; $CPU_PERCENT / 100 * $TOTAL_CAPACITY" | bc) | |
echo "Absolute CPU usage of container $CONTAINER_ID: $ABSOLUTE_CPU_USAGE CPU units" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment