Last active 6 months ago

run_goaccess Raw
1#!/bin/bash
2
3LOG_DIR="/var/log/apache2/vhosts/access_logs/"
4VHOST_BASE="/var/www/"
5STATS_OUT_DIR="/stats/"
6BOOTSTRAP=0
7
8for DIR in $(find /var/www/ -maxdepth 2 -name 'public_html' -type d); do
9 VHOST=$(basename $(echo $DIR | sed "s/\/public_html.*//"))
10
11 if [ "$BOOTSTRAP" == "1" ]; then
12 [ ! -d $VHOST_BASE/$VHOST/goaccess-db ] && mkdir -p $VHOST_BASE/$VHOST/goaccess-db
13 [ ! -d $VHOST_BASE/$VHOST/stats ] && mkdir -p $VHOST_BASE/$VHOST/stats
14 [ $(grep -c "Alias /stats" /etc/apache2/sites-available/"$VHOST"_settings) -le 0 ] && sed -i "/Alias \/phpmyadmin.*/a \\\tAlias /stats \"\/var\/www\/$VHOST\/stats\/\"" /etc/apache2/sites-available/"$VHOST"_settings
15 if [ ! -f /var/www/"$VHOST"/stats/.htaccess ]; then
16 USER="$(stat -c "%U" "$VHOST_BASE/$VHOST/public_html" )"
17 printf "AuthName \"restricted stuff\"\nAuthType Basic\nAuthUserFile /etc/apache2/users\n\nrequire user focus $USER" > $VHOST_BASE/$VHOST/stats/.htaccess
18 fi
19 for FILE in $(find $LOG_DIR -name $VHOST-access.log.*.gz); do
20 zcat $FILE | goaccess \
21 --agent-list \
22 --anonymize-ip \
23 --persist \
24 --restore \
25 --config-file /etc/goaccess/goaccess.conf \
26 --db-path "$VHOST_BASE/$VHOST/goaccess-db" \
27 --log-format COMBINED \
28 --output "$VHOST_BASE/$VHOST/$STATS_OUT_DIR/index.html" \
29 -
30 done
31 fi
32
33 LOGFILES=()
34 for ext in log log.1; do
35 logfile="$LOG_DIR/$VHOST-access.$ext"
36 [ -e "$logfile" ] && LOGFILES+=("$logfile")
37 done
38
39 if [ ${#LOGFILES[@]} -eq 0 ]; then
40 echo "No log files in '$LOGDIR'"
41 else
42 goaccess \
43 --agent-list \
44 --anonymize-ip \
45 --persist \
46 --restore \
47 --config-file /etc/goaccess/goaccess.conf \
48 --db-path "$VHOST_BASE/$VHOST/goaccess-db" \
49 --log-format "COMBINED" \
50 --output "$VHOST_BASE/$VHOST/$STATS_OUT_DIR/index.html" \
51 "${LOGFILES[@]}"
52 fi
53done
54