46 lines
1.6 KiB
Bash
Raw Normal View History

2023-01-22 20:51:28 +01:00
#!/bin/bash
DIRECTORY=/home/valentin/mail
TOKEN=af920d2f7dbe97
DATE=$(date +%Y%m%d-%H%M%S)
HOST=($(cat /etc/sentinel/virtualhost))
WEEK=$(date +%V)
DAY=$(date +%u)
if [ ${DAY} -eq 1 ]; then
if [ ${WEEK} -ne "01" ]; then
WEEK=$(echo "$WEEK-1" |bc)
else
WEEK="53"
fi
fi
for i in ${HOST[@]}
do
directory_host=$DIRECTORY/$i/$WEEK
log_access=/var/log/nginx/${i}_access.log.1
mkdir -pv $directory_host
cat $log_access | grep "|" | awk -F "|" '{print $1}' | sort | uniq > $directory_host/list_$DATE
while read line; do
if grep $line $DIRECTORY/*/*/output_*.txt > /dev/null 2>&1; then
grep -h -B1 -A8 $line $DIRECTORY/*/*/output_*.txt |head -10 >> $directory_host/output_$DATE.txt
else
curl "ipinfo.io/$line?token=$TOKEN" >> $directory_host/output_$DATE.txt
fi
echo >> $directory_host/output_$DATE.txt
done <$directory_host/list_$DATE
echo "nombre de visite : $(wc -l $directory_host/list_$DATE |cut -d ' ' -f1)" > /tmp/mail
echo "nombre de visite par pays, par region et par ville : " >> /tmp/mail
LIST=("country" "region" "city")
for j in ${LIST[@]}
do
echo "----${j}------" >> /tmp/mail
cat $directory_host/output_$DATE.txt |grep "${j}" |sort |uniq -c >> /tmp/mail
echo "--------------" >> /tmp/mail
done
echo "nombre erreur 400 par IP : " >> /tmp/mail
cat $log_access | awk -F "|" '{ if($2 == "400") print $1}' |sort |uniq -c >> /tmp/mail
echo "--------" >> /tmp/mail
echo "nombre erreur 404 par IP : " >> /tmp/mail
cat $log_access | awk -F "|" '{ if($2 == "404") print $1}' |sort |uniq -c >> /tmp/mail
cat /tmp/mail |mail -s "Rapport reverse proxy $DATE" -A $directory_host/output_$DATE.txt valczebackup@gmail.com
#rm $directory_host/*
done