linux中shell脚本统计apache日志中页面访问量
2019/10/10/17:46:14 阅读:3333 来源:谷歌SEO算法 标签:
自媒体
apache日志是可以记录到网站的各种访问与状态了,那么我们生成的日志一个个查看是非常的困难的,下面一起来看一个中shell脚本统计apache日志中页面访问量的例子,希望文章可以帮助到大家.
需求:在程序中有两个页面,用于某种用途,现需要统计这两个页面在每分钟内的访问量,包括总访问次数,成功次数(状态码200),失败次数,然后写入到mysql中.
mysql字段:id(自增长)、time(实际统计时间)、year、month、day、hour、visit(总访问量)、success、fail、target(目标).
- #!/bin/bash
- #Desc:analysisapachelogforwitchpageswithoneminutes
- #installbcpackageswithyuminstall-ybcifnotfoundbccommand
- 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)
- #getcurrentsystemtimesto/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
- #computerunminute
- 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
- #computerunhour
- 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
- #getrunlogfile
- if["$hour"=="00"];then
- if["$min"=="00"];then
- RUN_LOG=$logdir/$(date-dlast-day+%Y%m%d_access_log)
- else
- RUN_LOG=$logfile
- fi
- else
- RUN_LOG=$logfile
- fi
- #beginloganalysis
- p1=cnbpush.php
- p2=jlpush.php
- if["$hour"=="00"];then
- if["$min"=="00"];then
- INSERT_TIME=$(date-d'-1hours'+%Y%m%d|cut-c3-8)$RUN_HOUR$RUN_MINUTE
- else
- INSERT_TIME=$(date+%Y%m%d|cut-c3-8)$RUN_HOUR$RUN_MINUTE
- fi
- else
- INSERT_TIME=$(date+%Y%m%d|cut-c3-8)$RUN_HOUR$RUN_MINUTE
- fi
- echo$INSERT_TIME|cut-c1-2>/tmp/IY
- echo$INSERT_TIME|cut-c3-4>/tmp/IM
- echo$INSERT_TIME|cut-c5-6>/tmp/ID
- echo$INSERT_TIME|cut-c7-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"INSERTINTO$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"INSERTINTO$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');"--phpfensi.com
- exit
放到任务计划,一分钟一执行.
热门评论