使用GoAccess分析Nginx日志以及sed/awk手動分析實踐
前言
使用Nginx的網站可能會遇到訪問流量異常、被友情檢測、程序出現Bug等各種突然情況,這時大家的反應想必都是第一時間分析日志,然后發現日志有幾十GB之多,又需要按照時間、錯誤類型或者關鍵字段檢索信息時會不會有種醍醐灌頂、菊花一緊的錯覺。文中介紹的方法不管是GoAccess還是sed/awk雖然可以解決一時的問題但未必能夠治本,也許ELK(Logstash+ElasticSearch+Kibana)對我們大多數人來說是更合理的集中化日志管理解決方案。
日志固然重要,但努力建設適合業務發展的集中化日志管理平臺才是基礎核心
</div>更新歷史
2015年07月16日 - 初稿
閱讀原文 - http://wsgzao.github.io/post/goaccess/
擴展閱讀
GoAccess - http://goaccess.io/
用GoAccess分析Nginx的日志 - http://www.fancycoding.com/log-analyse-using-goaccess/
sed 簡明教程 - http://coolshell.cn/articles/9104.html
AWK 簡明教程 - http://coolshell.cn/articles/9070.html
</div>安裝GoAccess
各平臺都有灰常簡單的部署方案 - http://goaccess.io/download
</div>wget http://tar.goaccess.io/goaccess-0.9.2.tar.gz tar -xzvf goaccess-0.9.2.tar.gz cd goaccess-0.9.2/ ./configure --enable-utf8 make make install
使用方式
更多常見問題請參考官方FAQ - http://goaccess.io/faq
</div>#直接打開 goaccess -f access.log選擇日志格式
NCSA Combined Log Format
剩下的操作都蠻簡單的,參考擴展閱讀和官方文檔吧
導出HTML報告會遇到的問題
goaccess -f time_access.log -a > report.html
GoAccess - version 0.9.2 - Jul 15 2015 16:23:20 Config file: /usr/local/etc/goaccess.conf
Fatal error has occurred Error occured at: src/parser.c - verify_formats - 1691 No time format was found on your conf file.
添加配置文件
vi ~/.goaccessrc
time-format %T date-format %d/%b/%Y log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
重新指定配置文件后執行
goaccess -f time_access.log -p ~/.goaccessrc -a > report.html</pre>
使用bash/sed/awk手動查找Nginx日志
更多技巧可以參考擴展閱讀,Python的處理效率或者更優
#按日期查找時間段 sed -n "/14\/Jul\/2015:00:00:00/,/15\/Jul\/2015:15:00:00/"p access.log > time_access.log查找504錯誤的頁面和數量
awk '($9 ~ /504/)' time_access.log | awk '{print $7}' | sort | uniq -c | sort -rn > 504.log
查找訪問最多的20個IP及訪問次數
awk '{print $1}' time_access.log | sort | uniq -c | sort -n -k 1 -r | head -n 20 > top.log</pre>
</div> 原文 http://wsgzao.github.io/post/goaccess/
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!