diff --git a/show-ram.sh b/show-ram.sh new file mode 100755 index 00000000..4b4b5ac4 --- /dev/null +++ b/show-ram.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +# Show RAM usage for containers in a compose file +# Usage: ./show-ram.sh +# Example: ./show-ram.sh ethereum-mainnet-geth-archive + +BASEPATH="$(dirname "$0")" +COMPOSE_NAME="$1" + +if [ -z "$COMPOSE_NAME" ]; then + echo "Usage: ./show-ram.sh " + echo "Example: ./show-ram.sh ethereum-mainnet-geth-archive" + exit 1 +fi + +COMPOSE_FILE="${BASEPATH}/${COMPOSE_NAME}.yml" + +if [ ! -f "$COMPOSE_FILE" ]; then + echo "Error: Compose file not found: $COMPOSE_FILE" + exit 1 +fi + +# Get container IDs for this compose project +CONTAINER_IDS=$(docker compose -f "$COMPOSE_FILE" ps -q 2>/dev/null) + +if [ -z "$CONTAINER_IDS" ]; then + echo "No running containers found for $COMPOSE_NAME" + exit 0 +fi + +echo "RAM usage for $COMPOSE_NAME:" +echo "----------------------------------------" + +# Show stats for each container +docker stats --no-stream --format "{{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" $CONTAINER_IDS + +echo "----------------------------------------" + +# Calculate total +total_mib=$(docker stats --no-stream --format "{{.MemUsage}}" $CONTAINER_IDS | \ + awk -F'/' '{ + val=$1; + gsub(/[^0-9.]/,"",val); + if($1 ~ /GiB/) val*=1024; + sum+=val + } END {printf "%.0f", sum}') + +if [ "$total_mib" -ge 1024 ]; then + total_gib=$(echo "scale=2; $total_mib / 1024" | bc) + echo "Total: ${total_gib} GiB" +else + echo "Total: ${total_mib} MiB" +fi