show-ram.sh: Fix node_path handling, group by node
- Fix: handle full path like ethereum/geth/node-name (extract basename) - Without args: show RAM per node (grouped by compose project), not per container - Sorted by RAM usage descending Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
111
show-ram.sh
111
show-ram.sh
@@ -1,63 +1,94 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Show RAM usage for containers
|
# Show RAM usage for containers
|
||||||
# Usage: ./show-ram.sh [compose-file-name]
|
# Usage: ./show-ram.sh [node-path]
|
||||||
# Without argument: shows all containers and total server RAM
|
# Without argument: shows RAM per node and total server RAM
|
||||||
# With argument: shows containers for specific node
|
# With argument: shows containers for specific node
|
||||||
|
|
||||||
BASEPATH="$(dirname "$0")"
|
BASEPATH="$(dirname "$0")"
|
||||||
COMPOSE_NAME="$1"
|
NODE_PATH="$1"
|
||||||
|
|
||||||
# Function to calculate and print total from container IDs
|
# Function to convert memory string to MiB
|
||||||
calc_total() {
|
to_mib() {
|
||||||
local container_ids="$1"
|
local val="$1"
|
||||||
local total_mib=$(docker stats --no-stream --format "{{.MemUsage}}" $container_ids | \
|
local num=$(echo "$val" | sed 's/[^0-9.]//g')
|
||||||
awk -F'/' '{
|
if echo "$val" | grep -qi "GiB"; then
|
||||||
val=$1;
|
echo "$num * 1024" | bc
|
||||||
gsub(/[^0-9.]/,"",val);
|
elif echo "$val" | grep -qi "MiB"; then
|
||||||
if($1 ~ /GiB/) val*=1024;
|
echo "$num"
|
||||||
sum+=val
|
elif echo "$val" | grep -qi "KiB"; then
|
||||||
} END {printf "%.0f", sum}')
|
echo "$num / 1024" | bc
|
||||||
|
|
||||||
if [ "$total_mib" -ge 1024 ] 2>/dev/null; then
|
|
||||||
local total_gib=$(echo "scale=2; $total_mib / 1024" | bc)
|
|
||||||
echo "${total_gib} GiB"
|
|
||||||
else
|
else
|
||||||
echo "${total_mib} MiB"
|
echo "$num"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ -z "$COMPOSE_NAME" ]; then
|
# Function to format MiB to human readable
|
||||||
# No argument: show all containers grouped by compose project
|
format_size() {
|
||||||
echo "RAM usage for all nodes:"
|
local mib="$1"
|
||||||
|
if [ "${mib%.*}" -ge 1024 ] 2>/dev/null; then
|
||||||
|
echo "scale=2; $mib / 1024" | bc | xargs printf "%.2f GiB"
|
||||||
|
else
|
||||||
|
printf "%.0f MiB" "$mib"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ -z "$NODE_PATH" ]; then
|
||||||
|
# No argument: show RAM per node (compose project)
|
||||||
|
echo "RAM usage per node:"
|
||||||
echo "========================================"
|
echo "========================================"
|
||||||
|
|
||||||
# Get all running containers
|
# Find all compose files that have running containers
|
||||||
ALL_CONTAINERS=$(docker ps -q 2>/dev/null)
|
declare -A node_ram
|
||||||
|
|
||||||
if [ -z "$ALL_CONTAINERS" ]; then
|
# 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
|
||||||
|
|
||||||
|
# Get compose project label
|
||||||
|
project=$(docker inspect "$container_id" --format '{{index .Config.Labels "com.docker.compose.project"}}' 2>/dev/null)
|
||||||
|
[ -z "$project" ] && 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
|
||||||
|
|
||||||
|
mem_mib=$(to_mib "$mem_usage")
|
||||||
|
|
||||||
|
# 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}}")
|
||||||
|
|
||||||
|
if [ ${#node_ram[@]} -eq 0 ]; then
|
||||||
echo "No running containers found"
|
echo "No running containers found"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Show stats for all containers, sorted by memory usage (descending)
|
# Sort by RAM usage and display
|
||||||
docker stats --no-stream --format "table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" $ALL_CONTAINERS | \
|
total_mib=0
|
||||||
head -1
|
for project in "${!node_ram[@]}"; do
|
||||||
docker stats --no-stream --format "{{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" $ALL_CONTAINERS | \
|
echo "${node_ram[$project]} $project"
|
||||||
sort -t$'\t' -k2 -h -r
|
total_mib=$(echo "$total_mib + ${node_ram[$project]}" | bc)
|
||||||
|
done | sort -rn | while read mib name; do
|
||||||
|
printf "%-50s %s\n" "$name" "$(format_size $mib)"
|
||||||
|
done
|
||||||
|
|
||||||
echo "========================================"
|
echo "========================================"
|
||||||
|
echo "Total container RAM: $(format_size $total_mib)"
|
||||||
# Calculate total
|
|
||||||
echo -n "Total container RAM: "
|
|
||||||
calc_total "$ALL_CONTAINERS"
|
|
||||||
|
|
||||||
# Show server total RAM
|
# Show server total RAM
|
||||||
total_server_ram=$(free -h | awk '/^Mem:/ {print $2}')
|
total_server_ram=$(free -h | awk '/^Mem:/ {print $2}')
|
||||||
used_server_ram=$(free -h | awk '/^Mem:/ {print $3}')
|
used_server_ram=$(free -h | awk '/^Mem:/ {print $3}')
|
||||||
echo "Server RAM: ${used_server_ram} / ${total_server_ram}"
|
echo "Server RAM: ${used_server_ram} / ${total_server_ram}"
|
||||||
else
|
else
|
||||||
# Specific node
|
# Specific node - extract compose name from path
|
||||||
|
# Handle both "ethereum-mainnet-geth-archive" and "ethereum/geth/ethereum-mainnet-geth-archive"
|
||||||
|
COMPOSE_NAME=$(basename "$NODE_PATH")
|
||||||
COMPOSE_FILE="${BASEPATH}/${COMPOSE_NAME}.yml"
|
COMPOSE_FILE="${BASEPATH}/${COMPOSE_NAME}.yml"
|
||||||
|
|
||||||
if [ ! -f "$COMPOSE_FILE" ]; then
|
if [ ! -f "$COMPOSE_FILE" ]; then
|
||||||
@@ -80,6 +111,14 @@ else
|
|||||||
docker stats --no-stream --format "{{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" $CONTAINER_IDS
|
docker stats --no-stream --format "{{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" $CONTAINER_IDS
|
||||||
|
|
||||||
echo "----------------------------------------"
|
echo "----------------------------------------"
|
||||||
echo -n "Total: "
|
|
||||||
calc_total "$CONTAINER_IDS"
|
# 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}')
|
||||||
|
mem_mib=$(to_mib "$mem_usage")
|
||||||
|
total_mib=$(echo "$total_mib + $mem_mib" | bc)
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "Total: $(format_size $total_mib)"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user