CRITICAL FIX: Eliminate grep bottleneck in threat score calculation

PERFORMANCE BUG: is_excluded_ip() was calling grep for EVERY IP during threat
scoring, causing O(n*m) complexity where n=number of IPs and m=lines in server_ips.txt.
With hundreds of IPs, this resulted in thousands of grep calls (3+ minutes of hangs).

SOLUTION: Pre-load server IPs into associative array in calculate_threat_scores()
function, then use O(1) hash table lookups instead of O(m) grep searches.

Performance improvement: From 180+ seconds hanging to instant completion.
Changed from: grep -qFx "$ip" "$TEMP_DIR/server_ips.txt"
Changed to: [ -n "${server_ips_array[$ip]}" ]
This commit is contained in:
Developer
2026-04-23 22:20:14 -04:00
parent 1c3f12744b
commit baf058d1dc
+11 -5
View File
@@ -1608,11 +1608,9 @@ is_excluded_ip() {
return 0 # True - should be excluded
fi
# Check if it's the server's own IP
if [ -f "$TEMP_DIR/server_ips.txt" ]; then
if grep -qFx "$ip" "$TEMP_DIR/server_ips.txt" 2>/dev/null; then
return 0 # True - should be excluded
fi
# Check if it's the server's own IP (using pre-loaded array for speed)
if [ -n "${server_ips_array[$ip]}" ]; then
return 0 # True - should be excluded
fi
return 1 # False - should not be excluded
@@ -1656,6 +1654,14 @@ analyze_time_series() {
calculate_threat_scores() {
print_info "Calculating threat scores..."
# Pre-load server IPs for fast exclusion checking (avoids grep in loop)
declare -A server_ips_array
if [ -f "$TEMP_DIR/server_ips.txt" ]; then
while read -r ip; do
[ -n "$ip" ] && server_ips_array["$ip"]=1
done < "$TEMP_DIR/server_ips.txt"
fi
# Pre-count requests per IP (MUCH faster than grepping for each IP)
declare -A ip_request_counts
while IFS='|' read -r ip rest; do