fluentd 总结(个人备忘录)

首先

因为接触过Fluentd,所以我打算将其作为备忘录记下来,以免遗忘。我会随时更新。

2. Fluentd是什么?

    • ログデータ収集管理ツール

 

    多数のIoT機器からデータを収集し、処理を加えてS3などに保存することができる

3. 配置fluentd之前的设定

安装

# td-agent 4
​​$ curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-bionic-td-agent4.sh | sh

版本验证

$ td-agent --version

确认设定文件

$ sudo vim /etc/td-agent/td-agent.conf

开始

$ sudo systemctl start td-agent

确认状态

$ sudo systemctl status td-agent

查看日志

$ curl -X POST -d 'json={"test":"hello fluentd"}' http://localhost:8888/debug.test
$ sudo vim /var/log/td-agent/td-agent.log 
or
$ sudo tail -n 1 /var/log/td-agent/td-agent.log

停止 – 停住不动

$ sudo systemctl stop td-agent

4. 插件

    • fluentdでは、8つのプラグインがある。

Input, Parser,Filter, Output, Formatter, Storage, Service Discovery, Buffer

「@type XXX」のXXXの部分だと思えば良い
以下で何個かピックアップする

(1). 输入插件

tail : ファイルの末尾を読む

<source>
  @type tail
  path /var/log/httpd-access.log
  pos_file /var/log/td-agent/httpd-access.log.pos
  tag apache.access
  <parse>
    @type apache2
  </parse>
</source>

forward : イベントストリームを受け取るためにTCPソケットをlistenする(他のfluentdからデータを受け付けるなど)

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>

http : HTTPリクエストを通してデータを受け取る

<source>
  @type http
  port 9880
  bind 0.0.0.0
  body_size_limit 32m
  keepalive_timeout 10s
</source>

sample : テストイベントを生成、test, debugに役立つ

syslog : UDP/TCPのsyslogプロトコル経由で取得する
その他 monitor_agent, exec, windows_eventlog

(2) 输出插件

■ 指定插件类型(@type)

file : ログをファイルに書き出す

<match pattern>
  @type file
  path /var/log/fluent/myapp
  compress gzip
  <buffer>
    timekey 1d
    timekey_use_utc true
    timekey_wait 10m
  </buffer>
</match>

forward: 他のfluentdに送信

<match pattern>
  @type forward
  send_timeout 60s
  recover_wait 10s
  hard_timeout 60s

  <server>
    name myserver1
    host 192.168.1.3
    port 24224
    weight 60
  </server>
  <server>
    name myserver2
    host 192.168.1.4
    port 24224
    weight 60
  </server>
  ...

  <secondary>
    @type file
    path /var/log/fluent/forward-failed
  </secondary>
</match>

**secondary: 在output插件内可以进行编写,并且当重试且无法输出时,进行相应的处理编写。
*例如,由于无法连接到服务器,可以将其写入本地文件等等。

    http
<match pattern>
  @type http

  endpoint http://logserver.com:9000/api
  open_timeout 2

  <format>
    @type json
  </format>
  <buffer>
    flush_interval 10s  #ざっくりとした送信間隔だと思って良い(実際は違うが)
  </buffer>
</match>

* 在Endpoint上进行HTTP Post请求之处

copy : 1つのソースを複数に送信するのに使用

<match pattern>
  @type copy
  <store>
    @type file
    path /var/log/fluent/myapp1
    ...
  </store>
  <store>
    ...
  </store>
  <store>
    ...
  </store>
</match>

*因为如果写两个match,只会执行先写的处理,所以要使用copy。
*关于处理,要在 中进行描述。
*对于fluentd,如果有先匹配的内容,后面的match语句将不起作用。

    • stdout:標準出力にログを吐き出す。デバックの処理に役立つ。

 

    • s3 : 出力をAWSのS3に指定できる

kafka, exec, exec_filter, secondary_file, relabel, mongo

(3). 过滤器插件

record_transformer : ログを加工できる。JSONにデータ追加できる。

<filter foo.bar>
  @type record_transformer
  <record>
    hostname "#{Socket.gethostname}"
    tag ${tag}
  </record>
</filter>

在record指令的内部,写下字段名和其对应的值。

    その他 : parser,grep

(4). 解析器插件

    • regexp

 

    • apache2

 

    • apache_error

 

    • nginx

 

    • syslog

 

    • json

 

    none

(5)缓冲器插件

    • Bufferディレクティブは、Matchの中に記述する

 

    memory : バッファをメモリ上に保存。流量も流速も多い時に使用
<match pattern>
  <buffer>
    @type memory
  </buffer>
</match>

file: バッファとしてファイルを使用。ログ欠損しないように

<match pattern1>
  <buffer>
    @type file
    path /var/log/fluent/foo.bar
  </buffer>
</match>
    file_single

4.指示

(1) 源頭指令

    • Inputの定義

 

    • イベントの受信方法を指定

 

    • 各source要素は、@typeパラメータでinputプラグインを指定

 

    • 各sourceプラグインはfluentdのルーティングエンジンにイベントを投げている

 

    複数の入力プラグインを指定できる

◆例① 如果从本地主机的TCP端口号8888收到HTTP请求,则进行接收。

<source>
    @type http
    port 8888
    bind 0.0.0.0 #listenしているアドレス(bind 0.0.0.0 :すべてのアドレスから受け取る)
</source>

将标签添加到通过8888端口收到的HTTP请求数据。

<source>
    @type http
    port 8888
    bind 0.0.0.0
    tag test.error
</source>

◆例③ “读取文件的末尾”

<source>
  @type tail
  <parse>
    @type none
  </parse>
  path /var/log/httpd/access_log
  pos_file /var/log/td-agent/tmp/access.log.pos
</source>

(2). 转化指令

    • Outputの定義

 

    • 受信したイベントをどのように処理するかを記述

 

    • typeパラメータにoutputプラグインを設定

 

    インプットプラグインが出してきたJSONのデータをフィルタリングしたり、特定のキーの値を加工したり、別のファイルや標準出力へ出力したりする

◆Option 1: 「将日志打印到标准输出」

<match 条件式(tagの名前とか)>
    @type stdout 
</match>

(3). 过滤器指令

    • source要素とmatch要素の間に書く

 

    • matchの後だと適用されない

 

    イベント処理をどのようにするか

(4)系统指令

在这个指令中,决定了fluentd核心部分的运行方式
– 日志级别
– 防止重复堆栈跟踪
– 发送错误日志的间隔
– 防止配置转储

to end/finally

我总结了一下笔记。
由于我认为使用fluentd的机会很多,所以我希望能熟练掌握它。

文献引用

    • Fluentdのバッファリングで抑えておくべき大事なポイント

 

    fluentd公式ページ