1. 程式人生 > >GoAccess 分析nginx 日誌

GoAccess 分析nginx 日誌

1、安裝GoAccess需要一些系統支援庫

# yum -y install glib2 glib2-devel GeoIP-devel  ncurses-devel zlib zlib-devel

由於Geo-IP庫沒有得到軟體作者的更新,分析的IP地理位置不準確,所以需要安裝外接ip庫分析ip的城市位置

下載nali-0.2.tar.gz,百度雲盤下載地址:http://pan.baidu.com/s/1eREVhYy
將下載的檔案拷貝到伺服器的目錄:/usr/local

# cd /usr/local
# tar zxvf nali-0.2.tar.gz
# cd nali-0.2
# ./configure
# make && make install

分析IPd地址城市,外掛IP庫:/usr/local/share/QQWry.Dat
需要定期更新ip庫,更新命令:

# nali-update

或者在win7系統安裝:純真IP地址資料庫,更新軟體,將QQWry.Dat檔案複製到服務 器/usr/local/share/目錄下

編譯安裝GoAccess:

# cd /usr/local
# wget http://tar.goaccess.io/goaccess-0.8.1.tar.gz
# tar -xzvf goaccess-0.8.1.tar.gz
# cd goaccess-0.8.1/
# ./configure --enable-geoip --enable-utf8 # make && make install

Centos伺服器可以雲安裝:yum install goaccess

然後就可以開始使用GoAccess了。

首先,進入nginx的日誌目錄,如/var/log/nginx,輸入如下命令讓goaccess開啟指定檔案:

1
goaccess -f access.log
開啟之後會跳出選擇日誌檔案型別的介面:
這裡寫圖片描述
Nginx日誌是屬於Combined Log Format (XLF/ELF)型別的,所以我們選擇第三個。用上下游標移動,空格選中,回車確定。

進入了主介面:
這裡寫圖片描述
鍵盤操作:

F1或h:幫助
F5 :重新整理主介面
q:退出程式/當前視窗/摺疊當前模組
o或Enter:展開選中的模組或視窗
0-9以及Shift + 0:將選中的模組或視窗啟用
k和j:模組內部移動
c:修改配色
^f和^b:模組中上下滾屏
tab shift+tab:前後切換模組
s:模組內部排序選擇
/:在所有模組中搜索(支援正則)
n:找到下個匹配
g和G:跳到第一項/

如果報錯,則是nginx日誌格式和Goaccess配置檔案不同,修改配置檔案

#vim /etc/goaccess.conf
#time-format %f

time-format %T
#date-format %d/%b/%Y
log-format %h %^[%d:%^] "%r" %s %b "%R" "%u" %^

# Squid native log format
#
#time-format %s

更改為如上所示即可

使用GoAccess生成html報告
進入日誌目錄:

# cd /var/log/nginx/

如果日誌未壓縮:

#goaccess -f access.log.20161103 -a > index.html

如果日誌壓縮:

# zcat access.log.20161103.gz | goaccess -a >index.html

此時,就可以搭建php環境,將生成的html在網頁上呈現:

Centos 7防火牆firewalld開放80埠:

# firewall-cmd --zone=public --add-port=80/tcp --permanent

平滑重啟:

# firewall-cmd --reload

安裝Apache

# yum -y install httpd

開啟apache服務

# systemctl start httpd.service

設定apache服務開機啟動

# systemctl enable httpd.service

驗證apache服務是否安裝成功
在瀏覽器輸入:

安裝PHP

# yum -y install php

重啟apache服務

# systemctl restart httpd

或者

# systemctl restart httpd.service

在apache2預設目錄 /var/www/html
寫個php檔案

# vim /var/www/html/info.php
<?php phpinfo(); ?>

退出儲存

再在瀏覽器中輸入:

mysql安裝詳見另篇部落格

將PHP和MySQL關聯起來

# yum -y install php-mysql

安裝常用的PHP模組

# yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel

重啟apache服務

# systemctl restart httpd.service

【 LAMP一鍵安裝:
不想折騰的,可以一鍵安裝LAMP環境,詳見另篇部落格:Centos7安裝zabbix監控

最後,可將Goaccess轉化log日誌生成的html檔案放在目錄/var/www/html中通過瀏覽器訪問
博主做了日誌模板:

登入頁
這裡寫圖片描述

nginx日誌
這裡寫圖片描述

IP城市分析
這裡寫圖片描述

apache2日誌
這裡寫圖片描述

還有zabbix監控 、app介面訪問請求分析等等。如果需要模板,部落格留言郵箱,將會發附件給你們。

監控apache2日誌:
則需要更改格式為 下面所示:

# vim /etc/goaccess.conf
# Google Cloud Storage or
# The time in microseconds since the Unix epoch.
#
#time-format %f
time-format %T
date-format %d/%b/%Y
log-format %v:%p %h %^[%d:%^] "%r" %s %b "%R" "%u" %^

# Squid native log format
#
#time-format %s