Line 1... |
Line 1... |
1 |
#!/bin/bash
|
1 |
#!/bin/bash
|
2 |
# $Id: alcasar-activity_report.sh 2189 2017-04-28 21:34:19Z tom.houdayer $
|
2 |
# $Id: alcasar-activity_report.sh 2190 2017-04-28 21:38:49Z tom.houdayer $
|
3 |
#
|
3 |
#
|
4 |
# Create an activity report for ALCASAR every week (sunday at 5.35 pm --> see cron.d).
|
4 |
# Create an activity report for ALCASAR every week (sunday at 5.35 pm --> see cron.d).
|
5 |
# We read configuration files and logs to create cool charts.
|
5 |
# We read configuration files and logs to create cool charts.
|
6 |
# Written by Raphaël PION & Rexy
|
6 |
# Written by Raphaël PION, Rexy & Tom HOUDAYER
|
7 |
|
7 |
|
8 |
# files
|
8 |
# files
|
9 |
DIR_TMP="/var/tmp"
|
9 |
DIR_TMP="/var/tmp"
|
10 |
TMP_AV="$DIR_TMP/av_count.txt"
|
10 |
TMP_AV="$DIR_TMP/av_count.txt"
|
11 |
TMP_BL="$DIR_TMP/bl_count.txt"
|
11 |
TMP_BL="$DIR_TMP/bl_count.txt"
|
Line 559... |
Line 559... |
559 |
$SED "/^$tmp_account:/d" $DIR_KEY/key_only_manager
|
559 |
$SED "/^$tmp_account:/d" $DIR_KEY/key_only_manager
|
560 |
$SED "/^$tmp_account:/d" $DIR_KEY/key_manager
|
560 |
$SED "/^$tmp_account:/d" $DIR_KEY/key_manager
|
561 |
$SED "/^$tmp_account:/d" $DIR_KEY/key_all
|
561 |
$SED "/^$tmp_account:/d" $DIR_KEY/key_all
|
562 |
fi
|
562 |
fi
|
563 |
|
563 |
|
- |
|
564 |
|
- |
|
565 |
###################### ALCASAR : LOG ACCESS ######################
|
- |
|
566 |
echo "Get ACC log access of the week"
|
- |
|
567 |
#create html document
|
- |
|
568 |
echo "<h3>Connexion à l'ALCASAR Control Center (ACC)</h3>" >> $HTML_REPORT
|
- |
|
569 |
|
- |
|
570 |
#create new htdigest user to consult statistique of ACC
|
- |
|
571 |
#if user does not exist, we create him
|
- |
|
572 |
if [ $(grep "$tmp_account:" $DIR_KEY/key_only_manager | wc -l) -lt 1 ]
|
- |
|
573 |
then
|
- |
|
574 |
(echo -n "$tmp_account:$realm:" && echo -n "$tmp_account:$realm:$password" | md5sum | awk '{print $1}' ) >> $DIR_KEY/key_only_manager
|
- |
|
575 |
(echo -n "$tmp_account:$realm:" && echo -n "$tmp_account:$realm:$password" | md5sum | awk '{print $1}' ) >> $DIR_KEY/key_manager
|
- |
|
576 |
(echo -n "$tmp_account:$realm:" && echo -n "$tmp_account:$realm:$password" | md5sum | awk '{print $1}' ) >> $DIR_KEY/key_all
|
- |
|
577 |
chown -R root:apache $DIR_KEY
|
- |
|
578 |
chmod 640 $DIR_KEY/key_*
|
- |
|
579 |
fi
|
- |
|
580 |
|
- |
|
581 |
#get admin_log.php from ACC
|
- |
|
582 |
wget -q -nv --user $tmp_account --password $password "https://alcasar/acc/admin_log.php?startTime=$SECS_AGO" -O $TMP_STATS --no-check-certificate
|
- |
|
583 |
|
- |
|
584 |
#clean this file to include it in html report.
|
- |
|
585 |
DELIM_1="<table class=\"table table-striped\">"
|
- |
|
586 |
DELIM_2="<\/table>"
|
- |
|
587 |
cat $TMP_STATS | sed -n "/$DELIM_1/,/$DELIM_2/p" > $TMP_STATS_2
|
- |
|
588 |
cat $TMP_STATS_2 | sed -e 's:images/pixel.gif:../../manager/htdocs/images/pixel.gif:g' >> $HTML_REPORT
|
- |
|
589 |
|
- |
|
590 |
#we delete our user if he still exists
|
- |
|
591 |
if [ $(grep "$tmp_account:" $DIR_KEY/key_only_manager | wc -l) -ge 1 ]
|
- |
|
592 |
then
|
- |
|
593 |
$SED "/^$tmp_account:/d" $DIR_KEY/key_only_manager
|
- |
|
594 |
$SED "/^$tmp_account:/d" $DIR_KEY/key_manager
|
- |
|
595 |
$SED "/^$tmp_account:/d" $DIR_KEY/key_all
|
- |
|
596 |
fi
|
- |
|
597 |
|
- |
|
598 |
|
- |
|
599 |
###################### ALCASAR : GLOBAL TRAFFIC ######################
|
- |
|
600 |
echo "Get Global traffic of the last 30 days"
|
- |
|
601 |
|
- |
|
602 |
ROWS=""
|
- |
|
603 |
EXTIF=$(cat /usr/local/etc/alcasar.conf | grep EXTIF | cut -d'=' -f2)
|
- |
|
604 |
for day in $(vnstat --exportdb -i $EXTIF | grep '^d;' | sort -t";" -k3 -r); do
|
- |
|
605 |
day_datas=(${day//;/ })
|
- |
|
606 |
day_date=${day_datas[2]}
|
- |
|
607 |
day_rxMio=${day_datas[3]}
|
- |
|
608 |
day_txMio=${day_datas[4]}
|
- |
|
609 |
day_rxKio=${day_datas[5]}
|
- |
|
610 |
day_txKio=${day_datas[6]}
|
- |
|
611 |
day_act=${day_datas[7]}
|
- |
|
612 |
|
- |
|
613 |
if [ $day_act -ne 1 ]; then
|
- |
|
614 |
continue
|
- |
|
615 |
fi
|
- |
|
616 |
|
- |
|
617 |
if [ $day_date -lt $SECS_AGO ]; then
|
- |
|
618 |
break
|
- |
|
619 |
fi
|
- |
|
620 |
|
- |
|
621 |
day_dateFormated=$(date -d @$day_date +%x)
|
- |
|
622 |
day_rx=$(($day_rxMio * 1048576 + $day_rxKio * 1024))
|
- |
|
623 |
day_tx=$(($day_txMio * 1048576 + $day_txKio * 1024))
|
- |
|
624 |
day_total=$(($day_rx + $day_tx))
|
- |
|
625 |
day_rxFormated=$(numfmt --from=iec --to=iec --suffix=B $day_rx)
|
- |
|
626 |
day_txFormated=$(numfmt --from=iec --to=iec --suffix=B $day_tx)
|
- |
|
627 |
day_totalFormated=$(numfmt --from=iec --to=iec --suffix=B $day_total)
|
- |
|
628 |
|
- |
|
629 |
ROWS="$ROWS<tr><td>$day_dateFormated</td><td>$day_rxFormated</td><td>$day_txFormated</td><td>$day_totalFormated</td></tr>"
|
- |
|
630 |
done
|
- |
|
631 |
|
- |
|
632 |
# Create html document
|
- |
|
633 |
echo "<h3>Trafic global</h3>" >> $HTML_REPORT
|
- |
|
634 |
echo "<table class=\"table table-striped\">" >> $HTML_REPORT
|
- |
|
635 |
echo "<thead><tr><th>Date</th><th>Entrant</th><th>Sortant</th><th>Total</th></tr></thead><tbody>" >> $HTML_REPORT
|
- |
|
636 |
echo $ROWS >> $HTML_REPORT
|
- |
|
637 |
echo "</tbody></table>" >> $HTML_REPORT
|
- |
|
638 |
|
- |
|
639 |
|
- |
|
640 |
###################### ALCASAR : FAIL2BAN ######################
|
- |
|
641 |
echo "Get fail2ban log of the week"
|
- |
|
642 |
|
- |
|
643 |
# Create html document
|
- |
|
644 |
echo "<h3>Adresse(s) IP bloquée(s) (Fail2Ban)</h3>" >> $HTML_REPORT
|
- |
|
645 |
echo "<table class=\"table table-striped\">" >> $HTML_REPORT
|
- |
|
646 |
echo "<thead><tr><th>Date</th><th>Adresse IP</th><th>Règle</th></tr></thead><tbody>" >> $HTML_REPORT
|
- |
|
647 |
|
- |
|
648 |
DATE_1_WEEK_AGO=$(date --date="$MAX_DAY_AGO days ago" +'%Y-%m-%d %H:%M:%S,%N' | rev | cut -c 7- | rev)
|
- |
|
649 |
# TODO: awk compare date [$1] AND hour [$2] (currently only date is compared)
|
- |
|
650 |
grep " Ban " /var/log/fail2ban.log | sort -r | awk '$1 >= "$DATE_1_WEEK_AGO"' | while read -r log ; do
|
- |
|
651 |
log_datas=($log)
|
- |
|
652 |
log_date="${log_datas[0]} ${log_datas[1]}"
|
- |
|
653 |
log_type=${log_datas[4]:1:-1}
|
- |
|
654 |
log_ip=${log_datas[6]}
|
- |
|
655 |
log_dateFormated=$(date -d "$log_date" +"%x %X")
|
- |
|
656 |
|
- |
|
657 |
echo "<tr><td>$log_dateFormated</td><td>$log_ip</td><td>$log_type</td></tr>" >> $HTML_REPORT
|
- |
|
658 |
done
|
- |
|
659 |
|
- |
|
660 |
echo "</tbody></table>" >> $HTML_REPORT
|
- |
|
661 |
|
- |
|
662 |
|
564 |
######################FIN HTML######################
|
663 |
######################FIN HTML######################
|
565 |
|
664 |
|
566 |
#Execute our javascript function to print charts
|
665 |
#Execute our javascript function to print charts
|
567 |
echo "<script>window.onload = function() {" >> $HTML_REPORT
|
666 |
echo "<script>window.onload = function() {" >> $HTML_REPORT
|
568 |
#BL SINCE INSTALLATION
|
667 |
#BL SINCE INSTALLATION
|