Fluentd是一个日志收集系统,它的特点在于其各部分均是可定制化的,你可以通过简单的配置,将日志收集到不同的地方。
目前开源社区已经贡献了下面一些存储插件: MongoDB, Redis, CouchDB, Amazon S3, Amazon SQS, Scribe, 0MQ, AMQP, Delayed, Growl 等等。
本文要介绍的是在 Fluentd的最新版中已经内置的 MongoDB支持。主要通过一个收集Apache日志的例子来说明其使用方法:
机制图解
为了完成相关的测试,需要安装下面一些组件:
在Fluentd的最新安装包中已经包含了MongoDB插件,你也可以用命令
来进行安装
yum install td-agent.x86_64
如果你是使用上面的deb/rpm包安装的Fluentd,那么配置文件位置在:/etc/td-agent/td-agent.conf,否则其位置应该在:/etc/fluentd/fluentd.conf
首先我们编辑配置文件中的source来设置日志来源
其中:
下面再来编辑输出配置,配置日志收集后存储到MongoDB中
match标签后面可以跟正则表达式以匹配我们指定的tag,只有匹配成功的tag对应的日志才会运用里面的配置。配置中的其它项都比较好理解,看注释就可以了,其中flush_interval是用来控制多长时间将日志写入MongoDB一次。
用ab工具对Apache进行访问,以产生相应的访问日志以供收集
然后我们在MongoDB中就能看到收集到的日志了
<source>
type tail
path /home/logs/nginx/domain.access.log
pos_file /var/log/td-agent/tmp/domain.com.pos
format /^([^ ]*) (?<domain>[^ ]*) - ([^ ]*) ([^ ]*) ("[^"]*") (?<code>[^ ]*) (?<bsize>[^ ]*) ("[^"]*") ("[^"]*") ("[^"]*") "(?<r
estime>[^"]*)" "(?<backend>[^,"]*)[^"]*" "(?<bcode>[^,"]*)[^"]*" "(?<nginxtime>[^"]*)"$/
tag mongo.domain.com.access
</source>
<match mongo.domain.com.access>
type mongo
database SHBNJ-LB-core-205-98
collection 3g.pptv.com.access
host 10.204.10.88
port 27017
capped
capped_size 5m
buffer_type memory
buffer_chunk_limit 2M
buffer_queue_limit 50
retry_limit 15
retry_wait 1s
flush_interval 2s
</match>
目前开源社区已经贡献了下面一些存储插件: MongoDB, Redis, CouchDB, Amazon S3, Amazon SQS, Scribe, 0MQ, AMQP, Delayed, Growl 等等。
本文要介绍的是在 Fluentd的最新版中已经内置的 MongoDB支持。主要通过一个收集Apache日志的例子来说明其使用方法:
机制图解 
安装
为了完成相关的测试,需要安装下面一些组件:
- Fluentd with MongoDB Plugin
- MongoDB
- Apache (with the Combined Log Format)
在Fluentd的最新安装包中已经包含了MongoDB插件,你也可以用命令
gem install fluent-plugin-mongo
来进行安装
配置
yum install td-agent.x86_64
如果你是使用上面的deb/rpm包安装的Fluentd,那么配置文件位置在:/etc/td-agent/td-agent.conf,否则其位置应该在:/etc/fluentd/fluentd.conf
首先我们编辑配置文件中的source来设置日志来源
<source> type tail format apache path /var/log/apache2/access_log tag mongo.apache </source>
其中:
- type tail: tail方式是 Fluentd 内置的输入方式,其原理是不停地从源文件中获取新的日志。
- format apache: 指定使用 Fluentd 内置的 Apache 日志解析器。
- path /var/log/apache2/access_log: 指定日志文件位置。
- tag mongo.apache: 指定tag,tag被用来对不同的日志进行分类
下面再来编辑输出配置,配置日志收集后存储到MongoDB中
<match mongo.**> # plugin type type mongo # mongodb db + collection database apache collection access # mongodb host + port host localhost port 27017 # interval flush_interval 10s </match>
match标签后面可以跟正则表达式以匹配我们指定的tag,只有匹配成功的tag对应的日志才会运用里面的配置。配置中的其它项都比较好理解,看注释就可以了,其中flush_interval是用来控制多长时间将日志写入MongoDB一次。
测试
用ab工具对Apache进行访问,以产生相应的访问日志以供收集
$ ab -n 100 -c 10 http://localhost/
然后我们在MongoDB中就能看到收集到的日志了
$ mongo > use apache > db.access.find() { "_id" : ObjectId("4ed1ed3a340765ce73000001"), "host" : "127.0.0.1", "user" : "-", "method" : "GET", "path" : "/", "code" : "200", "size" : "44", "time" : ISODate("2011-11-27T07:56:27Z") } { "_id" : ObjectId("4ed1ed3a340765ce73000002"), "host" : "127.0.0.1", "user" : "-", "method" : "GET", "path" : "/", "code" : "200", "size" : "44", "time" : ISODate("2011-11-27T07:56:34Z") } { "_id" : ObjectId("4ed1ed3a340765ce73000003"), "host" : "127.0.0.1", "user" : "-", "
完整的配置文件:
<source>
type tail
path /home/logs/nginx/domain.access.log
pos_file /var/log/td-agent/tmp/domain.com.pos
format /^([^ ]*) (?<domain>[^ ]*) - ([^ ]*) ([^ ]*) ("[^"]*") (?<code>[^ ]*) (?<bsize>[^ ]*) ("[^"]*") ("[^"]*") ("[^"]*") "(?<r
estime>[^"]*)" "(?<backend>[^,"]*)[^"]*" "(?<bcode>[^,"]*)[^"]*" "(?<nginxtime>[^"]*)"$/
tag mongo.domain.com.access
</source>
<match mongo.domain.com.access>
type mongo
database SHBNJ-LB-core-205-98
collection 3g.pptv.com.access
host 10.204.10.88
port 27017
capped
capped_size 5m
buffer_type memory
buffer_chunk_limit 2M
buffer_queue_limit 50
retry_limit 15
retry_wait 1s
flush_interval 2s
</match>
转自: http://blog.nosqlfan.com/html/3521.html
相关博客: http://blog.nosqlfan.com/html/3548.html