#!/bin/bash # uncomment when use script from cron PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin # variables readonly LOGFILE="$(hostname)_clamav-$(date +'%Y-%m-%d').log" readonly NC_USER="log" readonly NC_PASS="t1todelogs" readonly NC_URL_WEBDAV="https://cloud.studi7.com/remote.php/dav/files" readonly GOTIFY_TOKEN="A4w5ShWUHxcTLbx" readonly GOTIFY_HOST="https://push.studi7.com" readonly GOTIFY_PRIORITY=5 readonly CLAM_LOGPATH="/var/log/clamav/scan/" readonly LOGS_PRESERVE_DAYS=30 # Multiple folders separate by space readonly CLAM_DIRSTOSCAN="/home/roger/Baixades/ /home/roger/Públic/" GOTIFY_MESSAGE="Scan init: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r" MALWARE_FOUND=false mkdir -p $CLAM_LOGPATH for S in ${CLAM_DIRSTOSCAN}; do 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"."; clamscan -ri "$S" >> "$CLAM_LOGPATH$LOGFILE"; echo "Scanned folder: "$S >> "$CLAM_LOGPATH$LOGFILE"; # get the value of "Infected lines" MALWARE=$(tail "$CLAM_LOGPATH$LOGFILE"|grep Infected|cut -d" " -f3); # if the value is not equal to zero, send an email with the log file attached if [ "$MALWARE" -ne "0" ];then GOTIFY_MESSAGE="${GOTIFY_MESSAGE} Found **$MALWARE** infected files into **$S** \r" MALWARE_FOUND=true else echo "[clamav] No infected files found." fi done if $MALWARE_FOUND then # send log to nextcloud folder #LOG_URL=$(sh ./utils/logger.sh "$CLAM_LOGPATH$LOGFILE" $LOGS_RELATIVE_PATH) curl -u $NC_USER:$NC_PASS -T "$CLAM_LOGPATH$LOGFILE" "$NC_URL_WEBDAV/$NC_USER/" GOTIFY_MESSAGE="${GOTIFY_MESSAGE} Scan end: **"`date +"%d/%m/%Y %H:%M:%S"`"** \r" GOTIFY_MESSAGE="${GOTIFY_MESSAGE} [LogFile]($NC_URL_WEBDAV/$NC_USER/$LOGFILE)" #sh ./utils/gotifypush.sh "ClamAV Scan $(hostname)" "$GOTIFY_MESSAGE" 5 $TOKEN # send gotify notification TITLE="ClamAV Scan $(hostname)" EXTRAS="{\"client::display\": {\"contentType\": \"text/markdown\"}, \"client::notification\": {\"click\": { \"url\": \"$NC_URL_WEBDAV/$NC_USER/$LOGFILE\"}}}" curl -X POST "$GOTIFY_HOST/message?token=$GOTIFY_TOKEN" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"message\": \"${GOTIFY_MESSAGE}\", \"priority\": ${GOTIFY_PRIORITY}, \"title\": \"${TITLE}\", \"extras\": ${EXTRAS} }" fi #clean old logs files find $CLAM_LOGPATH -maxdepth 1 -mtime +$LOGS_PRESERVE_DAYS -exec "rm" -R {} \; exit 0