bashscripts/scan_clam.sh

48 lines
1.5 KiB
Bash
Raw Normal View History

2022-09-29 12:27:20 +02:00
#!/bin/bash
2023-07-25 17:20:02 +02:00
# uncomment when use script from cron
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
2023-07-25 17:20:02 +02:00
# variables
LOGFILE="$(hostname)_clamav-$(date +'%Y-%m-%d').log";
2023-07-20 12:52:12 +02:00
GOTIFY_MESSAGE="Scan init: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r"
2023-07-25 17:20:02 +02:00
LOGS_RELATIVE_PATH="security/"
2023-07-20 12:52:12 +02:00
MALWARE_FOUND=false
2023-07-25 17:20:02 +02:00
readonly TOKEN="<TOKEN_GOTIFY_APP>";
2022-09-29 12:27:20 +02:00
2023-07-25 17:20:02 +02:00
mkdir -p $E7S_CLAM_LOGPATH
2022-09-29 12:27:20 +02:00
2023-07-25 17:20:02 +02:00
for S in ${E7S_CLAM_DIRSTOSCAN}; do
2022-09-29 12:27:20 +02:00
DIRSIZE=$(du -sh "$S" 2>/dev/null | cut -f1);
echo "Starting a daily scan of "$S" directory. Amount of data to be scanned is "$DIRSIZE".";
2023-07-25 17:20:02 +02:00
clamscan -ri "$S" >> "$E7S_CLAM_LOGPATH$LOGFILE";
echo "Scanned folder: "$S >> "$E7S_CLAM_LOGPATH$LOGFILE";
2022-09-29 12:27:20 +02:00
# get the value of "Infected lines"
2023-07-25 17:20:02 +02:00
MALWARE=$(tail "$E7S_CLAM_LOGPATH$LOGFILE"|grep Infected|cut -d" " -f3);
2022-09-29 12:27:20 +02:00
# if the value is not equal to zero, send an email with the log file attached
if [ "$MALWARE" -ne "0" ];then
2023-07-20 12:52:12 +02:00
GOTIFY_MESSAGE="${GOTIFY_MESSAGE} Found **$MALWARE** infected files into **$S** \r"
MALWARE_FOUND=true
2022-09-29 12:27:20 +02:00
else
echo "[clamav] No infected files found."
fi
done
2023-07-20 12:52:12 +02:00
if $MALWARE_FOUND
then
# send log to logs public archive
2023-07-25 17:20:02 +02:00
LOG_URL=$(sh ./utils/logger.sh "$E7S_CLAM_LOGPATH$LOGFILE" $LOGS_RELATIVE_PATH)
2023-07-20 12:52:12 +02:00
GOTIFY_MESSAGE="${GOTIFY_MESSAGE} Scan end: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r"
GOTIFY_MESSAGE="${GOTIFY_MESSAGE} [LogFile]($LOG_URL)"
sh ./utils/gotifypush.sh "ClamAV Scan $(hostname)" "$GOTIFY_MESSAGE" 5 $TOKEN
fi
2022-09-29 12:27:20 +02:00
#clean old logs files
2023-07-25 17:20:02 +02:00
find $E7S_CLAM_LOGPATH -maxdepth 1 -mtime +$E7S_LOGS_PRESERVE_DAYS -exec "rm" -R {} \;
2022-09-29 12:27:20 +02:00
exit 0