-vi editor
First enter command mode (Esc)
:/str search for str, press n for the next match, press N for the previous match
:1 go to the first line
:$ go to the last line
– web access log statistics (tomcat/apache/nginx)
1. Count UVs based on visitor IPs
awk ‘{print $1}’ access.log.txt|sort | uniq -c |wc -l
2. Count PVs based on accessed URLs
awk ‘{print $7}’ access.log.txt|wc -l
3. Find the most frequently accessed URL
awk ‘{print $7}’ access.log.txt|sort | uniq -c |sort -n -k 1
-r|more
4. Find the most frequently accessing IP
awk ‘{print $1}’ access.log.txt|sort | uniq -c |sort -n -k 1
-r|more
5. View logs by time range
cat access.log.txt| sed -n
‘/14/Mar/2015:21/,/14/Mar/2015:22/p’|more
Is there anything better than the log analysis above? Of course—using goaccess directly is even more powerful:
– web log analysis tool goaccess
Install it first: yum install goaccess
The basic syntax of GoAccess is as follows:
goaccess [ -b ][ -s ][ -e IP_ADDRESS][ -a ] <-f log_file
>
Parameter description:
-f – log filename
-b – enable traffic statistics; this parameter is not recommended if you want to speed up analysis
-s – enable HTTP response code statistics
-a – enable user agent statistics
-e – enable statistics for the specified IP address; disabled by default
Usage examples:
The simplest and most commonly used command:
goaccess -f access.log
If you need to view other information, add the following parameters to display HTTP response codes, user agents, and traffic usage
goaccess -f access.log -s -a -b
If you feel this still does not meet your needs, don’t worry—goaccess supports Linux pipes, so we can preprocess the log files first and then pass them to goaccess for analysis.
zcat access.log.1.gz | goaccess
Let goaccess analyze log files that have already been packaged and compressed.
Or simply analyze all current logs
zcat access.log* | goaccess
If you need to analyze logs for a certain day, for example the logs from October 5th, we can let the linux pipe command show its power ^_^.
sed -n ‘/05/Dec/2010/,$ p’ access.log | goaccess -s -b
Analyze the logs for the one-month period from November 5 to December 5
sed -n ‘/5/Nov/2010/,/5/Dec/2010/ p’ access.log | goaccess
-s -b
If you don’t want to install the goaccess program on the server, you can analyze the server logs by calling your local goaccess program instead (pretty amazing, right? ^_^):
ssh
[email protected]
‘cat /var/log/apache2/access.log’ | goaccess -s -a -b