使用Fluentd + MongoDB构建实时日志收集系统
来源:互联网 发布:windows自带画图软件 编辑:程序博客网 时间:2024/06/05 09:51
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
来进行安装
- Debian Package
- RPM Package
- MongoDB Downloads
配置
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
- 使用Fluentd + MongoDB构建实时日志收集系统
- 使用Fluentd + MongoDB构建实时日志收集系统
- 使用Fluentd + MongoDB构建实时日志收集系统
- Fluentd (td-agent) 日志收集系统
- 利用fluentd构建分布式日志管理系统
- Elasticsearch+Fluentd+Kibana的日志收集分析系统
- kubernetes上部署Fluentd+Elasticsearch+kibana日志收集系统
- 使用开源工具fluentd-pilot收集容器日志
- 日志收集工具之Fluentd
- 通过fluentd收集docker日志
- fluentd分布式日志管理系统
- hadoop日志收集解决方案--fluentd配置文件
- docker容器日志的收集---fluentd
- Fluentd+Hadoop结合进行日志收集、分析
- 万能日志数据收集器 Fluentd
- 使用 Fluentd 管理 Docker 日志
- 使用fluentd管理docker日志
- 基于Elasticsearch+Fluentd+Kibana的日志收集分析系统搭建与应用
- 不重复的随机数
- 【2012年腾讯俱乐部ACM赛新手组1000】我水平弱爆了—Love Message
- socket的一些基本结构和用法
- 兼容IE、Firefox、Google的回车事件
- Oracle 11g 新特性 -- RMAN Data Recovery Advisor(DRA) 说明
- 使用Fluentd + MongoDB构建实时日志收集系统
- 使用TCP协议的NAT穿透技术 (转)
- ios之Category,Extension,Protocol,delegate
- 得到本机ip
- 你必须知道的28个HTML5特征、窍门和技术
- 如何合并apk和odex
- a letter and a number
- Python获取磁盘空闲空间并写入日志
- 查找数组中前K个大的和小的元素