Skip to content

Instantly share code, notes, and snippets.

@edr3x
Created July 3, 2024 04:34
Show Gist options
  • Save edr3x/756f168ee9c619dae8c0a7989dee1e55 to your computer and use it in GitHub Desktop.
Save edr3x/756f168ee9c619dae8c0a7989dee1e55 to your computer and use it in GitHub Desktop.
Get absolute CPU usae of container
#!/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