给自己看的一个apache分析脚本笔记

统计的日志格式:

[08/Nov/2010:21:03:19 +0800] 221.238.248.213 200 75020 12780 "GET /info/newdata/index.php?mod=league&op=view&sid=2264 HTTP/1.0" 28188 "http://www.8788.cn/info/newdata/index.php?mod=league&op=view&sid=2209" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"

脚本:

#!/bin/bash
cd /usr/local/apache2/logs
rm for8788.cn
echo "8788/paipai/tenpay.$(date -d "yesterday" +"%Y-%m-%d")   3个域名汇总情况" >> for8788.cn
#########统计每天php请求页面,404/500/slow 的php请求页面,其中第4个字段是请求状态,第5个字段是所花时间
daytotal=`cat  checklog |awk '{print $1,$8,$4,$5}' |egrep "*.php" |awk '$2~/(.*).php/' | wc -l `
day404=`cat checklog |awk '{if($4~"404") print $0}' |awk '$8~/(.*).php/' |wc -l`
day500=`cat checklog |awk '{if($4~"500") print $0}' |awk '$8~/(.*).php/' |wc -l`
dayslow=`cat checklog |awk '{if($5>1000000) print $0}' |awk '$8~/(.*).php/' |wc -l`
########统计 500/404/slow 页面 占总请求比例,用gwak写,shell好像没有浮点数相除的语句?
dcount500=`gawk -v x=$day500 -v y=$daytotal 'BEGIN{printf "%.3f%%",x * 100/y}'`
dcount404=`gawk -v x=$day404 -v y=$daytotal 'BEGIN{printf "%.3f%%",x * 100/y}'`
dcountslow=`gawk -v x=$dayslow -v y=$daytotal 'BEGIN{printf "%.3f%%",x * 100/y}'`
echo "天总请求数:$daytotal " >> for8788.cn
echo "总404:$day404 占:$dcount404" >> for8788.cn
echo "总500:$day500 占:$dcount500" >> for8788.cn
echo "总慢速:$dayslow 占:$dcountslow" >> for8788.cn
echo " " >> for8788.cn
echo "###########################################################################################" >> for8788.cn
######hourlist的内容是每一行一个小时的字符00 01 02 ..总共24行
cat hourlist|while read hour  #循环读取hourlist的每一行,第一行是 01
do
#####统计每小时的php请求页面

###这里:awk  '$1~/(.*):'$hour':[0-9]+:[0-9]+/'  匹配 [08/Nov/2010:hour:03:19
cat  checklog |awk '{print $1,$8,$4,$5}' |awk '$2~/(.*).php/'|awk  '$1~/(.*):'$hour':[0-9]+:[0-9]+/' >  hour_${hour}
#####统计每小时php fatal 记录
cat /usr/local/php/logs/php_error.log.$(date -d "yesterday" +"%Y-%m-%d") |awk  '$2~/'$hour':[0-9]+:[0-9]+/'|awk '{if($4~"Fatal") print $0}'|head > /usr/local/php/logs/phperr
countphp=`cat /usr/local/php/logs/php_error.log.$(date -d "yesterday" +"%Y-%m-%d") |awk  '$2~/'$hour':[0-9]+:[0-9]+/'|awk '{if($4~"Fatal") print $0}'|wc -l`
#####统计每小时 500/404/slow页面数量,和占比
log500=`cat  hour_${hour} |awk '{if($3~"500")print $2}' |wc -l`
log404=`cat  hour_${hour} |awk '{if($3~"404")print $2}' |wc -l`
logtime=`cat hour_${hour} |awk '{if($4>1000000)print $2}' |wc -l`
hourtotal=`cat hour_${hour}|wc -l`
count500=`gawk -v x=$log500 -v y=$hourtotal 'BEGIN{printf "%.3f%%",x * 100/y}'`
count404=`gawk -v x=$log404 -v y=$hourtotal 'BEGIN{printf "%.3f%%",x * 100/y}'`
countslow=`gawk -v x=$logtime -v y=$hourtotal 'BEGIN{printf "%.3f%%",x * 100/y}'`
echo "$hour:00:00 到 $hour:59:59 总请求数:$hourtotal  (404错误次数:$log404 占:$count404)   (500错误次数:$log500 占:$count500 ) (slow错误次数:$logtime 占:$countslow) " >> for8788.cn
echo " " >> for8788.cn
#####输出 每小时404/500/slow 页面的 前10个
if [ "$log404" -gt 0 ];then
echo "404详细: 出现次数| url" >> for8788.cn
cat  hour_${hour} |awk '{if($3~"404")print $2}' |sort|uniq -c |sort -rn |head >> for8788.cn
echo " " >> for8788.cn
fi
if [ "$log500" -gt 0 ];then
echo "500详细: 出现次数|url" >> for8788.cn
cat  hour_${hour} |awk '{if($3~"500")print $2}' |sort|uniq -c |sort -rn |head >> for8788.cn
echo " " >> for8788.cn
fi
if [ "$logtime" -gt 0 ];then
echo "慢速详细:出现次数|url" >> for8788.cn
cat hour_${hour} |awk '{if($4>2000000)print $2}' |sort|uniq -c |sort -rn |head >> for8788.cn
echo " "  >> for8788.cn
fi
echo "php Fatal 次数:$countphp" >> for8788.cn
if [ "$countphp" -gt 0 ];then
echo "详细:" >> for8788.cn
cat /usr/local/php/logs/phperr |awk '{print $6,$7,$8,$9}' >> for8788.cn
echo " " >> for8788.cn
fi
echo  "######################################################################################" >> for8788.cn
rm hour_${hour}

done
cp -f  for8788.cn /data/backup/
chown backup /data/backup/for8788.cn
 

####所有结果输出到 for8788.cn文件

你可能感兴趣的:(apache,职场,脚本,分析,休闲)