linux中shell指令碼統計apache日誌中頁面訪問量

來源:互聯網
上載者:User

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

放到任務計劃,一分鐘一執行。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.