利用APACHE及SHELL做当日时间端内流量统计

    做流量统计有很多种方式,这里介绍一种利用APACHE日志文件与SHELL脚本来做的,利用NGINX的反向代理功能可以将前后端分离,由NGINX或CDN服务器负责静态文件的访问,而后端通过APACHE只执行动态程序。这样就可以监控及测试服务器处理动态程序的能力。
下面我们来开始设置

1、将APACHE的日志格式设置为LogFormat “%h %a %B %t %H %m”
2、创建SHELL脚本如脚本名称为tongji,代码如下:

<!-- lang: shell -->
#!bin/bash
echo 0 > /logs/sumnum
arr=("January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December")
arr1=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
hours=$*
hours1=${hours:0:2}
hours2=${hours:3:2}
ifhours1=${hours1:0:1}
ifhours2=${hours2:0:1}
if [ $ifhours1 -eq 0 ]
then
bhours1=${hours1:1:1}
else
bhours1=$hours1
fi

if [ $ifhours2 -eq 0 ]
then
bhours2=${hours2:1:1}
else
bhours2=$hours2
fi
sumnum=0
if [ $bhours1 -lt $bhours2 ]
then
for((i=$bhours1;i<=$bhours2;i++));
do
curdate=`date +%d/%B/%Y:"$i"`
echo $curdate > /logs/abc
for j in {0..11}
do
tmp=`grep ${arr[$j]} /logs/abc`
if [ -n $tmp ]
then
if [ ${#tmp} -gt 0 ]
then
format=${curdate/${arr[$j]}/${arr1[$j]}}
format=${format//\//\\/}
num=`awk '$1="127.0.0.1" && $4 ~/'"$format"'/ {++s} END {print s}' /apachelog   /proxycgi_access_log`
if [ ${#num} -gt 0 ]
then
`echo $num >> /logs/sumnum`
fi
fi
fi
done
done
else
echo error
fi
sumnum=`awk '{S[s++]=$1} END {for(a in S) sum += S[a]; print sum}' /logs/sumnum`
echo $sumnum

3、chmod u+x tongji
4、输入命令tongji 12-23,参数12-23表示当日12点至23点的流量

你可能感兴趣的:(shell流量统计,流量统计,apache流量统计)