CRITICAL FIX: traffic percentage calculation - use peak concurrent instead of log parsing

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.
This commit is contained in:
Developer
2026-04-20 19:05:23 -04:00
parent 3a14df27ae
commit ba6848e113
+26 -57
View File
@@ -413,69 +413,38 @@ get_domain_traffic_percentage() {
domain_count=$(echo "$all_domains" | grep -v "^$" | wc -l) domain_count=$(echo "$all_domains" | grep -v "^$" | wc -l)
[ "$domain_count" -lt 1 ] && domain_count=1 [ "$domain_count" -lt 1 ] && domain_count=1
# SIMPLIFIED APPROACH: Use equal distribution by default # CRITICAL FIX: Use peak concurrent to estimate traffic percentage
# (Advanced traffic analysis requires domain-specific access logs which are control-panel dependent) # (Access log parsing is unreliable across control panels)
local equal_share=$((100 / domain_count)) # Peak concurrent is a reliable indicator of traffic intensity
# Try to find domain-specific access log (best effort, not required) # Get this domain's peak concurrent
# This varies by control panel: local domain_peak
# - cPanel: /var/log/apache2/domlogs/DOMAIN or /home/user/logs/DOMAIN-access_log domain_peak=$(get_domain_peak_concurrent "$domain" 2>/dev/null || echo "0")
# - Plesk: /var/www/vhosts/DOMAIN/logs/access_log [ -z "$domain_peak" ] && domain_peak=0
# - InterWorx: /home/user/var/DOMAIN/logs/transfer.log
local domain_requests=0 # Calculate total peak concurrent across ALL domains
local total_requests=0 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 # Calculate percentage based on peak concurrent
local access_log if [ "$total_peak" -gt 0 ]; then
access_log=$(find /var/log/apache2/domlogs -name "*$domain*" -type f 2>/dev/null | head -1) local percentage=$((domain_peak * 100 / total_peak))
[ "$percentage" -lt 1 ] && percentage=1
if [ -z "$access_log" ]; then [ "$percentage" -gt 99 ] && percentage=99
access_log=$(find /var/www/vhosts -name "access_log" -path "*$domain*" -type f 2>/dev/null | head -1) echo "$percentage|$domain_peak|Based on peak concurrent (traffic intensity)"
fi return
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
fi fi
# Fallback: equal distribution among all domains # 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" echo "$equal_share|0|Using equal distribution ($domain_count domains) - safest assumption"
} }