需求:在程式中有兩個頁面,用於某種用途,現需要統計這兩個頁面在每分鐘內的訪問量,包括總訪問次數,成功次數(狀態代碼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
放到任務計劃,一分鐘一執行。