Nginx日誌分析腳本

運維工作是一個比較複雜的工作,有時候面對上萬條的日誌,如何作分析?難道一條條的分析?
聰明的人會選擇腳本,這就是爲什麼現在提倡自動化運維的原因吧,廢話不多說,直接上腳本。

vim /data/scripts/log_analysis.sh
#!/bin/bash
###############################################
#    Desc        :nginx日誌分析腳本                                    #
#    Author      : Bertram                                                       #
#    Date        : 2019-12-21                                                  #
#    Copyright   : Personal belongs                                      #
###############################################
public(){
    echo ""
    read -p "請輸入要分析的訪問日誌: " log_file
    echo ""   
    if [ ! -f $log_file ];then
        echo "未找到: ${log_file}"
        exit 1
    fi  

    if [ ! -s $log_file ];then
        echo "${log_file}是空文件"
        exit 1
    fi

    #輸出日誌訪問量排名前top_num條數據,可自定義 
    top_num=5
    input_file=`echo $log_file | awk -F '/' '{print $(NF)}'`
    analyze_dir=/home/Bertram/`date +%F`
    top_ip_file=$analyze_dir/ngx_log_top_ip_${input_file}.txt
    top_src_url_file=$analyze_dir/ngx_log_top_src_url_${input_file}.txt
    top_dest_url_file=$analyze_dir/ngx_log_top_dest_url_${input_file}.txt
    top_code_file=$analyze_dir/ngx_log_top_code_${input_file}.txt
    top_terminal_file=$analyze_dir/ngx_log_top_terminal_${input_file}.txt

    mkdir -p $analyze_dir
    start_time=`head -1 $log_file | awk '{print $4}'|cut -d "[" -f2`
    end_time=`tail -1 $log_file | awk '{print $4}'|cut -d "[" -f2`
    total_nums=`wc -l $log_file | awk '{print $1}'`
    size=`du -sh $log_file | awk '{print $1}'`

    #獲取起始與截止時間
    echo "訪問起始時間: $start_time ; 截止時間: $end_time"
    #獲取總行數與大小
    echo  "共訪問 $total_nums 次 ; 日誌大小: $size"
    #獲取最活躍IP
    ##cat $log_file | awk '{print $1}' | sort | uniq -c | sort -rn | head -${top_num} > $top_ip_file
    awk '{ips[$1]++} END{for (i in ips){print ips[i],i}}' $log_file | sort | uniq -c | sort -k1 -nr| head -${top_num} > $top_ip_file
    #獲取訪問來源最多的url
    cat $log_file | awk '{print $13}' | sort | uniq -c | sort -rn | head -${top_num} > $top_src_url_file
    #獲取請求最多的url
    cat $log_file | awk '{print $8}' | sort | uniq -c | sort -rn | head -${top_num} > $top_dest_url_file
    #獲取返回最多的狀態碼
    cat $log_file | awk '{print $11}'| sort | uniq -c | sort -rn | head -${top_num} > $top_code_file
    #獲取返回最多的終端類型
    cat $log_file | awk '{print $14}'| sort | uniq -c | sort -rn | head -${top_num} > $top_terminal_file
    }

    simple(){
    echo "+-+-+-+-+-+- 下面是分析內容 +-+-+-+-+-+-"
    #獲取最活躍IP
    printf "最活躍的前${top_num}個訪問IP: \n"
    cat $top_ip_file
    echo ""
    #獲取訪問來源最多的url
    printf "訪問來源最多的前${top_num}個url: \n"
    cat $top_src_url_file
    echo ""
    #獲取請求最多的url
    printf "請求最多的前${top_num}個url: \n"
    cat $top_dest_url_file
    echo ""
    #獲取返回最多的狀態碼
    printf "返回最多的前${top_num}個狀態碼: \n"
    cat $top_code_file
    echo ""
    printf ""
    #獲取返回最多的終端號
    printf "返回最多的前${top_num}個終端號: \n"
    cat $top_terminal_file
    echo ""
    printf ""   
    printf "返回最多的前${top_num}個IP所屬城市(查詢時間有點慢,耐心等待!): \n"
    echo ''
    printf "%-15s %-15s %-30s\n" "訪問次數" "  IP地址" "      歸屬地"
    echo '-----------------------------------------------'
    a=0
    cat $analyze_dir/ngx_log_top_ip_${input_file}.txt | while read line
    do
    ip=$(echo $line | cut -d '"' -f2)
    count=$(echo $line | cut -d '"' -f1)
        printf "%-10s %-15s %-30s\n" $count $ip $(curl -s "http://freeapi.ipip.net/$(echo $line | cut -d '"' -f2)" | awk -F '\"' {'print $2"--"$4"--"$6'})
    echo '-----------------------------------------------'
    let a=a+1
    done
    echo ""
    printf ""
}

case $1 in
    help)
        echo ""
        echo -e $"Usage: $0 enter a log file \n"               
        ;;
    *)
     public
     simple
        ;;
esac
exit 0

實現功能:
1、分析訪問排名前N的ip地址;
2、分析訪問排名前N的url;
3、分析訪問排名前N的目標url;
4、分析訪問排名前N的終端類型;
5、自動匹配排名前N的ip的歸屬地。
注意:日誌文件和分析腳本放在一個目錄即可;日誌文件輸入絕對路徑。

用法:
Nginx日誌分析腳本

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章