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