From f7920fc8a9276ee5dc91deb8951807eabad39620 Mon Sep 17 00:00:00 2001 From: cschantz Date: Wed, 3 Dec 2025 01:23:34 -0500 Subject: [PATCH] Fix memory capacity calculation to iterate through domains not just users MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: - calculate_server_memory_capacity() showed '0MB required' - Only iterated through users, called find_fpm_pool_config() with username only - cPanel uses domain-based pool configs (domain.conf not username.conf) - Result: No pools found, 0MB calculated Fix: - Added nested loop: users → domains - Pass both username AND domain to find_fpm_pool_config() - Extract pool name from config file to get actual process memory - Use get_fpm_memory_usage(pool_name) directly instead of calculate_memory_per_process() - Added domain to details output format Changes: - Lines 745-800: Rewrote user iteration to include domain loop - Now correctly finds pools like pickledperil.com.conf - Calculates actual memory usage per pool Result: - Memory capacity analysis now shows real data - Proper OOM risk assessment --- lib/php-analyzer.sh | 65 ++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/php-analyzer.sh b/lib/php-analyzer.sh index 3cd2aaf..3d98bd6 100644 --- a/lib/php-analyzer.sh +++ b/lib/php-analyzer.sh @@ -742,50 +742,61 @@ calculate_server_memory_capacity() { local pool_count=0 local details="" - # Iterate through all users + # Iterate through all users and their domains while IFS= read -r username; do [ -z "$username" ] && continue - # Find FPM pool config - local pool_config - pool_config=$(find_fpm_pool_config "$username") + # Get all domains for this user + local user_domains + user_domains=$(get_user_domains "$username") - if [ -z "$pool_config" ] || [ ! -f "$pool_config" ]; then - continue - fi + while IFS= read -r domain; do + [ -z "$domain" ] && continue - pool_count=$((pool_count + 1)) + # Find FPM pool config for this domain + local pool_config + pool_config=$(find_fpm_pool_config "$username" "$domain") - # Get max_children from pool config - local max_children - max_children=$(grep "^pm.max_children" "$pool_config" | awk -F'=' '{print $2}' | tr -d ' ') + if [ -z "$pool_config" ] || [ ! -f "$pool_config" ]; then + continue + fi - if [ -z "$max_children" ] || [ "$max_children" -eq 0 ]; then - max_children=5 # Safe default if not set - fi + pool_count=$((pool_count + 1)) - # Get average memory per process - local memory_stats - memory_stats=$(calculate_memory_per_process "$username") + # Get max_children from pool config + local max_children + max_children=$(grep "^pm.max_children" "$pool_config" | awk -F'=' '{print $2}' | tr -d ' ') - local avg_kb - avg_kb=$(echo "$memory_stats" | cut -d'|' -f1) + if [ -z "$max_children" ] || [ "$max_children" -eq 0 ]; then + max_children=5 # Safe default if not set + fi - if [ "$avg_kb" -eq 0 ]; then + # Get average memory per process from pool name + local pool_name + pool_name=$(grep "^\[" "$pool_config" | tr -d '[]' | head -1) + + # Get memory usage for this specific pool + local avg_kb=0 + if [ -n "$pool_name" ]; then + avg_kb=$(get_fpm_memory_usage "$pool_name") + fi + + if [ -z "$avg_kb" ] || [ "$avg_kb" -eq 0 ]; then # No active processes, estimate 50MB per process (conservative) avg_kb=$((50 * 1024)) fi - local avg_mb=$((avg_kb / 1024)) + local avg_mb=$((avg_kb / 1024)) - # Calculate max memory for this pool - local pool_max_mb=$((max_children * avg_mb)) - total_required_mb=$((total_required_mb + pool_max_mb)) - total_max_children=$((total_max_children + max_children)) + # Calculate max memory for this pool + local pool_max_mb=$((max_children * avg_mb)) + total_required_mb=$((total_required_mb + pool_max_mb)) + total_max_children=$((total_max_children + max_children)) - # Add to details - details+="$username|$max_children|${avg_mb}MB|${pool_max_mb}MB"$'\n' + # Add to details + details+="$domain|$username|$max_children|${avg_mb}MB|${pool_max_mb}MB"$'\n' + done <<< "$user_domains" done <<< "$users" # Calculate percentage