Last active
October 17, 2024 15:13
-
-
Save pkellner/58238c16925e49e21f323bb58383dcec to your computer and use it in GitHub Desktop.
dockerprofiler grouped (done with o1-preview)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Define group prefixes | |
groups="better-rules-com better-rules-net gmail-to-icloud-net coolify" | |
group_totals="0 0 0 0" | |
unassigned_total=0 | |
# Create temporary files | |
stats_file=$(mktemp) | |
temp_file=$(mktemp) | |
# Capture the stats for all containers | |
docker stats --no-stream --format "{{.Container}} {{.Name}} {{.CPUPerc}} {{.MemUsage}}" > "$stats_file" | |
# Function to convert memory to MiB | |
convert_memory_to_mib() { | |
mem_str=$1 | |
# Extract the used memory before the '/' | |
mem_used=$(echo "$mem_str" | awk -F'/' '{print $1}' | xargs) | |
mem_value=$(echo "$mem_used" | grep -Eo '^[0-9.]+') | |
mem_unit=$(echo "$mem_used" | grep -Eo '[A-Za-z]+$') | |
# Convert GiB to MiB or handle MiB directly | |
if [ "$mem_unit" = "GiB" ]; then | |
echo "$(awk "BEGIN {print $mem_value * 1024}")" # Convert GiB to MiB | |
elif [ "$mem_unit" = "MiB" ]; then | |
echo "$mem_value" # Already in MiB | |
else | |
echo "0" # Default to 0 for unknown or unsupported units | |
fi | |
} | |
# Function to determine the group based on container name | |
determine_group() { | |
container_name=$1 | |
group_index=0 | |
for group in $groups; do | |
if echo "$container_name" | grep -q "^$group"; then | |
echo "$group_index" | |
return | |
fi | |
group_index=$((group_index + 1)) | |
done | |
echo "unassigned" | |
} | |
# Prepare data | |
while read -r line; do | |
container_id=$(echo "$line" | awk '{print $1}') | |
container_name=$(echo "$line" | awk '{print $2}') | |
cpu_usage=$(echo "$line" | awk '{print $3}') | |
# Capture the entire MemUsage field | |
memory_usage=$(echo "$line" | awk '{$1=$2=$3=""; print $0}' | xargs) | |
# Determine the group | |
group=$(determine_group "$container_name") | |
# Convert memory to MiB and add it to the group's total | |
mem_in_mib=$(convert_memory_to_mib "$memory_usage") | |
if [ "$group" = "unassigned" ]; then | |
unassigned_total=$(awk "BEGIN {print $unassigned_total + $mem_in_mib}") | |
else | |
current_total=$(echo $group_totals | cut -d' ' -f$((group + 1))) | |
new_total=$(awk "BEGIN {print $current_total + $mem_in_mib}") | |
group_totals=$(echo "$group_totals" | awk -v n=$((group + 1)) -v new_total="$new_total" '{ $n = new_total }1') | |
fi | |
# Store the data in a temporary file for sorting | |
echo "$container_id|$container_name|$cpu_usage|$memory_usage|$group" >> "$temp_file" | |
done < "$stats_file" | |
# Sort containers by group and then by name | |
sorted_file=$(mktemp) | |
sort -t'|' -k5,5 -k2,2 "$temp_file" > "$sorted_file" | |
# Print the containers grouped by name prefix | |
group_index=0 | |
for group in $groups unassigned; do | |
echo "$group:" # Group header | |
while IFS='|' read -r container_id container_name cpu_usage memory_usage current_group; do | |
if [ "$current_group" = "$group_index" ] || ([ "$group" = "unassigned" ] && [ "$current_group" = "unassigned" ]); then | |
echo " $container_name ($container_id) - CPU: $cpu_usage, Memory: $memory_usage" | |
fi | |
done < "$sorted_file" | |
echo # Blank line after each group | |
group_index=$((group_index + 1)) | |
done | |
# Print the memory usage by group | |
echo "Memory Usage by Group:" | |
group_index=0 | |
for group in $groups; do | |
total_mem=$(echo $group_totals | cut -d' ' -f$((group_index + 1))) | |
printf "%s: %.2f MiB\n" "$group" "$total_mem" | |
group_index=$((group_index + 1)) | |
done | |
printf "%s: %.2f MiB\n" "unassigned" "$unassigned_total" | |
# Print total memory usage | |
total_memory=$(awk "BEGIN {print $unassigned_total + $(echo $group_totals | tr ' ' '+')}") | |
echo "--------------------------------------" | |
printf "Total Memory Usage: %.2f MiB\n" "$total_memory" | |
# Clean up temporary files | |
rm "$stats_file" "$temp_file" "$sorted_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment