1、td-agent是什么
td-agent是一個日志采集器,提供了豐富的插件來適配不同的資料源、輸出目的地等
在使用上,我們可以把各種不同來源的資訊,通過簡單的配置,將日志收集到不同的地方,首先發送給Fluentd,接著Fluentd根據配置通過不同的插件把資訊轉發到不同的 地方,比如檔案、SaaS Platform、資料庫,甚至可以轉發到另一個Fluentd,

2、如何安裝td-agent
Linux系統:centos
2.1 執行腳本
curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh | sh
2.2 查看是否安裝
rpm -qa|grep td-agent

2.3 啟動命令
啟動td-agent systemctl start td-agent
啟動服務 /etc/init.d/td-agent start
查看服務狀態 /etc/init.d/td-agent status
停止服務 /etc/init.d/td-agent stop
重啟服務 /etc/init.d/td-agent restart
2.4 默認組態檔路徑
/etc/td-agent/td-agent.conf
2.5 默認日志檔案路徑:
/var/log/td-agent/td-agent.log
3、 名詞解釋
source:指定資料源
match:指定輸出地址
filter:指定了一個事件處理程序
system:用來設定系統的配置
label:為output和filter分組
@include:使用它可以在組態檔里面包含其他的組態檔
插件:fluentd采集發送日志時要使用插件,一些插件是內置的,要使用非內置的插件需要安裝插件
4、組態檔決議
# Receive events from 20000/tcp
# This is used by log forwarding and the fluent-cat command
<source>
@type forward
port 20000
</source>
# http://this.host:8081/myapp.access?json={"event":"data"}
<source>
@type http
port 8081
</source>
<source>
@type tail
path /root/shell/test.log
tag myapp.access
</source>
# Match events tagged with "myapp.access" and
# store them to /var/log/td-agent/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/td-agent/access
</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存盤,可以配置本地檔案存盤
<source>
type tail
format none
path /var/log/apache2/access_log
pos_file /var/log/apache2/access_log.pos
tag mytail
</source>
5、部分引數解釋
format:配置運算式,去過濾資料,只有滿足format運算式的字串才能在match中進行store存盤,
type tail: tail方式是 Fluentd 內置的輸入方式,其原理是不停地從源檔案中獲取增量日志,與linx命令tail相似,也可以使用其他輸入方式如http、forward等輸入,也可以使用輸入插件,將 tail 改為相應的插件名稱 如: type tail_ex ,注意tail_ex為下劃線,
format apache: 指定使用 Fluentd 內置的 Apache 日志決議器,可以自己配置運算式,
path /var/log/apache2/access_log: 指定收集日志檔案位置,
Pos_file /var/log/apache2/access_log.pos:強烈建議使用此引數,access_log.pos檔案可以自動生成,要注意access_log.pos檔案的寫入權限,因為要將access_log上次的讀取長度寫入到該檔案,主要保證在fluentd服務宕機重啟后能夠繼續收集,避免日志資料收集丟失,保證資料收集的完整性,
6、組態檔案例
6.1、通過http的方式,同時往日志和Kafka傳輸資料
# http://this.host:8888/mytail?json={"event":"data"}
<source>
@type http
port 8081
</source>
<match mytail>
@type copy
<store>
@type kafka
brokers localhost:9092
default_topic test1
default_message_key message
ack_timeout 2000
flush_interval 1
required_acks -1
</store>
<store>
@type file
path /var/log/td-agent/access
</store>
</match>
6.2 通過讀取檔案檔案,同時往日志和Kafka傳輸資料
<source>
type tail
format none
path /var/log/apache2/access_log
pos_file /var/log/apache2/access_log.pos
tag mytail
</source>
<match mytail>
@type copy
<store>
@type kafka
brokers localhost:9092
default_topic test1
default_message_key message
ack_timeout 2000
flush_interval 1
required_acks -1
</store>
<store>
@type file
path /var/log/td-agent/access
</store>
</match>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/420021.html
標籤:其他
