Fluentd是一个通用的数据收集框架,通常用来作为统一的日志处理平台,这篇文章主要记录Fluentd的学习与使用过程,也包括一些采坑的点,和大家分享经验。
安装一个fluentd的环境是一个基本操作, 最有价值的参考 信息 当然是fluentd的指导文档了,URL链接为: https://docs.fluentd.org/installation
笔者使用的Euler2.9的环境, 在安装过程中遇到了一些坑。
推荐使用rpm包的方式安装td-agent— fluentd的一个稳定发布版本(stable distribution)
推荐原因: 简单方便, 不需要安装ruby通过gem安装。
安装指导:
主要可以参考:https://docs.fluentd.org/installation/install-by-rpm
要点:
# td-agent 4
$ curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent4.sh | sh
sudo systemctl restart td-agent.service
总结:
yum install ruby
gem install fluentd --no-doc
但是这两个命令对euler操作系统并不友好,遇到如下报错, 尝试解决未果
mkmf.rb can't find header files for ruby at /usr/share/include/ruby.h
docker pull fluent/fluentd:v1.7-1
docker run -p 8888:8888 --rm -v $(pwd)/etc:/fluentd/etc -v $(pwd)/log:/fluentd/log fluent/fluentd:v1.7-1 -c /fluentd/etc/fluentd_basic_setup.conf -v
td-agent默认配置了 8888端口的监听,可以用于测试
curl -X POST -d 'json={"json":"message"}' http://localhost:8888/debug.test
tail -n 1 /var/log/td-agent/td-agent.log
效果:
2022-08-02 20:30:50.129095885 +0800 debug.test: {"json":"message"}
参考文档:
http://t.zoukankan.com/wzs5800-p-13528430.html
需求推动学习, 描述一下自己在实践中遇到的需求:
目的: 将日志自动接入到日志平台中。
先通过几个简单的示例熟悉这个中间件的使用。
<source>
@type tail
path /home/fluentd/test.log
pos_file /var/log/td-agent/test.pos
tag fluent.test
<parse>
@type none
</parse>
</source>
<match **>
@type stdout
</match>
source: 配置数据的来源
# Receive events from 24224/tcp
# This is used by log forwarding and the fluent-cat command
<source>
@type forward
port 24224
</source>
# http://<ip>:9880/myapp.access?json={"event":"data"}
<source>
@type http
port 9880
</source>
可以添加的数据配置
tag: myapp.access # 指定数据的方向 time: (current time) # 时间 record: {“event”:“data”} # 记录,json格式 match: 指定输出的方向
match: 可以设置日志的输出
下面的例子可以通过发送http请求来获取日志
# http://<ip>: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>
下面的例子是 把 nginx 日志传入到kafka
<source>
@type tcp
port 1517
bind 0.0.0.0
tag nginx
<parse>
@type syslog
</parse>
</source>
<match nginx>
@type kafka2
brokers 1.2.3.4:9092
use_event_time false
<format>
@type json
</format>
topic_key nginx_log
default_topic nginx_log
</match>
发现有几个标签的功能不是特别了解,在这里补充一下
@type : 表示输入的插件
@label : 个人理解是为了简化tag的路由。 在<source>中指定了输入和label, 而label又关联了对应的filter和match, 这样可以简化逻辑。
<parse></parse> : 可以用于<source> <match> <filter>中, 是一个解析插件, 可以解析csv, nginx, json等格式的数据。
<format></format> : 用于<match><filter>中, 输出为csv,json等格式,作用是输出的格式化。
如果kafa作为数据生产者的话,可以通过如下方式进行消息传播
发送消息:
bin/kafka- console -producer.sh --broker-list localhost:9092 --topic test
在这里, 可以把fluentd看作生产者, 那么如何进行fluentd的配置呢?
需求场景:
环境中会记录一些日志, 日志信息追加到某个特定的日志文件 test. log 中, 下面希望配置fluentd和kafka对接,配置文件如下:
<source>
@type tail
path /opt/test/test.log
pos_file /var/log/td-agent/test.pos
tag nuclei
<parse>
@type regexp
expression /^\[(?<logtime>[^\]]*)\] \[(?<vul_name>[^\]]*)\] \[(?<protocal>[^\]]*)\] \[(?<level>[^\]]*)\] (?<url>[^ ]*)(?<detail>.*)$/
</parse>
</source>
<match nuclei>
@type kafka2
brokers ip:port
use_event_time false
<format>
@type json
</format>
topic_key nuclei
default_topic nuclei
</match>
调试总结:
对于各种格式的匹配, 建议使用正则的方式。
正则的调试可以在下面网址进行:
https://rubular.com/r/xfQHocREGj
在实践中重点关注了tail 方式收集日志,fluentd 还支持其他许多种输入的方式,

免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删