說到 Log 分析大家都會先想到用 AWStats 來分析,沒錯這絕對是一個最好的解決方式,但如果你只是要簡單的分析一些資訊,就可以利用一些簡單的 shell 組合來撈出你要的資料

這篇主要是針對 Apache 的 access log 來進行分析,並提供以下範例給大家參考

取得前十名 access 最多的 IP 位址


cat access_log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10

取得前十名 access 最多的網頁

cat access_log|awk '{print $11}'|sort|uniq -c|sort -nr|head -10

取得前十名下載流量最大的 zip 檔案

cat access.log |awk '($7~/\.zip/){print $10 " " $1 " " $4 " " $7}'|sort -nr|head -10

取得前十名 Loading 最大的頁面 (大於60秒的 php 頁面)

cat access_log |awk '($NF > 60 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -10

取得前十名 User access 最久的頁面

cat access_log |awk  '($7~/\.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -10

取得 access log 平均流量 (GB)

cat access_log |awk '{sum+=$10} END {print sum/1024/1024/1024}'

取得所有 404 Link

awk '($9 ~/404/)' access_log | awk '{print $9,$7}' | sort

取得所有 access code 的 stats 數量

cat access_log | awk -F' ' '$9 == "400" || $9 == "404" || $9 == "408" || $9 == "499" || $9 == "500" || $9 =="502" || $9 =="504" {print $9}' | sort | uniq -c | more

以上只是簡單分析出常用的需求,也可以自行斟酌調整,然後再從中找到自己想要的分析模式

相信在日常的維護使用中可以幫上很大的忙。

原文取自 http://shazikai.blogspot.tw/2015/03/apache-access-log.html


arrow
arrow
    文章標籤
    access.log apache http
    全站熱搜

    Frank 發表在 痞客邦 留言(0) 人氣()