写在前面
Fluentd的配置文件让我们可以通过指定输入输出插件以及设置各个插件的参数来控制Fluentd的输入输出行为。
配置文件的位置
由于我们之前采用的是.rpm/.deb方式安装的稳定版本的Fluentd——td-agent,采用这种方式安装的配置文件在此:
$ sudo vi /etc/td-agent/td-agent.conf
配置文件中一些基本指令source
决定输入源match
决定输出目的地include
指定配置文件的包含实战source
Fluentd的数据输入源是通过source
指令,选取使用什么输入插件,并加以配置来决定的。Fluentd标准输入插件有两个:http
和forward
。
http
指定Fluentd接收来自HTTP的数据信息forward
指定Fluentd接收来自TCP的数据包的信息
二者可以同时存在,也即Fluentd可以同时接受来自两个源,甚至多个源的信息。
每个source指令必须包含一个type
参数。,用来指定该source使用哪个输入插件,例如:
# Receive events from 24224/tcp# This is used by log forwarding and the fluent-cat command<source> type forward port 24224</source># http://this.host:9880/myapp.access?json={"event":"data"}<source> type http port 9880</source>
插播消息一:Routing
Fluentd接收到数据后,输入的数据被提交到Fluentd内部的路由模块。一条数据记录包括三方面的信息,tag,time,record。tag是一个被“.”分隔开的字符串(比e.g. myapp.access),用做路由引擎路由时的依据。time域是由输入插件指定的,必须采用Unix时间格式,record就是数据的具体内容了。一条数据记录是一个JSON对象。
比如在上述例子中,这个请求http://this.host:9880/myapp.access?json={"event":"data"}
,让Fluentd的HTTP输入插件向路由模块提交了如下信息:
# generated by http://this.host:9880/myapp.access?json={"event":"data"}tag: myapp.accesstime: (current time)record: {"event":"data"}
插播消息二:自己写输入插件
在这里查看更多信息。
match
——告诉Fluentd用收到的数据做什么!
match根据上述的数据记录的tag信息对记录做出不同的处理。最常见的是通过该指令告诉Fluentd将收到的数据输出的不同的地方(这也是为什么match指令中指定的插件被称为输出插件)。Fluentd标准的输出插件是file
和forward
。
# Receive events from 24224/tcp# This is used by log forwarding and the fluent-cat command<source> type forward port 24224</source># http://this.host:9880/myapp.access?json={"event":"data"}<source> type http port 9880</source># Match events tagged with "myapp.access" and# store them to /var/log/fluent/access.%Y-%m-%d# Of course, you can control how you partition your data# with the time_slice_format option.<match myapp.access> type file path /var/log/fluent/access</match>
类似地,每个match指令都得包含一个type
参数来指定输出插件。在上面的例子中,只有tag匹配上myapp.access的数据记录会被输出到/var/log/fluent/access中。
forward
输出插件是将接收到的数据流转发到其他Fluentd节点,不赘述。
当然,也可以写自己的输出插件,更多信息请看这里。
match指令中的tag模式可以支持通用字符匹配。
*
a.*
可以匹配a.b
,a.c
,不可以匹配a
和a.b.c
**
a.**
可以匹配a
,a.b
,a.b.c
{X, Y, Z}
{a, b}
可以匹配a
,b
,不可以匹配c
以上可以混合使用,例如a.{b,c}.*
,a.{b,c.**}
。
include
——重用配置
例子:
# absolute pathinclude /path/to/config.conf# if using a relative path, the directive will use # the dirname of this config file to expand the pathinclude extra.conf# glob match patterninclude config.d/*.conf# httpinclude http://example.com/fluent.conf
写在前面
Fluentd的配置文件让我们可以通过指定输入输出插件以及设置各个插件的参数来控制Fluentd的输入输出行为。