-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-reports.sh
executable file
·47 lines (36 loc) · 1.83 KB
/
gen-reports.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# Script downloads logs, processes them, and uploads the report
# Cron example, cmd argument (10 or 30) is the number of logs to process
# Number of days if the webhost rotates the log each day as it does in my case
# Runs every 10 minutes
# */10 * * * * /bin/bash /opt/goaccess/gen-reports.sh 10 > /opt/goaccess/gen-reports-10.log 2>&1
# Runs 5 minuts before the date rolls over, account for UTC offset if needed since the config converts to UTC
# 55 23 * * * /bin/bash /opt/goaccess/gen-reports.sh 30 > /opt/goaccess/gen-reports-30.log 2>&1
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <number_of_logs>"
exit 1
fi
# Logs to process
log_day_count="$1"
# Log directory
logs_dir="/opt/goaccess/logs"
# Report directory
reports_dir="/opt/goaccess/reports"
# Copy down logs
# Exclude compressed logs (same name ends with .gz), include the access logs, exclude everything else not matching.
/usr/bin/rsync -avzP --exclude='*.gz' --include='access.log.*.*' --exclude='*' -e "ssh -i /path/to/ssh-key" user@web-host:logs/ $logs_dir
# Clear combined log file
# https://unix.stackexchange.com/a/485652
: > "$logs_dir/$log_day_count.access.log"
# Combine last x logs into one file
# grep -v is used to filter out matching lines
recent_logs=($(/usr/bin/ls -1t $logs_dir/access.log.*.* | /usr/bin/head -n "$log_day_count"))
for log_file in "${recent_logs[@]}"
do
/usr/bin/cat "$log_file" | /usr/bin/grep -v '/reports/' >> "$logs_dir/$log_day_count.access.log"
done
# Run goaccess on the combined log file
/usr/bin/goaccess "$logs_dir/$log_day_count.access.log" --config-file=/opt/goaccess/goaccess.conf --html-report-title="Visitor Statistics $log_day_count Days (UTC)" -o $reports_dir/report$log_day_count.html
# Upload reports
/usr/bin/rsync -avzP -e "ssh -i /path/to/ssh-key" $reports_dir/ user@web-host:reports/
echo "===== DONE ====="