GoAccess 是一款开源的网站日志实时分析工具。GoAccess 的工作方式很容易理解,就是读取和解析 Apache/Nginx/Lighttpd 的访问日志文件 access log,然后以更友好的方式把统计信息显示出来。统计的内容包括:访问概况、动态页面请求、静态页面请求(如图片、样式表、脚本等)、访客排名,访客使用的操作系统,访客使用的浏览器,来路域名,404 错误,搜索爬虫,搜索关键词等等。
安装组件
yum -y install glib2 glib2-devel ncurses ncurses-devel GeoIP GeoIP-devel
下载源码包安装
wget http://tar.goaccess.io/goaccess-0.8.5.tar.gz
tar zxvf goaccess-0.8.5.tar.gz -C /usr/src/
cd /usr/src/goaccess-0.8.5/
./configure --prefix=/usr/local/goaccess
make && make install
下载文件
cd /usr/local/goaccess/
wget -O goaccessrc https://raw.githubusercontent.com/allinurl/goaccess/master/config/goaccess.conf --no-check-certificate
编辑文件
vim goaccessrc
去除下面22,33行的注释,增加235行的注释
22 date-format %d/%b/%Y
33 log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
235 #ignore-panel KEYPHRASES
创建网页目录
mkdir html
生成网页报告
/usr/local/goaccess/bin/goaccess -f /usr/local/nginx/logs/access.log -d -a -p /usr/local/goaccess/goaccessrc > /usr/local/goaccess/html/access.html
Nginx发布goaccess网页
cd /usr/local/nginx/conf/vhosts/
vi goaccess.conf
server {
listen 80;
server_name goaccess.xx.com;
autoindex on;
location / {
root /usr/local/goaccess/html;
}
}
重新加载
/usr/local/nginx/sbin/nginx -s reload
访问网页
http://goaccess.xx.com/access.html
下面介绍nginx日志切割和自动生成网页报告以及汉化。
vi nginx_cut.sh
#!/bin/bash
#############日志切割###########################
time=`date -d today +%Y%m%d%H%M`
/usr/local/nginx/sbin/nginx -s stop
cd /usr/local/nginx/logs
mv access.log access_$time.log
mv error.log error_$time.log
/usr/local/nginx/sbin/nginx
#删除超过7天的日志文件
find /usr/local/nginx/logs -ctime 3 -type f -name "*.log" -exec rm {} \;
#自动生成网页报告
/usr/local/goaccess/bin/goaccess -f access_$time.log -d -a -p /usr/local/goaccess/goaccessrc > /usr/local/goaccess/html/access_$time.html
#自动汉化网页报告文件
echo access_$time.html > /tmp/time.log
/manage/hanhua.sh
#删除超过7天的报告文件
find /usr/local/goaccess/html -ctime 7 -type f -name "*.html" -exec rm {} \;
编辑汉化脚本,英文翻译参考百度翻译,可能不太准确。
vi /manage/hanhua.sh
#/bin/bash
file=`cat /tmp/time.log`
cd /usr/local/goaccess/html
sed -i ‘s/Overall/整体/g‘ $file
sed -i ‘s/Unique visitors/独立访客/g‘ $file
sed -i ‘s/Requested files/请求的文件/g‘ $file
sed -i ‘s/Requested static files/请求的静态文件/g‘ $file
sed -i ‘s/Not found URLs/没有找到网址/g‘ $file
sed -i ‘s/Hosts/主机/g‘ $file
sed -i ‘s/Operating Systems/操作系统/g‘ $file
sed -i ‘s/Browsers/浏览器/g‘ $file
sed -i ‘s/Referrers URLs/反向链接的url/g‘ $file
sed -i ‘s/Referring sites/参考网站/g‘ $file
sed -i ‘s/Keyphrases/关键词/g‘ $file
sed -i ‘s/Status codes/状态码/g‘ $file
sed -i ‘s/Generated by/生成的/g‘ $file
sed -i ‘s/Dashboard/仪表板/g‘ $file
sed -i ‘s/Analyzed Requests/分析请求/g‘ $file
sed -i ‘s/Total Requests/总请求/g‘ $file
sed -i ‘s/Failed Requests/请求失败/g‘ $file
sed -i ‘s/Generation Time/生成时间/g‘ $file
sed -i ‘s/Unique Visitors/独立访客/g‘ $file
sed -i ‘s/Unique Files/独特的文件/g‘ $file
sed -i ‘s/Excl. IP Hits/除外的IP点击/g‘ $file
sed -i ‘s/Referrers/反向链接/g‘ $file
sed -i ‘s/Unique 404/独特的404/g‘ $file
sed -i ‘s/Static Files/静态文件/g‘ $file
sed -i ‘s/Log Size/日志大小/g‘ $file
sed -i ‘s/Bandwidth/带宽/g‘ $file
sed -i ‘s/per day/每一天/g‘ $file
sed -i ‘s/Including spiders/包括蜘蛛/g‘ $file
sed -i ‘s/Hits having the same IP/具有相同的IP点击/g‘ $file
sed -i ‘s/date and agent are a unique visit/日期和代理是一种独特的访问/g‘ $file
sed -i ‘s/Top Requested Files sorted by hits/请求的文件按点击顶部/g‘ $file
sed -i ‘s/time served/时间送达/g‘ $file
sed -i ‘s/protocol/协议/g‘ $file
sed -i ‘s/method/方法/g‘ $file
sed -i ‘s/Hits/点击/g‘ $file
sed -i ‘s/Protocol/协议/g‘ $file
sed -i ‘s/Method/方法/g‘ $file
sed -i ‘s/Top 404 Not Found URLs sorted by hits/前404没有找到网址按点击率排序/g‘ $file
sed -i ‘s/Top/前/g‘ $file
sed -i ‘s/sorted by hits/按点击率/g‘ $file
sed -i ‘s/bandwidth/带宽/g‘ $file
sed -i ‘s/Hostname/主机名/g‘ $file
sed -i ‘s/sorted by unique visitors/通过独特的访客排序/g‘ $file
sed -i ‘s/Requested/要求/g‘ $file
sed -i ‘s/Status Codes/状态码/g‘ $file
sed -i ‘s/Code/代码/g‘ $file
sed -i ‘s/Visitors/游客/g‘ $file
sed -i ‘s/Name/名称/g‘ $file
sed -i ‘s/Not Found URLs/没有找到网址/g‘ $file
sed -i ‘s/or service not known/或服务不知道/g‘ $file
sed -i ‘s/Pages/页面/g‘ $file
sed -i ‘s/Referring Sites/参考网站/g‘ $file
访问网页
http://goaccess.xx.com/
点击网页文件
汉化之后的页面效果如下: