系統運維
運維工作是一個比較復雜的工作,有時候面對上萬條的日志,如何作分析?難道一條條的分析?
聰明的人會選擇腳本,這就是為什么現在提倡自動化運維的原因吧,廢話不多說,直接上腳本。
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 ;;esacexit 0實現功能:
1、分析訪問排名前n的ip地址;
2、分析訪問排名前n的url;
3、分析訪問排名前n的目標url;
4、分析訪問排名前n的終端類型;
5、自動匹配排名前n的ip的歸屬地。
注意:日志文件和分析腳本放在一個目錄即可;日志文件輸入絕對路徑。
用法:
怎樣利用whois查詢域名信息?whois查詢域名信息怎么樣?租用云服務器費剛出生一年就把“一支雪糕”賣到一個億 中街1946怎么做到的?彈性云服務器nginx實現請求轉發ecs云服務器網站修改國內最便宜的云gpu服務器45億美金收優酷土豆,表明阿里流量壓力空前