From 9ce4b5cd0ac432825fb80e23badfb23324ccbc36 Mon Sep 17 00:00:00 2001 From: rob Date: Sat, 7 Feb 2026 09:38:49 +0000 Subject: [PATCH] show-ram.sh: Show all containers when no node_path given - Without argument: shows all containers sorted by RAM, total container RAM, server RAM - With argument: shows RAM for specific node only Co-Authored-By: Claude Opus 4.5 --- show-ram.sh | 108 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 38 deletions(-) diff --git a/show-ram.sh b/show-ram.sh index 4b4b5ac4..c4b2b632 100755 --- a/show-ram.sh +++ b/show-ram.sh @@ -1,53 +1,85 @@ #!/bin/bash -# Show RAM usage for containers in a compose file -# Usage: ./show-ram.sh -# Example: ./show-ram.sh ethereum-mainnet-geth-archive +# Show RAM usage for containers +# Usage: ./show-ram.sh [compose-file-name] +# Without argument: shows all containers and total server RAM +# With argument: shows containers for specific node BASEPATH="$(dirname "$0")" COMPOSE_NAME="$1" +# Function to calculate and print total from container IDs +calc_total() { + local container_ids="$1" + local 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 ] 2>/dev/null; then + local total_gib=$(echo "scale=2; $total_mib / 1024" | bc) + echo "${total_gib} GiB" + else + echo "${total_mib} MiB" + fi +} + if [ -z "$COMPOSE_NAME" ]; then - echo "Usage: ./show-ram.sh " - echo "Example: ./show-ram.sh ethereum-mainnet-geth-archive" - exit 1 -fi + # No argument: show all containers grouped by compose project + echo "RAM usage for all nodes:" + echo "========================================" -COMPOSE_FILE="${BASEPATH}/${COMPOSE_NAME}.yml" + # Get all running containers + ALL_CONTAINERS=$(docker ps -q 2>/dev/null) -if [ ! -f "$COMPOSE_FILE" ]; then - echo "Error: Compose file not found: $COMPOSE_FILE" - exit 1 -fi + if [ -z "$ALL_CONTAINERS" ]; then + echo "No running containers found" + exit 0 + fi -# Get container IDs for this compose project -CONTAINER_IDS=$(docker compose -f "$COMPOSE_FILE" ps -q 2>/dev/null) + # Show stats for all containers, sorted by memory usage (descending) + docker stats --no-stream --format "table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" $ALL_CONTAINERS | \ + head -1 + docker stats --no-stream --format "{{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" $ALL_CONTAINERS | \ + sort -t$'\t' -k2 -h -r -if [ -z "$CONTAINER_IDS" ]; then - echo "No running containers found for $COMPOSE_NAME" - exit 0 -fi + echo "========================================" -echo "RAM usage for $COMPOSE_NAME:" -echo "----------------------------------------" + # Calculate total + echo -n "Total container RAM: " + calc_total "$ALL_CONTAINERS" -# 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" + # Show server total RAM + total_server_ram=$(free -h | awk '/^Mem:/ {print $2}') + used_server_ram=$(free -h | awk '/^Mem:/ {print $3}') + echo "Server RAM: ${used_server_ram} / ${total_server_ram}" else - echo "Total: ${total_mib} MiB" + # Specific node + 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 "----------------------------------------" + echo -n "Total: " + calc_total "$CONTAINER_IDS" fi