source
"source": where all the data come from
source:就是输入源(input),比较常用的有两个插件一个是http,一个是forward(tcp)
其它的插件,可以去官网查找
# 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
"match": Tell fluentd what to do
match:指定输出的目的(output)
# 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 # 这个就是output插件
path /var/log/fluent/access
</match>
filter
"filter": Event processing pipeline
filter:可以理解为过滤器
流程如下:
Input -> filter 1 -> ... -> filter N -> Output
如下为record_transformer
filter插件实例
# http://this.host:9880/myapp.access?json={"event":"data"}
<source>
@type http
port 9880
</source>
<filter myapp.access>
@type record_transformer
<record>
host_param "#{Socket.gethostname}"
</record>
</filter>
<match myapp.access>
@type file
path /var/log/fluent/access
</match>
label
就是为了降低tag过滤的复杂性
<source>
@type forward
</source>
<source>
@type tail
@label @SYSTEM # 这个input 直接进到label 哪里进行处理
</source>
<filter access.**>
@type record_transformer
<record>
# ...
</record>
</filter>
<match **>
@type elasticsearch
# ...
</match>
<label @SYSTEM>
<filter var.log.middleware.**>
@type grep
# ...
</filter>
<match **>
@type s3
# ...
</match>
</label>
system
主要设置一些系统配置的
- log_level
- suppress_repeated_stacktrace
- emit_error_log_interval
- suppress_config_dump
- without_source
- process_name (only available in system directive. No fluentd
option)
在td-agent.conf文件中添加如下
<system>
process_name fluentd1
</system>
% ps aux | grep fluentd1
foo 45673 0.4 0.2 2523252 38620 s001 S+ 7:04AM 0:00.44 worker:fluentd1
foo 45647 0.0 0.1 2481260 23700 s001 S+ 7:04AM 0:00.40 supervisor:fluentd1
include
就是导入
第一步:先编写source.conf
<source>
@type http
port 8887
bind 0.0.0.0
</source>
第二步:编写td-agent.conf
文件
@include ./source.conf # 这一步:就会将上面的内容导入到这里
<filter test.cycle>
@type grep
<exclude>
key action
pattern ^login$
</exclude>
</filter>
<label @STAGING>
<filter test.cycle>
@type grep
<exclude>
key action
pattern ^logout$
</exclude>
</filter>
<match test.cycle>
@type stdout
</match>
</label>
<match test.cycle>
@type stdout
</match>
通配符
filter 和 match 标签中的tag 通配符号
*
:匹配满足一个tag部分的事件, 比如: a.*, 它将匹配a.b这样的tag, 但是不会处理a或者a.b.c这类tag**
:匹配满足0个或多个tag部分,比如: a.**, 它将匹配a, a.b, a.b.c这三种tag{X,Y,Z}
:匹配满足X,Y或者Z的tag, 比如: {a, b}将匹配a或者b,但是不会匹配c。这种格式也可以和通配符组合使用,比如a.{b.c}.*或a.{b.c}.*#{...}
:会将里面的内容当作ruby表达式处理:比如
<match "app.#{ENV['FLUENTD_TAG']}">
@type stdout
</match>
如果设置了环境变量FLUENTD_TAG
为dev
,那上面等价于app.dev
- 当指定了多个模式时(使用一个或多个空格分开),只要满足其中任意一个就行.比如:
<match a b>
匹配a和b
<match a.** b.*>
匹配a, a.b, a.b.c, b.d等
配置文件中的参数类型
每个Fluentd插件都有一组参数。例如,in_tail具有rotate_wait和pos_file等参数。每个参数都有一个与之关联的特定类型。它们的定义如下:
-
string
:字符串,最常见的格式,详细支持语法见文档[^literal]; -
integer
:整数 -
float
:浮点数; -
size
大小,仅支持整数 -
<INTEGER>k
或<INTERGER>K
; -
<INTEGER>m
或<INTERGER>M
; -
<INTEGER>g
或<INTERGER>G
; -
<INTEGER>t
或<INTERGER>T
。 -
time
:时间,也只支持整数;<INTEGER>s
或<INTERGER>S
;<INTEGER>m
或<INTERGER>M
;<INTEGER>h
或<INTERGER>H
;<INTEGER>d
或<INTERGER>D
。
-
array
:按照 JSON array 解析
完整格式的写法:["key1", "key2"]
简写:key1,key2
-
hash
:按照 JSON object 解析
完整格式的写法:{"key1":"value1", "key2":"value2"}
简写:key1:value1,key2:value2
多个match之间的顺序
当有多个match, 需要注意一下它们的顺序, 如下面的例子,第二个match永远也不会生效
# ** matches all tags. Bad :(
<match **>
@type blackhole_plugin
</match>
<match myapp.access>
@type file
path /var/log/fluent/access
</match>
如果将filter放在match之后,那么它也永远不会生效,正确的用法如下:
# You should NOT put this <filter> block after the <match> block below.
# If you do, Fluentd will just emit events without applying the filter.
<filter myapp.access>
@type record_transformer
...
</filter>
<match myapp.access>
@type file
path /var/log/fluent/access
</match>
检查配置文件是否可用
fluentd --dry-run -c fluent.conf