需求:在程序中有两个页面,用于某种用途,现需要统计这两个页面在每分钟内的访问量,包括总访问次数,成功次数(状态码200),失败次数。然后写入到mysql中。
mysql字段:id(自增长)、time(实际统计时间)、year、month、day、hour、visit(总访问量)、success、fail、target(目标)
#!/bin/bash
#Desc : analysis apache log for witch pages with one minutes
#install bc packages with yum install -y bc if not found bc command
logdir=/var/log/httpd/80/access
logfile=$logdir/$(date +%Y%m%d)_access_log
month=$(date|awk '{print$2}')
day=$(date|awk '{print$3}')
year=$(date|awk '{print$6}')
hour=$(date|awk '{print$4}'|cut -d: -f1)
min=$(date|awk '{print$4}'|cut -d: -f2)
# get current system times to /tmp
echo $month > /tmp/month.txt
echo $day > /tmp/day.txt
echo $year > /tmp/year.txt
echo $hour > /tmp/hour.txt
echo $min > /tmp/min.txt
# compute run minute
if [ "$min" == "00" ]; then
RUN_MINUTE=59
else
if [ "$(awk -F '' '{print$(NF-1)}' /tmp/min.txt)" == "0" ]; then
echo $(awk -F '' '{print$NF}' /tmp/min.txt) > /tmp/min2.txt
echo $(cat /tmp/min2.txt) -1|bc > /tmp/run_minute.txt
if [ "cat /tmp/run_minute.txt" == "0" ]; then
RUN_MINUTE=00
else
RUN_MINUTE=0$(cat /tmp/run_minute.txt)
fi
else
echo $(cat /tmp/min.txt) -1|bc > /tmp/run_minute.txt
if [ "$(cat /tmp/run_minute.txt)" == "9" ]; then
RUN_MINUTE=09
else
RUN_MINUTE=$(cat /tmp/run_minute.txt)
fi
fi
fi
# compute run hour
if [ "$hour" == "00" ]; then
if [ "$min" == "00" ]; then
RUN_HOUR=23
else
RUN_HOUR=$hour
fi
else
if [ "$min" == "00" ]; then
if [ "$(awk -F '' '{print$(NF-1)}' /tmp/hour.txt)" == "0" ]; then
echo $(awk -F '' '{print$NF}' /tmp/hour.txt) > /tmp/hour2.txt
echo $(cat /tmp/hour2.txt) -1|bc >/tmp/run_hour.txt
if [ "$(cat /tmp/run_hour.txt)" == "0" ]; then
RUN_HOUR=00
else
RUN_HOUR=0$(cat /tmp/run_hour.txt)
fi
else
echo $(cat /tmp/hour.txt) -1|bc >/tmp/run_hour.txt
RUN_HOUR=$(cat /tmp/run_hour.txt)
fi
else
RUN_HOUR=$hour
fi
fi
# get run log file
if [ "$hour" == "00" ]; then
if [ "$min" == "00" ]; then
RUN_LOG=$logdir/$(date -d last-day +%Y%m%d_access_log)
else
RUN_LOG=$logfile
fi
else
RUN_LOG=$logfile
fi
# begin log analysis
p1=cnbpush.php
p2=jlpush.php
if [ "$hour" == "00" ]; then
if [ "$min" == "00" ]; then
INSERT_TIME=$(date -d '-1 hours' +%Y%m%d |cut -c 3-8)$RUN_HOUR$RUN_MINUTE
else
INSERT_TIME=$(date +%Y%m%d |cut -c 3-8)$RUN_HOUR$RUN_MINUTE
fi
else
INSERT_TIME=$(date +%Y%m%d |cut -c 3-8)$RUN_HOUR$RUN_MINUTE
fi
echo $INSERT_TIME | cut -c 1-2 > /tmp/IY
echo $INSERT_TIME | cut -c 3-4 > /tmp/IM
echo $INSERT_TIME | cut -c 5-6 > /tmp/ID
echo $INSERT_TIME | cut -c 7-8 > /tmp/IH
INSERT_YEAR=20$(cat /tmp/IY)
INSERT_MONTH=$(cat /tmp/IM)
INSERT_DAY=$(cat /tmp/ID)
INSERT_HOUR=$(cat /tmp/IH)
CN_TOTAL_ACCESS=$(cat $RUN_LOG | grep "$p1" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)
CN_SUCC_ACCESS=$(cat $RUN_LOG | grep "$p1" | grep "200" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)
CN_FAIL_ACCESS=$(cat $RUN_LOG | grep "$p1" | grep -v "200" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)
JL_TOTAL_ACCESS=$(cat $RUN_LOG | grep "$p2" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)
JL_SUCC_ACCESS=$(cat $RUN_LOG | grep "$p2" | grep "200" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)
JL_FAIL_ACCESS=$(cat $RUN_LOG | grep "$p2" | grep -v "200" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)
DB_NAME=shell_log
DB_USER=root
DB_PASS=root
DB_TAB=log
mysql -u$DB_USER -p$DB_PASS $DB_NAME -e "INSERT INTO $DB_TAB(time,year,month,day,hour,visit,success,fail,target) VALUES ('$INSERT_TIME', '$INSERT_YEAR', '$INSERT_MONTH', '$INSERT_DAY', '$INSERT_HOUR', '$CN_TOTAL_ACCESS', '$CN_SUCC_ACCESS', '$CN_FAIL_ACCESS', 'cnbpush');"
mysql -u$DB_USER -p$DB_PASS $DB_NAME -e "INSERT INTO $DB_TAB(time,year,month,day,hour,visit,success,fail,target) VALUES ('$INSERT_TIME', '$INSERT_YEAR', '$INSERT_MONTH', '$INSERT_DAY', '$INSERT_HOUR', '$JL_TOTAL_ACCESS', '$JL_SUCC_ACCESS', '$JL_FAIL_ACCESS', 'jlpush');"
exit
放到任务计划,一分钟一执行。
创造与魔法 安卓版v1.0.0750
创造与魔法是一款开放世界手游,在游戏中玩家可探索这个奇妙的世
创造与魔法修改版 最新版v1.0.0750
创造与魔法无限点券版是款探索冒险游戏,该款游戏的操作还是蛮自
战争与文明官方版本 安卓版v1.7.16
战争与文明是一款由上海邮通科技有限公司开发的战争策略游戏,这
迷你世界0元领皮肤无限迷你币版 最新安卓版v1.43.0
迷你世界0元购买皮肤版是这款开放沙盒冒险建造游戏的特殊破解版
创造与魔法无限经验版 安卓版v1.0.0750
创造与魔法无限经验版是款可以改造环境,整个游戏的自由度还是蛮