From 9f2a0cdbe81f3c5547e5d6f1305acb0085dc46d2 Mon Sep 17 00:00:00 2001 From: cschantz Date: Thu, 11 Dec 2025 21:29:56 -0500 Subject: [PATCH] Fix 2 additional critical bugs in PHP scripts BUG #7: php-optimizer.sh - Undefined variable in optimize_domain() Location: modules/performance/php-optimizer.sh:507 Problem: Variable current_max_children was scoped inside if block (line 436) but used outside the if block (line 507), causing undefined variable Solution: Moved declaration to line 435, before the if block Impact: optimize_domain() would fail when trying to apply changes BUG #8: php-analyzer.sh - calculate_memory_per_process() format mismatch Location: lib/php-analyzer.sh:196-218 Problem: Function called get_fpm_memory_usage() expecting "kb|mb" format but get_fpm_memory_usage() returns only a single number (avg KB) This caused total_mb to always be empty Solution: Fixed to: 1. Accept single number from get_fpm_memory_usage() 2. Get process_count separately 3. Calculate total_mb = (avg_kb * process_count / 1024) Impact: All memory calculations were wrong, showing 0 total memory VERIFICATION: - calculate_memory_per_process now correctly returns: avg_kb|count|total_mb - optimize_domain can now access current_max_children when applying changes - Memory statistics will show accurate values --- lib/php-analyzer.sh | 21 ++++++++++++--------- modules/performance/php-optimizer.sh | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/php-analyzer.sh b/lib/php-analyzer.sh index 321a257..89a85a0 100644 --- a/lib/php-analyzer.sh +++ b/lib/php-analyzer.sh @@ -199,20 +199,23 @@ analyze_execution_timeout_errors() { calculate_memory_per_process() { local username="$1" - local memory_stats - memory_stats=$(get_fpm_memory_usage "$username") + # Get average KB per process (single number) + local avg_kb + avg_kb=$(get_fpm_memory_usage "$username") - if [ -z "$memory_stats" ] || [[ "$memory_stats" == "0|0" ]]; then + # Get process count + local process_count + process_count=$(get_fpm_process_count "$username") + + # Check if no processes found + if [ -z "$avg_kb" ] || [ "$avg_kb" -eq 0 ] || [ "$process_count" -eq 0 ]; then echo "0|0|0" return fi - local avg_kb total_mb - avg_kb=$(echo "$memory_stats" | cut -d'|' -f1) - total_mb=$(echo "$memory_stats" | cut -d'|' -f2) - - local process_count - process_count=$(get_fpm_process_count "$username") + # Calculate total memory in MB + local total_mb + total_mb=$((avg_kb * process_count / 1024)) echo "$avg_kb|$process_count|$total_mb" } diff --git a/modules/performance/php-optimizer.sh b/modules/performance/php-optimizer.sh index a79f28b..0a53936 100755 --- a/modules/performance/php-optimizer.sh +++ b/modules/performance/php-optimizer.sh @@ -432,8 +432,8 @@ optimize_domain() { local pool_config pool_config=$(find_fpm_pool_config "$username") + local current_max_children="" if [ -n "$pool_config" ] && [ -f "$pool_config" ]; then - local current_max_children current_max_children=$(grep "^pm.max_children" "$pool_config" | awk -F'=' '{print $2}' | tr -d ' ') if [ -n "$current_max_children" ] && [ "$recommended_max_children" -ne "$current_max_children" ]; then