-
Notifications
You must be signed in to change notification settings - Fork 0
/
goaccess.sh
212 lines (189 loc) · 7.1 KB
/
goaccess.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/ksh
######################################
# Elias Griffin
# Quadhelion Engineering
# https://www.quadhelion.engineering
# elias@quadhelion.engineering
######################################
######################################
# License Attribution
# ASN Data Provided by MaxMind
# https:/www.maxmind.com
# License CC BY 4.0
######################################
######################################
# License Attribution
# IP to ASN Data provided by DB-IP
# https://db-ip.com/
# License: CC BY 4.0
######################################
#######################################################################
# CUSTOMIZE -----------------------------------------------------------
# ---------------------------------------------------------------------
ASN_URL="https://download.db-ip.com/free/dbip-asn-lite-2023-05.mmdb.gz"
#
# MaxMind GitHub 2021
# "https://git.io/GeoLite2-ASN.mmdb"
#
# MaxMind Direct Updated (Requires Account)
# "https://www.maxmind.com/en/accounts/871098/geoip/downloads"
# CUSTOMIZE -----------------------------------------------------------
#######################################################################
ASN_FILE=$(echo ${ASN_URL} | sed 's:.*/::')
VERSION=$(pkg_info goaccess | head -n 1)
DIR="$( cd "$( dirname "$0" )" && pwd )"
ASN_BASENAME=$(echo ${ASN_FILE%.*})
FULL_REPORT_DATE=$(stat -f "%t%Sm" /var/log/goaccess-total-report.html | awk '{print $1,$2;}')
CURRENT_MONTH_DAY=$(date '+%B %d')
DATE=`date +%b%e`
FILENAME=goaccess-report-${DATE}.html
## Check to see if user running script can write to /var
if id -nG "$USER" | grep -qw "wheel"; then
echo "\\n********************\033[38;5;75m User Mode \033[0;0m************************"
echo "Permissions are correct to write to /var/"
echo "*******************************************************\\n"
else
echo "${USER} \033[38;5;9mdoes not have permission to write to /var/\033[0;0m"
echo "*******************************************************\\n"
fi
## Script Usage
info()
{
echo
echo "********************\033[38;5;75m Base Mode \033[0;0m************************"
echo "${VERSION}"
echo "Available mode: \033[38;5;208mfull\033[0;0m"
echo "Argument full will combine all access.log.*.gz"
echo "*******************************************************\\n"
}
## If ASN DB doesn't exist, ask user to download it
if [ ! -f "/var/db/GeoIP/ASN.mmdb" ] ; then
echo
echo "\033[38;5;208mWould you like to download ASN Data to /var/db/GeoIP/ASN.mmdb\033[0;0m? (y/N)"
read answer
case $answer in
y|Y|"")
echo "*******************************************************"
echo "\033[38;5;75mDownloading ASN Data \033[0;0m"
echo "Destination: /var/db/GeoIP/ASN.mmdb"
echo "*******************************************************"
wget --header='Accept-Encoding: gzip' --tries=3 ${ASN_URL} && gunzip ${ASN_FILE}
echo "****************\033[38;5;75m Unzipped, Moving File \033[0;0m****************"
mv ${DIR}/${ASN_BASENAME} /var/db/GeoIP/ASN.mmdb
echo
echo
echo "*********************\033[38;5;76m Success \033[0;0m*************************\\n"
;;
no|n)
echo
echo "*******************************************************"
echo "\033[38;5;75mSkipping ASN Database \033[0;0m"
echo "*******************************************************"
;;
esac
fi
## Check for ASN Install
if [ -f "/var/db/GeoIP/ASN.mmdb" ] ; then
ASN_INSTALLED=1
else
ASN_INSTALLED=0
fi
## Get script argument "full" or not. If not, display usage
if [ $# -ne 1 ] ; then
info
## Run correct report for just the current /var/www/logs/access.log
case $ASN_INSTALLED in
1)
echo
echo "*********************\033[38;5;75m Results \033[0;0m*************************"
zcat -f /var/www/logs/access.log \
| grep -v 'logfile turned over$' \
| awk '$8=$1$8' \
| goaccess \
--no-global-config \
--ignore-crawlers \
--log-format='%v %h %^ %e [%d:%t %^] "%r" %s %b "%R" "%u" %^' \
--date-format='%d/%b/%Y' \
--time-format='%T' \
--geoip-database=/var/db/GeoIP/ASN.mmdb \
--geoip-database=/var/db/GeoIP/GeoLite2-Country.mmdb \
--output='/var/log/'${FILENAME}
echo "*******************************************************"
;;
0)
echo
echo "*********************\033[38;5;75m Results \033[0;0m*************************"
zcat -f /var/www/logs/access.log \
| grep -v 'logfile turned over$' \
| awk '$8=$1$8' \
| goaccess \
--no-global-config \
--ignore-crawlers \
--log-format='%v %h %^ %e [%d:%t %^] "%r" %s %b "%R" "%u" %^' \
--date-format='%d/%b/%Y' \
--time-format='%T' \
--geoip-database=/var/db/GeoIP/GeoLite2-Country.mmdb \
--output='/var/log/'${FILENAME}
echo "*******************************************************"
;;
esac
if [ -e /var/log/${FILENAME} ]
then
echo
echo "*********************\033[38;5;76m Success \033[0;0m*************************"
echo "Report created at /var/log/${FILENAME}"
echo "*******************************************************\\n"
else
echo "Error creating /var/log/${FILENAME}"
echo "*******************************************************\\n"
fi
## Run report for all web logs, include the gzipped logs with ASN data, omit ASN if skipped
else
case $ASN_INSTALLED in
1)
echo
echo "*********************\033[38;5;75m Results \033[0;0m*************************"
zcat -f /var/www/logs/access.log* \
| grep -v 'logfile turned over$' \
| awk '$8=$1$8' \
| goaccess \
--no-global-config \
--ignore-crawlers \
--log-format='%v %h %^ %e [%d:%t %^] "%r" %s %b "%R" "%u" %^' \
--date-format='%d/%b/%Y' \
--time-format='%T' \
--geoip-database=/var/db/GeoIP/ASN.mmdb \
--geoip-database=/var/db/GeoIP/GeoLite2-Country.mmdb \
--output='/var/log/goaccess-total-report.html'
echo "*******************************************************\\n"
;;
0)
echo
echo "*********************\033[38;5;75m Results \033[0;0m*************************"
zcat -f /var/www/logs/access.log* \
| grep -v 'logfile turned over$' \
| awk '$8=$1$8' \
| goaccess \
--no-global-config \
--ignore-crawlers \
--log-format='%v %h %^ %e [%d:%t %^] "%r" %s %b "%R" "%u" %^' \
--date-format='%d/%b/%Y' \
--time-format='%T' \
--geoip-database=/var/db/GeoIP/GeoLite2-Country.mmdb \
--output='/var/log/goaccess-total-report.html'
echo "*******************************************************\\n"
;;
esac
## Check to see if new full report was created
## Expected maximum of one full report per day, checks Month-Day to see if newer than last full report
if [ -e /var/log/goaccess-total-report.html ] || [ FULL_REPORT_DATE == CURRENT_MONTH_DAY ]
then
echo
echo "*********************\033[38;5;76m Success \033[0;0m*************************"
echo "Report created at /var/log/goaccess-total-report.html"
echo "*******************************************************\\n"
else
echo "Error creating /var/log/goaccess.total-report.html"
echo "*******************************************************\\n"
fi
fi