#!/bin/bash# this script run at 00:00# the nginx logs path# Zhengyansheng time:2014-08-27# qq 467754239logs_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 "). logcat ${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 reloadcat $logs _path/ Access_mongo.log | while read line;docookieid= ' 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.txtdonecat $logs _path/nginx_log_mongo.txt | mongo 192.168.0.100:27017/hktv_tv -u hktv -p cdvcloud#123mv $logs _path/nginx_log_mongo.txt $logs _path/nginx_log_mongo_bak.txt
Mission Plan! The log is cut, then stored in the MongoDB database,
*/5 * * * */bin/bash/usr/local/nginx/logs/nginx_mongo.sh
This script has been optimized several times and the thread has run successfully! If there are deficiencies please also point out, thank you
This article is from the "Zheng" blog, make sure to keep this source http://467754239.blog.51cto.com/4878013/1548026
Nginx log logs stored to MongoDB do video click Leaderboard