0


Fluentd (tD-agent) 日志处理

1、td-agent是什么

td-agent是一个日志采集器,提供了丰富的插件来适配不同的数据源、输出目的地等

在使用上,我们可以把各种不同来源的信息,通过简单的配置,将日志收集到不同的地方,首先发送给Fluentd,接着Fluentd根据配置通过不同的插件把信息转发到不同的 地方,比如文件、SaaS Platform、数据库,甚至可以转发到另一个Fluentd。
在这里插入图片描述

2、如何安装td-agent

Linux系统:centos

2.1 执行脚本

  1. curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh | sh

2.2 查看是否安装

  1. rpm -qa|grep td-agent

2.3 启动命令

  1. 启动td-agent systemctl start td-agent
  1. 启动服务 /etc/init.d/td-agent start
  2. 查看服务状态 /etc/init.d/td-agent status
  3. 停止服务 /etc/init.d/td-agent stop
  4. 重启服务 /etc/init.d/td-agent restart

2.4 默认配置文件路径

  1. /etc/td-agent/td-agent.conf

2.5 默认日志文件路径:

  1. /var/log/td-agent/td-agent.log

3、 名词解释

source:指定数据源
match:指定输出地址
filter:指定了一个事件处理过程
system:用来设置系统的配置
label:为output和filter分组
@include:使用它可以在配置文件里面包含其他的配置文件
插件:fluentd采集发送日志时要使用插件,一些插件是内置的,要使用非内置的插件需要安装插件

4、配置文件解析

  1. # Receive events from 20000/tcp
  2. # This is used by log forwarding and the fluent-cat command
  3. <source>
  4. @type forward
  5. port 20000
  6. </source>
  7. # http://this.host:8081/myapp.access?json={"event":"data"}
  8. <source>
  9. @type http
  10. port 8081
  11. </source>
  12. <source>
  13. @type tail
  14. path /root/shell/test.log
  15. tag myapp.access
  16. </source>
  17. # Match events tagged with "myapp.access" and
  18. # store them to /var/log/td-agent/access.%Y-%m-%d
  19. # Of course, you can control how you partition your data
  20. # with the time_slice_format option.
  21. <match myapp.access>
  22. @type file
  23. path /var/log/td-agent/access
  24. </match>

sources 配置日志文件的来源

@type :指定配置文件来自哪里

forward:来自另一个fluent

http: 来自一个http请求传的参数

tail:来自一个日志文件

port:读取其他机器传的数据时,开发的数据传输端口

path: 读取的数据位置

tag: 数据的标签,和 match配置的标签进行匹配

match 数据转发配置

myapp.access:输出的标签,和输入的标签进行匹配

@type:输出位置,可以输出到kafak,本地文件,数据库,monggo得到

path:输出到文件时,文件的路径,如果输出到其他位置,还会有其他的专项的配置,比如下面这个配置,因为是转发到Kafka,所以match标签中还配置了很多关于Kafka的配置,

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wU9d5qLk-1642839362691)(img/1642670826444.png)]

里的store标签,每个store表示这个tag属性的数据的存储方向,我们可以配置往Kafka存储,可以配置本地文件存储

  1. <source>typetailformat none
  2. path /var/log/apache2/access_log
  3. pos_file /var/log/apache2/access_log.pos
  4. tag mytail
  5. </source>

5、部分参数解释

  1. format:配置表达式,去过滤数据,只有满足format表达式的字符串才能在match中进行store存储。
  2. type tail: tail方式是 Fluentd 内置的输入方式,其原理是不停地从源文件中获取增量日志,与linx命令tail相似,也可以使用其他输入方式如httpforward等输入,也可以使用输入插件,将 tail 改为相应的插件名称 如: type tail_ex ,注意tail_ex为下划线。
  3. format apache: 指定使用 Fluentd 内置的 Apache 日志解析器。可以自己配置表达式。
  4. path /var/log/apache2/access_log: 指定收集日志文件位置。
  5. Pos_file /var/log/apache2/access_log.pos:强烈建议使用此参数,access_log.pos文件可以自动生成,要注意access_log.pos文件的写入权限,因为要将access_log上次的读取长度写入到该文件,主要保证在fluentd服务宕机重启后能够继续收集,避免日志数据收集丢失,保证数据收集的完整性。

6、配置文件案例

6.1、通过http的方式,同时往日志和Kafka传输数据

  1. # http://this.host:8888/mytail?json={"event":"data"}
  2. <source>
  3. @type http
  4. port 8081
  5. </source>
  6. <match mytail>
  7. @type copy
  8. <store>
  9. @type kafka
  10. brokers localhost:9092
  11. default_topic test1
  12. default_message_key message
  13. ack_timeout 2000
  14. flush_interval 1
  15. required_acks -1
  16. </store>
  17. <store>
  18. @type file
  19. path /var/log/td-agent/access
  20. </store>
  21. </match>

6.2 通过读取文件文件,同时往日志和Kafka传输数据

  1. <source>
  2. type tail
  3. format none
  4. path /var/log/apache2/access_log
  5. pos_file /var/log/apache2/access_log.pos
  6. tag mytail
  7. </source>
  8. <match mytail>
  9. @type copy
  10. <store>
  11. @type kafka
  12. brokers localhost:9092
  13. default_topic test1
  14. default_message_key message
  15. ack_timeout 2000
  16. flush_interval 1
  17. required_acks -1
  18. </store>
  19. <store>
  20. @type file
  21. path /var/log/td-agent/access
  22. </store>
  23. </match>
标签: 运维 服务器

本文转载自: https://blog.csdn.net/qq_45171957/article/details/122639120
版权归原作者 i进击的攻城狮 所有, 如有侵权,请联系我们删除。

“Fluentd (tD-agent) 日志处理”的评论:

还没有评论