show-ram.sh: Fix to show node paths like show-status

- Use COMPOSE_FILE from .env to get list of nodes
- Match containers by service labels from compose files
- Output format now matches show-status (node paths, not container IDs)
- Sorted by RAM usage descending

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
rob
2026-02-07 10:13:47 +00:00
parent f92f80abd0
commit 413ca7ccd4

View File

@@ -6,6 +6,8 @@
# With argument: shows containers for specific node # With argument: shows containers for specific node
BASEPATH="$(dirname "$0")" BASEPATH="$(dirname "$0")"
source $BASEPATH/.env
NODE_PATH="$1" NODE_PATH="$1"
# Function to convert memory string to MiB # Function to convert memory string to MiB
@@ -33,49 +35,80 @@ format_size() {
fi fi
} }
# Function to get RAM for a compose file by matching service names
get_compose_ram() {
local compose_file="$1"
local total=0
# Get service names defined in this compose file
services=$(cat "$compose_file" | yaml2json - 2>/dev/null | jq -r '.services | keys[]' 2>/dev/null)
[ -z "$services" ] && echo "0" && return
for service in $services; do
# Find container by service label
cid=$(docker ps -q --filter "label=com.docker.compose.service=$service" 2>/dev/null)
[ -z "$cid" ] && continue
mem_usage=$(docker stats --no-stream --format "{{.MemUsage}}" "$cid" 2>/dev/null | awk -F'/' '{print $1}')
[ -z "$mem_usage" ] && continue
mem_mib=$(to_mib "$mem_usage")
total=$(echo "$total + $mem_mib" | bc)
done
echo "$total"
}
# Blacklist - skip infrastructure compose files
blacklist=("drpc.yml" "drpc-free.yml" "drpc-home.yml" "base.yml" "rpc.yml" "monitoring.yml" "ftp.yml" "backup-http.yml")
is_blacklisted() {
local part="$1"
for word in "${blacklist[@]}"; do
if echo "$part" | grep -qE "$word"; then
return 0
fi
done
return 1
}
if [ -z "$NODE_PATH" ]; then if [ -z "$NODE_PATH" ]; then
# No argument: show RAM per node (compose project) # No argument: show RAM per node using COMPOSE_FILE from .env
echo "RAM usage per node:" echo "RAM usage per node:"
echo "========================================" echo "========================================"
# Find all compose files that have running containers IFS=':' read -ra parts <<< "$COMPOSE_FILE"
declare -A node_ram declare -A node_ram
total_mib=0
# Get all running containers with their compose project for part in "${parts[@]}"; do
while IFS= read -r line; do # Skip blacklisted files
container_id=$(echo "$line" | awk '{print $1}') is_blacklisted "$part" && continue
[ -z "$container_id" ] && continue
# Get compose project label compose_file="${BASEPATH}/${part}"
project=$(docker inspect "$container_id" --format '{{index .Config.Labels "com.docker.compose.project"}}' 2>/dev/null) [ ! -f "$compose_file" ] && continue
[ -z "$project" ] && continue
# Get memory usage # Get node path (remove .yml extension)
mem_usage=$(docker stats --no-stream --format "{{.MemUsage}}" "$container_id" 2>/dev/null | awk -F'/' '{print $1}') node_path="${part%.yml}"
[ -z "$mem_usage" ] && continue
mem_mib=$(to_mib "$mem_usage") # Get RAM usage
ram_mib=$(get_compose_ram "$compose_file")
[ "$ram_mib" = "0" ] && continue
# Add to node total node_ram["$node_path"]="$ram_mib"
if [ -z "${node_ram[$project]}" ]; then total_mib=$(echo "$total_mib + $ram_mib" | bc)
node_ram[$project]="$mem_mib" done
else
node_ram[$project]=$(echo "${node_ram[$project]} + $mem_mib" | bc)
fi
done < <(docker ps --format "{{.ID}}")
if [ ${#node_ram[@]} -eq 0 ]; then if [ ${#node_ram[@]} -eq 0 ]; then
echo "No running containers found" echo "No running nodes found"
exit 0 exit 0
fi fi
# Sort by RAM usage and display # Sort by RAM usage and display
total_mib=0 for node_path in "${!node_ram[@]}"; do
for project in "${!node_ram[@]}"; do echo "${node_ram[$node_path]} $node_path"
echo "${node_ram[$project]} $project"
total_mib=$(echo "$total_mib + ${node_ram[$project]}" | bc)
done | sort -rn | while read mib name; do done | sort -rn | while read mib name; do
printf "%-50s %s\n" "$name" "$(format_size $mib)" printf "%-55s %s\n" "$name" "$(format_size $mib)"
done done
echo "========================================" echo "========================================"
@@ -87,38 +120,39 @@ if [ -z "$NODE_PATH" ]; then
echo "Server RAM: ${used_server_ram} / ${total_server_ram}" echo "Server RAM: ${used_server_ram} / ${total_server_ram}"
else else
# Specific node - path maps directly to compose file # Specific node - path maps directly to compose file
# e.g., "op/reth/base-mainnet-op-reth-archive-trace" -> "op/reth/base-mainnet-op-reth-archive-trace.yml" COMPOSE_FILE_PATH="${BASEPATH}/${NODE_PATH}.yml"
COMPOSE_FILE="${BASEPATH}/${NODE_PATH}.yml"
COMPOSE_NAME=$(basename "$NODE_PATH")
if [ ! -f "$COMPOSE_FILE" ]; then if [ ! -f "$COMPOSE_FILE_PATH" ]; then
echo "Error: Compose file not found: $COMPOSE_FILE" echo "Error: Compose file not found: $COMPOSE_FILE_PATH"
exit 1 exit 1
fi fi
# Get container IDs for this compose project # Get service names from compose file
CONTAINER_IDS=$(docker compose -f "$COMPOSE_FILE" ps -q 2>/dev/null) services=$(cat "$COMPOSE_FILE_PATH" | yaml2json - 2>/dev/null | jq -r '.services | keys[]' 2>/dev/null)
if [ -z "$CONTAINER_IDS" ]; then if [ -z "$services" ]; then
echo "No running containers found for $COMPOSE_NAME" echo "No services found in $NODE_PATH"
exit 0 exit 0
fi fi
echo "RAM usage for $COMPOSE_NAME:" echo "RAM usage for $NODE_PATH:"
echo "----------------------------------------" echo "----------------------------------------"
# Show stats for each container
docker stats --no-stream --format "{{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" $CONTAINER_IDS
echo "----------------------------------------"
# Calculate total
total_mib=0 total_mib=0
for cid in $CONTAINER_IDS; do for service in $services; do
mem_usage=$(docker stats --no-stream --format "{{.MemUsage}}" "$cid" 2>/dev/null | awk -F'/' '{print $1}') cid=$(docker ps -q --filter "label=com.docker.compose.service=$service" 2>/dev/null)
[ -z "$cid" ] && continue
mem_info=$(docker stats --no-stream --format "{{.MemUsage}}\t{{.MemPerc}}" "$cid" 2>/dev/null)
mem_usage=$(echo "$mem_info" | awk -F'\t' '{print $1}' | awk -F'/' '{print $1}')
mem_perc=$(echo "$mem_info" | awk -F'\t' '{print $2}')
printf "%-40s %s\t%s\n" "$service" "$mem_usage" "$mem_perc"
mem_mib=$(to_mib "$mem_usage") mem_mib=$(to_mib "$mem_usage")
total_mib=$(echo "$total_mib + $mem_mib" | bc) total_mib=$(echo "$total_mib + $mem_mib" | bc)
done done
echo "----------------------------------------"
echo "Total: $(format_size $total_mib)" echo "Total: $(format_size $total_mib)"
fi fi