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:
122
show-ram.sh
122
show-ram.sh
@@ -6,6 +6,8 @@
|
||||
# With argument: shows containers for specific node
|
||||
|
||||
BASEPATH="$(dirname "$0")"
|
||||
source $BASEPATH/.env
|
||||
|
||||
NODE_PATH="$1"
|
||||
|
||||
# Function to convert memory string to MiB
|
||||
@@ -33,49 +35,80 @@ format_size() {
|
||||
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
|
||||
# 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 "========================================"
|
||||
|
||||
# Find all compose files that have running containers
|
||||
IFS=':' read -ra parts <<< "$COMPOSE_FILE"
|
||||
|
||||
declare -A node_ram
|
||||
total_mib=0
|
||||
|
||||
# Get all running containers with their compose project
|
||||
while IFS= read -r line; do
|
||||
container_id=$(echo "$line" | awk '{print $1}')
|
||||
[ -z "$container_id" ] && continue
|
||||
for part in "${parts[@]}"; do
|
||||
# Skip blacklisted files
|
||||
is_blacklisted "$part" && continue
|
||||
|
||||
# Get compose project label
|
||||
project=$(docker inspect "$container_id" --format '{{index .Config.Labels "com.docker.compose.project"}}' 2>/dev/null)
|
||||
[ -z "$project" ] && continue
|
||||
compose_file="${BASEPATH}/${part}"
|
||||
[ ! -f "$compose_file" ] && continue
|
||||
|
||||
# Get memory usage
|
||||
mem_usage=$(docker stats --no-stream --format "{{.MemUsage}}" "$container_id" 2>/dev/null | awk -F'/' '{print $1}')
|
||||
[ -z "$mem_usage" ] && continue
|
||||
# Get node path (remove .yml extension)
|
||||
node_path="${part%.yml}"
|
||||
|
||||
mem_mib=$(to_mib "$mem_usage")
|
||||
# Get RAM usage
|
||||
ram_mib=$(get_compose_ram "$compose_file")
|
||||
[ "$ram_mib" = "0" ] && continue
|
||||
|
||||
# Add to node total
|
||||
if [ -z "${node_ram[$project]}" ]; then
|
||||
node_ram[$project]="$mem_mib"
|
||||
else
|
||||
node_ram[$project]=$(echo "${node_ram[$project]} + $mem_mib" | bc)
|
||||
fi
|
||||
done < <(docker ps --format "{{.ID}}")
|
||||
node_ram["$node_path"]="$ram_mib"
|
||||
total_mib=$(echo "$total_mib + $ram_mib" | bc)
|
||||
done
|
||||
|
||||
if [ ${#node_ram[@]} -eq 0 ]; then
|
||||
echo "No running containers found"
|
||||
echo "No running nodes found"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Sort by RAM usage and display
|
||||
total_mib=0
|
||||
for project in "${!node_ram[@]}"; do
|
||||
echo "${node_ram[$project]} $project"
|
||||
total_mib=$(echo "$total_mib + ${node_ram[$project]}" | bc)
|
||||
for node_path in "${!node_ram[@]}"; do
|
||||
echo "${node_ram[$node_path]} $node_path"
|
||||
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
|
||||
|
||||
echo "========================================"
|
||||
@@ -87,38 +120,39 @@ if [ -z "$NODE_PATH" ]; then
|
||||
echo "Server RAM: ${used_server_ram} / ${total_server_ram}"
|
||||
else
|
||||
# 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="${BASEPATH}/${NODE_PATH}.yml"
|
||||
COMPOSE_NAME=$(basename "$NODE_PATH")
|
||||
COMPOSE_FILE_PATH="${BASEPATH}/${NODE_PATH}.yml"
|
||||
|
||||
if [ ! -f "$COMPOSE_FILE" ]; then
|
||||
echo "Error: Compose file not found: $COMPOSE_FILE"
|
||||
if [ ! -f "$COMPOSE_FILE_PATH" ]; then
|
||||
echo "Error: Compose file not found: $COMPOSE_FILE_PATH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Get container IDs for this compose project
|
||||
CONTAINER_IDS=$(docker compose -f "$COMPOSE_FILE" ps -q 2>/dev/null)
|
||||
# Get service names from compose file
|
||||
services=$(cat "$COMPOSE_FILE_PATH" | yaml2json - 2>/dev/null | jq -r '.services | keys[]' 2>/dev/null)
|
||||
|
||||
if [ -z "$CONTAINER_IDS" ]; then
|
||||
echo "No running containers found for $COMPOSE_NAME"
|
||||
if [ -z "$services" ]; then
|
||||
echo "No services found in $NODE_PATH"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "RAM usage for $COMPOSE_NAME:"
|
||||
echo "RAM usage for $NODE_PATH:"
|
||||
echo "----------------------------------------"
|
||||
|
||||
# Show stats for each container
|
||||
docker stats --no-stream --format "{{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" $CONTAINER_IDS
|
||||
|
||||
echo "----------------------------------------"
|
||||
|
||||
# Calculate total
|
||||
total_mib=0
|
||||
for cid in $CONTAINER_IDS; do
|
||||
mem_usage=$(docker stats --no-stream --format "{{.MemUsage}}" "$cid" 2>/dev/null | awk -F'/' '{print $1}')
|
||||
for service in $services; do
|
||||
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")
|
||||
total_mib=$(echo "$total_mib + $mem_mib" | bc)
|
||||
done
|
||||
|
||||
echo "----------------------------------------"
|
||||
echo "Total: $(format_size $total_mib)"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user