由于项目需求,需要将玩家信息从日志中存入到DB中,DB暂选MySQL.
本文并未使用开源的向fluentd http://fluentd.org/doc/overview.html一样的系统,我的工作只是日志分析,存入DB,剩下的是开发人员如何展示数据啦。现列出部分脚本,欢迎各位提出意见:
日志收集脚本(用shell搞定),核心思想是scp,并用cat 合并文件。
此程序分3个目录,bin目录下可执行脚本collect_log.sh expect_rsync.sh
conf 目录,service_server sync_file web_server 三个文件存储ip,和文件名称
data目录 存储收集、合并结果
expect_rsync.sh 远程传输:
#!/usr/bin/expect -f
set host [lindex $argv 0] set remote_dir [lindex $argv 1] set file [lindex $argv 2] set local_dir [lindex $argv 3] spawn scp "xxxxxx@$host#36000:$remote_dir/$file" $local_dir expect { -re "assword:" { send "xxxxxx\r" } -re "Permission denied, please try again." { exit } -re "Connection refused" { exit } timeout { exit } eof { exit } } expect eof exit
collect_log.sh 判断机器和文件名
#!/bin/bash WEB_REMOTE_DIR=/data/matrixjoyweb/matrixjoy-dragon-web/logs SERVICE_REMOTE_DIR=/data/matrixjoyservice/matrixjoy-dragon-gameserver/logs/ SCDIR=/data/log-system/ RAWDATADIR=$SCDIR/data/raw/ LOGDATE=$(date +%Y%m%d --date="-$1 day") #transport function function expect_rsync_logs() { /usr/bin/expect -f $SCDIR/bin/expect_rsync.sh $@ } #mkdir directory function ensure_dir() { if [ ! -d $1 ] then mkdir -p $1 fi } function collect() { server_list=$1 remote_dir=$2 dirtype=$3 for serverip in $server_list do store_dir="$RAWDATADIR/$serverip/$dirtype/$LOGDATE" ensure_dir $store_dir for file in $(cat $SCDIR/conf/sync_file) do file_name=$file$LOGDATE #Call the funciton to sync file expect_rsync_logs $serverip $remote_dir $file_name $store_dir done done } function main() { source /etc/profile ensure_dir $RAWDATADIR IFS=" " collect $(cat $SCDIR/conf/service_server) $SERVICE_REMOTE_DIR service collect $(cat $SCDIR/conf/web_server) $WEB_REMOTE_DIR web } #merge log function merge() { TARGET="$SCDIR/data/total/$LOGDATE" server_ip_list=$1 dir_type=$2 echo $TARGET ensure_dir $TARGET for file in $(cat $SCDIR/conf/sync_file) do file_name=$file$LOGDATE for ip in $server_ip_list do srcdir="$RAWDATADIR/$ip/$dir_type/$LOGDATE" cat "$srcdir/$file_name" >> "$TARGET/$file_name" done done } function merge_result() { ensure_dir $TARGET IFS=" " merge $(cat $SCDIR/conf/service_server) service merge $(cat $SCDIR/conf/web_server) web } main merge_result
待续。。。
本文转自 位鹏飞 51CTO博客,原文链接:http://blog.51cto.com/weipengfei/1022288,如需转载请自行联系原作者