-
Notifications
You must be signed in to change notification settings - Fork 0
/
report-range
executable file
·75 lines (60 loc) · 1.45 KB
/
report-range
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
set -e
# Script version
readonly VERSION=0.0.1
# Script filename
readonly FILENAME=$(basename ${0})
# CLI arguments
#
# Start date range
readonly START_DATE=${1:-$(date --date='-1 month' -I)}
# End date range
readonly END_DATE=${2:-$(date --date='+1 month' -I)}
# Output report
readonly OUTPUT=${3:-"report.html"}
# Starting point
readonly STARTING_POINT=${4:-"/var/log/nginx/access.log*"}
# Environment variables
#
# goaccess configuration
readonly CONF=${CONF:-"/etc/goaccess/goaccess.conf"}
#Target domain and subdomains only
readonly GREP_FILTER=${GREP_FILTER:-"theobori.cafe"}
# Help display function
print_help() {
cat <<USAGE
${FILENAME}
Version ${VERSION}
Arguments
${FILENAME} <start_date> <end_date> <output path> <file(s)/dir>
The following environment variables are overridable:
- GREP_FILTER
- CONF
Usage example:
CONF="custom.conf" ${FILENAME} \\
"$(date --date='-1 month' -I)" \\
"$(date --date='+1 month' -I)" \\
"report.html" \\
"web_server_logs/*.log"
USAGE
}
report() {
local -r files=$(find ${STARTING_POINT} -maxdepth 1 -newermt "${START_DATE}" ! -newermt "${END_DATE}")
if [[ -z ${files} ]]
then
echo "Missing files to process !"
exit 1
fi
zcat -f ${files} \
| grep -P "${GREP_FILTER}" \
| goaccess -o ${OUTPUT} --log-format=COMBINED -p ${CONF} -
}
main() {
if [[ ${#@} -lt 3 ]]
then
print_help
exit 1
fi
report
}
main ${@}