fluentd是一个日志收集系统,它的特点在于其各部分均是可定制化的,你可以通过简单的配置,将日志收集到不同的地方。
目前开源社区已经贡献了下面一些存储插件:MongoDB, Redis, CouchDB,Amazon S3, Amazon SQS, Scribe, 0MQ, AMQP, Delayed, Growl 等等。
项目地址: https://github.com/fluent/fluentd
官网给出的两张张图能让你很直观的了解fluentd的作用
使用fluentd前,日志系统的状态:
使用fluentd后,日志系统的状态:
机制图解:
安装
配置NTP
不再赘述
修改文件描述符
vim /etc/security/limits.d/90-nproc.conf
* soft nproc 65535
root soft nproc unlimited
vim /etc/security/limits.conf
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536
修改内核网络部分配置
vim /etc/sysctl.conf
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240 65535
sysctl -p
配置官方源
vim /etc/yum.repose.d/td.repo
[treasuredata]
name=TreasureData
baseurl=http://packages.treasure-data.com/redhat/\$basearch
gpgcheck=1
gpgkey=http://packages.treasure-data.com/redhat/RPM-GPG-KEY-td-agent
安装
yum install td-agent
启动停止
service td-agent start|stop|restart
查看已安装插件
fluentd安装的时候会默认安装一些插件
/usr/lib64/fluent/ruby/bin/gem list|grep fluent-plugin
fluent-plugin-flume (0.1.1)
fluent-plugin-mongo (0.7.1)
fluent-plugin-rewrite-tag-filter (1.3.1)
fluent-plugin-s3 (0.3.5)
fluent-plugin-scribe (0.10.10)
fluent-plugin-tail-ex (0.1.1)
fluent-plugin-tail-multiline (0.1.5)
fluent-plugin-td (0.10.17)
fluent-plugin-td-monitoring (0.1.0)
fluent-plugin-webhdfs (0.2.1)
安装插件
fluent插件地址:http://fluentd.org/plugin/
下载后使用gem install
命令安装
配置
vim /etc/fluentd/fluentd.conf
<source>
type tail
format /^(?<remote>[^ ]*) (?<host>[-]) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*) "(?<referer>[^\"]*)" "(?<agent>[^\"]*)"/
time_format %d/%b/%Y:%H:%M:%S %z
path /data/logs/nginx/access.log
pos_file /data/logs/nginx/access.log.pos
tag mongo.nginx
</source>
<match mongo.**>
type mongo
database nginx
collection access
host localhost
port 27017
flush_interval 10s
</match>
其中:
- type tail: tail方式是 fluentd 内置的输入方式,其原理是不停地从源文件中获取新的日志。
- format nginx: 指定使用 fluentd 内置的 nginx 日志解析器。
- path /data/logs/nginx/access.log: 指定日志文件位置。
- pos_file /data/logs/nginx/access.log.pos:将上次的读取长度写入该文件,保证在fluentd服务重启后能够继续收集,避免日志数据收集丢失,保证数据收集的完整性(注意写入权限);
- tag mongo.nginx: 指定tag,tag被用来对不同的日志进行分类
- type mongo:fluentd插件类型,这里是针对mongodb的插件
- database:数据库名字
- collection:表名
- host:主机
- port:端口,默认27017
- flush_interval:刷新间隔,10s写入一次数据
关于MongoDB的安装在前一篇日志中有记录
测试
启动td-agent后,随便在浏览器上访问几次服务器上的url,查看日志文件
tail /data/logs/nginx/access.log
203.195.185.140 - - [03/Mar/2014:14:25:11 +0800] “-“ 400 0 “-“ “-“
再查看MongoDB中的相关数据
mongo
use nginx
db.access.find()
{ "_id" : ObjectId("53135d87319fd96c3600000e"), "remote" : "203.195.185.140", "host" : "-", "user" : "-", "method" : "-\"", "path" : "400 0", "code" : "\"-\"", "size" : "", "time" : ISODate("2014-03-02T16:34:11Z") }
{ "_id" : ObjectId("53135dc9319fd96c3600000f"), "remote" : "203.195.185.140", "host" : "-", "user" : "-", "method" : "-\"", "path" : "400 0", "code" : "\"-\"", "size" : "", "time" : ISODate("2014-03-02T16:35:11Z") }
.
.
.
关于fluentd与td-agent的区别
td-agent是fluentd的易安装版本;
fluentd适合折腾,td-agent适合安装在大规模的生产环境;
详细区别请看官网给出的解释:
| fluentd | td-agent
————————————-|————-|————-
Installation | gem install fluentd | .rpm/.deb packages for Linux. Homebrew for OSX
Configuration | generic | preconfigured to send data to Treasure Data (can be modified)
Adding 3rd party plugins | fluent-gem (ex: fluent-gem install fluent-plugin-td) | Manual setup (but it ships with several plugins pre-loaded)
init.d script | No (the user needs to write shell script to set it up) | Yes (shipped with .deb and .rpm)
Chef recipe | No | Yes
Memory allocator | OS default | optimized (jemalloc)
QA/Support | Community-driven | QA by Treasure Data/Support for Treasure Data’s paid customers