針對app,resin,tomcat日誌分割腳本
nginx
appname:填寫app(包含resin/tomcat)的名字;shell
key:過濾日誌關鍵字,避免刪除其餘沒必要要的文件;
tomcat
cleanday:日誌保存的週期,缺省保存30天;bash
cleanlog:刪除日誌的記錄保存的目錄app
核心命令: find命令去查找日誌目錄下含關鍵字的日誌文件,而後利用for循環去刪除$cleanday以前的日誌文件dom
#!/bin/bash today=$(date +%Y_%m%d_%H%M) #appname=tomcat #logdir=/data/log/tomcat #key=log #key=catalina.out appname=storm logdir=/data/log/$appname key=$appname cleanday=30 cleanlog=/data/log/clean filelist=$(find $logdir -type f -mtime +$cleanday |grep "$key" ) [[ -d $cleanlog ]] || mkdir -p $cleanlog echo "[Date:`date`]" if [[ -z $filelist ]];then echo "$appname logfile have't the $cleanday days ago file! ---exit!" echo "[ Date:`date` ] $appname logfile have't the $cleanday days ago file! ---exit!" >> $cleanlog/delete.log exit fi echo "Starting clean up the $appname is logfile for $cleanday days ago..." echo "Need to clean up the following directory:" echo "$filelist" echo "[ Date:`date` ]" >> $cleanlog/delete.log echo "Starting clean up the $appname is logfile..." >> $cleanlog/delete.log echo "Need to clean up the following directory:" >> $cleanlog/delete.log echo "$filelist" >> $cleanlog/delete.log for i in $filelist do rm -f $i #echo $i > /dev/null 2>&1 done filelist2=$(find $logdir -type f -mtime +$cleanday |grep "$key") if [[ -z $filelist2 ]];then echo "$appname logfile have cleanup ---successful!" echo "$appname logfile have cleanup ---successful!" >> $cleanlog/delete.log else echo "$appname logfile have cleanup ---faild!" echo "$appname logfile faild file:" echo "$filelist2" echo "$appname logfile have cleanup ---faild!" >> $cleanlog/delete.log echo "$appname logfile faild file:" >> $cleanlog/delete.log echo "$filelist2" >> $cleanlog/delete.log fi
針對nginx日誌分割腳本:ide
#!/bin/bash path=/data/log/nginx nginx=` cat /usr/local/nginx/logs/nginx.pid ` mv $path/access.log $path/access_`date +%Y%m%d`.log kill -USR1 $nginx #使用USR1參數通知Nginx進程切換日誌文
針對nginx日誌分析腳本:搜索引擎
#!/bin/bash #Date create 2013-10-23 #Author GaoMingHuang log_path=/data/log/nginx/access.log log_dir=/data/log/Analysis domain="crm.baoxian.in" email="530035210@qq.com" maketime=`date +%Y-%m-%d" "%H":"%M` logdate=`date -d "yesterday" +%Y-%m-%d` dayone=`date +%d/%b/%Y` now=`date +%Y_%m%d_%H%M` date_start=$(date +%s) total_visit=`wc -l ${log_path} | awk '{print $1}'` total_bandwidth=`awk -v total=0 '{total+=$10}END{print total/1024/1024}' ${log_path}` total_unique=`awk '{ip[$1]++}END{print asort(ip)}' ${log_path}` ip_pv=`awk '{ip[$1]++}END{for (k in ip){print ip[k],k}}' ${log_path} | sort -rn |head -20` url_num=`awk '{url[$7]++}END{for (k in url){print url[k],k}}' ${log_path} | sort -rn | head -20` #referer=`awk -v domain=$domain '$11 !~ /http:\/\/[^/]*'"$domain"'/{url[$11]++}END{for (k in url){print url[k],k}}' ${log_path} | sort -rn ` notfound=`awk '$9 == 404 {url[$7]++}END{for (k in url){print url[k],k}}' ${log_path} | sort -rn | head -20` #spider=`awk -F'"' '$6 ~ /Baiduspider/ {spider["baiduspider"]++} $6 ~ /Googlebot/ {spider["googlebot"]++}END{for (k in spider){print k,spider[k]}}' ${log_path}` #search=`awk -F'"' '$4 ~ /http:\/\/www\.baidu\.com/ {search["baidu_search"]++} $4 ~ /http:\/\/www\.google\.com/ {search["google_search"]++}END{for (k in search){print k,search[k]}}' ${log_path}` #echo -e "概況\n報告生成時間:${maketime}\n總訪問量:${total_visit}\n總帶寬:${total_bandwidth}M\n獨立訪客:${total_unique}\n\n訪問IP統計\n${ip_pv}\n\n訪問url(統計前20個頁面)\n${url_num}\n\n來源頁面統計\n${referer}\n\n404統計(統計前20個頁面)\n${notfound}\n\n蜘蛛統計\n${spider}\n\n搜索引擎來源統計\n${search}" #統計該ip在幹些什麼 max_ip=`awk '{ip[$1]++}END{for (k in ip){print ip[k],k}}' ${log_path} | sort -rn |head -1 |awk '{print $2}'` ip_havi=`cat $log_path | grep "$max_ip" | awk '{print $7}'| sort |uniq -c |sort -nr |head -20` #統計當天哪一個時間段訪問量最多 time_stats=`awk '{print $4}' ${log_path} | grep "$dayone" |cut -c 14-18 |sort|uniq -c|sort -nr |head -n 10` echo -e "概況\n報告生成時間:${maketime}\n總訪問量:${total_visit}\n總帶寬:${total_bandwidth}M\n獨立訪客:${total_unique}\n\n訪問IP統計(統計前20個IP):\n${ip_pv}\n\n訪問url最多(統計前20個頁面)\n:${url_num}\n\n404統計(統計前20個頁面):\n${notfound}\n\n當天訪問次數最多的時間段以下:\n${time_stats}\n\n訪問量最高的IP[${max_ip}]前20個最多的頁面以下:\n${ip_havi} " [[ -d $log_dir ]] || mkdir -p $log_dir echo -e "概況\n報告生成時間:${maketime}\n總訪問量:${total_visit}\n總帶寬:${total_bandwidth}M\n獨立訪客:${total_unique}\n\n訪問IP統計(統計前20個IP):\n${ip_pv}\n\n訪問url最多(統計前20個頁面) \n:${url_num}\n\n404統計(統計前20個頁面):\n${notfound}\n\n當天訪問次數最多的時間段以下:\n${time_stats}\n\n訪問量最高的IP[${max_ip}]前20個最多的頁面以下:\n${ip_havi} " > $log_dir/analysis_access$now.log date_end=$(date +%s) time_take=$(($date_end-$date_start)) take_time=$(($time_take/60)) echo "access統計腳本分析日誌花費了: [start:$date_start end:$date_end] $time_take"s" $take_time"min"" echo "access統計腳本分析日誌花費了: [start:$date_start end:$date_end] $time_take"s" $take_time"min"" >> $log_dir/analysis_access$now.log
針對nginx日誌分析腳本結果展示以下:google