nginx的log日志存储到mongodb做视频点击排行榜
#!/bin/bash # This script run at 00:00 # The Nginx logs path # zhengyansheng time:2014-08-27 # Qq 467754239 logs_path="/usr/local/nginx/logs/" access_dir="/usr/local/nginx/logs/Insert_mongodb" mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m"+"%d") mv $logs_path/access_hktv.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m"+"%d")/access_$(date -d "yesterday" +"%Y%m%d%H%M").log cat ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m"+"%d")/access_$(date -d "yesterday" +"%Y%m%d%H%M").log > $logs_path/access_mongo.log /usr/local/nginx/sbin/nginx -s reload cat $logs_path/access_mongo.log | while read line; do cookieId=`echo $line |awk -F "uid=" ‘{print $2}‘ | awk ‘{print $1}‘ | grep -v "^$"` ip=`echo $line | awk ‘{print $3}‘` appKey=`echo $line | awk ‘{print $1}‘` visidate=`echo $line | awk ‘{print $2}‘ | awk -F "[" ‘{print $2}‘` browsertype=`echo $line | awk -F "\"" ‘{print $6}‘` refer=`echo $line | awk -F "\"" ‘{print $4}‘` os=`echo $line | awk -F "\"" ‘{print $6}‘ |awk ‘{print $1}‘` videoID=`echo $line | gawk -F‘?id=‘ ‘{ print $2 }‘ |cut -d‘&‘ -f1` sql="db.user.visit.data.insert({browsertype:‘$browsertype‘,cookieId:‘$cookieId‘,appKey:‘$appKey‘,ip:‘"$ip"‘,os:‘"$os"‘,referer:‘"$refer"‘,videoId:‘$videoID‘,visitdate:ISODate(‘$visidate‘)})" echo "$sql" >> $logs_path/nginx_log_mongo.txt done cat $logs_path/nginx_log_mongo.txt | mongo 192.168.0.100:27017/hktv_tv -u hktv -p cdvcloud#123 mv $logs_path/nginx_log_mongo.txt $logs_path/nginx_log_mongo_bak.txt
任务计划!先日志切割、然后再存储到mongodb数据库中,
*/5 * * * * /bin/bash /usr/local/nginx/logs/nginx_mongo.sh
这个脚本已多次优化并在线程成功运作!如果有不足之处还请指出,谢谢
本文出自 “郑彦生” 博客,请务必保留此出处http://467754239.blog.51cto.com/4878013/1548026
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。