From e66270f25f39f764dd0fe31f4f48a0eb3a616002 Mon Sep 17 00:00:00 2001 From: colin Date: Tue, 29 Oct 2024 16:24:12 -0400 Subject: [PATCH] Update docker/clam/docker-entrypoint.sh --- docker/clam/docker-entrypoint.sh | 47 ++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/docker/clam/docker-entrypoint.sh b/docker/clam/docker-entrypoint.sh index f6cc72f..596f8fd 100644 --- a/docker/clam/docker-entrypoint.sh +++ b/docker/clam/docker-entrypoint.sh @@ -2,16 +2,45 @@ MODE=${1:-"scan"} +temp_log_file="/tmp/clamav_scan_$(date +%Y%m%d_%H%M%S).log" + +create_temp_log() { + local log_file="$1" + cat "$log_file" > "$temp_log_file" +} + scan() { echo "Running ClamAV scan..." SCAN_PRIORITY=${SCAN_PRIORITY:-low} + local retry_count=0 + local max_retries=5 if [ "$SCAN_PRIORITY" = "low" ]; then echo "Running scan in low priority mode." - nice -n 19 clamscan -r /scan --log=/var/log/clamav/clamav.log + while ! nice -n 19 clamscan -r /scan --log=/var/log/clamav/clamav.log; do + retry_count=$((retry_count + 1)) + if [ "$retry_count" -ge "$max_retries" ]; then + echo "Max retries reached. Sending failure report to GlitchTip..." + create_temp_log "/var/log/clamav/clamav.log" + go-glitch report --dsn "$GLITCHTIP_DSN" "$temp_log_file" || echo "Failed to report scan failure to GlitchTip" + return + fi + echo "Scan failed. Retrying... ($retry_count/$max_retries)" + sleep 5 + done else echo "Running scan in full power mode." - clamscan -r /scan --log=/var/log/clamav/clamav.log + while ! clamscan -r /scan --log=/var/log/clamav/clamav.log; do + retry_count=$((retry_count + 1)) + if [ "$retry_count" -ge "$max_retries" ]; then + echo "Max retries reached. Sending failure report to GlitchTip..." + create_temp_log "/var/log/clamav/clamav.log" + go-glitch report --dsn "$GLITCHTIP_DSN" "$temp_log_file" || echo "Failed to report scan failure to GlitchTip" + return + fi + echo "Scan failed. Retrying... ($retry_count/$max_retries)" + sleep 5 + done fi } @@ -22,20 +51,26 @@ report() { echo "No log file found." return fi - local total_files=$(grep "Infected files:" "$log_file" | cut -d" " -f3) - local infected_files=$(grep "Infected files:" "$log_file" | cut -d" " -f5) - local errors=$(grep "Total errors:" "$log_file" | cut -d" " -f3) + local total_files=$(grep "Scanned files:" "$log_file" | cut -d":" -f2 | xargs) + local infected_files=$(grep "Infected files:" "$log_file" | cut -d":" -f2 | xargs) + local errors=$(grep "Total errors:" "$log_file" | cut -d":" -f2 | xargs) echo "Scan Report:" echo "Total files scanned: $total_files" echo "Infected files found: $infected_files" echo "Errors during scan: $errors" + + if [ "$infected_files" -gt 0 ]; then + echo "Reporting detections to GlitchTip..." + go-glitch report --dsn "$GLITCHTIP_DSN" --message "ClamAV Scan: $infected_files infected files found" || echo "Failed to report to GlitchTip" + fi } + quarantine() { echo "Quarantining infected files..." local log_file="/var/log/clamav/clamav.log" local quarantine_dir="/quarantine" mkdir -p "$quarantine_dir" - grep "FOUND" "$log_file" | cut -d" " -f1 | while read -r infected_file; do + grep "FOUND" "$log_file" | cut -d":" -f1 | while read -r infected_file; do if [ -f "$infected_file" ]; then mv "$infected_file" "$quarantine_dir/" fi