聰明的人會選擇腳本,這就是為什么現在提倡自動化運維的原因吧,廢話不多說,直接上腳本。

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的歸屬地。
注意:日志文件和分析腳本放在一個目錄即可;日志文件輸入絕對路徑。

用法:

更多關于云服務器域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn

贊(0)
聲明:本網站發布的內容(圖片、視頻和文字)以原創、轉載和分享網絡內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。郵箱:3140448839@qq.com。本站原創內容未經允許不得轉載,或轉載時需注明出處:三五互聯知識庫 » Nginx日志分析腳本

登錄

找回密碼

注冊

主站蜘蛛池模板: 国产日韩精品中文字幕| 国产精品久久久久久亚洲色| 国产日韩精品中文字幕| 中文字幕在线精品国产| 欧美精品V欧洲精品| 少妇激情一区二区三区视频小说| 成人一区二区三区久久精品| 久久精品国内一区二区三区| 东京热一精品无码av| 国产综合久久99久久| 久热久精久品这里在线观看| 亚洲中文字幕av天堂| 久久亚洲国产精品五月天| 国产乱子伦农村xxxx| 日本高清视频网站www| 人妻精品动漫h无码| 国产精品无码专区| 亚洲国产成人av国产自| 都市激情 在线 亚洲 国产| 大尺度国产一区二区视频| 国产又爽又黄的精品视频| 国产精品免费看久久久| 免费无码久久成人网站入口| av午夜福利一片免费看久久| 福利视频在线播放| 18禁亚洲一区二区三区| 色婷婷五月综合久久| 爽爽精品dvd蜜桃成熟时电影院| 国产成人免费ā片在线观看| 熟女人妻aⅴ一区二区三区电影 | 国产熟女真实乱精品51| 久久天天躁狠狠躁夜夜躁2012| 开心激情站一区二区三区| 午夜射精日本三级| 深夜视频国产在线观看| 久久精品久久黄色片看看| 国产精品女人毛片在线看| 炉霍县| 四虎在线成人免费观看| 亚洲高清WWW色好看美女| 天峻县|