From ba6848e113479546c26d369ecf7ab160c339551c Mon Sep 17 00:00:00 2001 From: Developer Date: Mon, 20 Apr 2026 19:05:23 -0400 Subject: [PATCH] CRITICAL FIX: traffic percentage calculation - use peak concurrent instead of log parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The old approach counted lines from ALL files in a log directory and divided one domain's requests by that massive total. This gave every domain wrong percentages like 2% when they should be 80-99%. NEW APPROACH: Use peak concurrent values directly - Peak concurrent is a reliable indicator of traffic intensity - Calculate: domain_peak / sum_of_all_peaks * 100 - Much more accurate than trying to parse logs across different control panels Example: - Domain A peak: 421 concurrent -> 99% of server traffic ✅ - Domain B peak: 2 concurrent -> 1% of server traffic ✅ This makes far more sense than the old broken approach. --- lib/php-calculator-improved.sh | 83 +++++++++++----------------------- 1 file changed, 26 insertions(+), 57 deletions(-) diff --git a/lib/php-calculator-improved.sh b/lib/php-calculator-improved.sh index b9d36af..db4a589 100644 --- a/lib/php-calculator-improved.sh +++ b/lib/php-calculator-improved.sh @@ -413,69 +413,38 @@ get_domain_traffic_percentage() { domain_count=$(echo "$all_domains" | grep -v "^$" | wc -l) [ "$domain_count" -lt 1 ] && domain_count=1 - # SIMPLIFIED APPROACH: Use equal distribution by default - # (Advanced traffic analysis requires domain-specific access logs which are control-panel dependent) - local equal_share=$((100 / domain_count)) + # CRITICAL FIX: Use peak concurrent to estimate traffic percentage + # (Access log parsing is unreliable across control panels) + # Peak concurrent is a reliable indicator of traffic intensity - # Try to find domain-specific access log (best effort, not required) - # This varies by control panel: - # - cPanel: /var/log/apache2/domlogs/DOMAIN or /home/user/logs/DOMAIN-access_log - # - Plesk: /var/www/vhosts/DOMAIN/logs/access_log - # - InterWorx: /home/user/var/DOMAIN/logs/transfer.log + # Get this domain's peak concurrent + local domain_peak + domain_peak=$(get_domain_peak_concurrent "$domain" 2>/dev/null || echo "0") + [ -z "$domain_peak" ] && domain_peak=0 - local domain_requests=0 - local total_requests=0 + # Calculate total peak concurrent across ALL domains + local total_peak=0 + local domain_check + while IFS= read -r domain_check; do + [ -z "$domain_check" ] && continue + local peak_val + peak_val=$(get_domain_peak_concurrent "$domain_check" 2>/dev/null || echo "0") + [ -z "$peak_val" ] && peak_val=0 + total_peak=$((total_peak + peak_val)) + done <<< "$all_domains" - # Best-effort search for domain access log - local access_log - access_log=$(find /var/log/apache2/domlogs -name "*$domain*" -type f 2>/dev/null | head -1) - - if [ -z "$access_log" ]; then - access_log=$(find /var/www/vhosts -name "access_log" -path "*$domain*" -type f 2>/dev/null | head -1) - fi - - if [ -z "$access_log" ]; then - access_log=$(find /home -name "transfer.log" -path "*$domain*" -type f 2>/dev/null | head -1) - fi - - # If we found a domain-specific log, try to calculate percentage - if [ -n "$access_log" ] && [ -f "$access_log" ]; then - # Count lines in this domain's access log - domain_requests=$(tail -n 100000 "$access_log" 2>/dev/null | wc -l) - - # Only use calculated percentage if we have significant data - if [ "$domain_requests" -gt 10 ]; then - # IMPROVED: Try to sum requests from multiple domain logs (not just 2) - # This is more accurate for multi-domain servers - total_requests=0 - local log_count=0 - - # Find all domain logs in the same location as our domain log - local log_dir - log_dir=$(dirname "$access_log") - - # Count requests from all logs in this directory - while IFS= read -r log; do - [ -f "$log" ] || continue - local log_requests - log_requests=$(tail -n 100000 "$log" 2>/dev/null | wc -l) - total_requests=$((total_requests + log_requests)) - log_count=$((log_count + 1)) - done < <(find "$log_dir" -maxdepth 1 -type f -name "*" 2>/dev/null | head -20) - - # Only calculate percentage if we found at least 2 logs - if [ "$log_count" -ge 2 ] && [ "$total_requests" -gt 0 ]; then - local percentage=$((domain_requests * 100 / total_requests)) - [ "$percentage" -lt 1 ] && percentage=1 - [ "$percentage" -gt 99 ] && percentage=99 - echo "$percentage|$domain_requests|Based on $log_count domain logs" - return - fi - fi + # Calculate percentage based on peak concurrent + if [ "$total_peak" -gt 0 ]; then + local percentage=$((domain_peak * 100 / total_peak)) + [ "$percentage" -lt 1 ] && percentage=1 + [ "$percentage" -gt 99 ] && percentage=99 + echo "$percentage|$domain_peak|Based on peak concurrent (traffic intensity)" + return fi # Fallback: equal distribution among all domains - # This is the SAFEST approach when we can't reliably calculate percentages + # This is the SAFEST approach when we can't calculate percentages + local equal_share=$((100 / domain_count)) echo "$equal_share|0|Using equal distribution ($domain_count domains) - safest assumption" }