LoginSignup
6
5

More than 1 year has passed since last update.

Fluentdの設定調査

Last updated at Posted at 2019-11-16

メモ

@が必要なシステムパラメータ

@type@id@label@log_levelはプラグイン本体の設定ではなくて
Fluentdコアの設定なので@をつける。

format指定

format による指定は古い設定方法。
v1.0からparse プラグインとして 内で指定することが推奨されている。

例)
古い設定方法

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

推奨されている設定方法

<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>

type_name

type_nameは「_doc」固定

type_name _doc

Fluent-plugin-elasticsearch v2.8.0対応でわかったElasticsearch 6.0以降の動向
Elasticsearch, Kibanaを6.7.1から7.1.1にupgradeした

format none

ログの行をそのまま "message" key の値として取り込む場合に指定する

ホスト名を付与

詳細は、Fluentdのレコードにホスト名を付与する最良の方法

<filter *.*>
  @type record_transformer
  <record>
    host ${hostname}
  </record>
</filter>

入力データの特定フィールドの値を変更

record_transformer
fluentd の filter_record_transformer プラグイン便利っぽい
Fluentd (td-agent) でレコードの値を書き換える方法
fluentdで送信recordに文字列を代入してから送信する
fluentdのrecord_transformerでログを加工する
続・fluentdでつくる監視系

Fluentdのレコードに環境変数を付与

例) 環境変数名「COMPUTERNAME」

<filter *.*>
  @type record_transformer
  <record>
    computer_name "#{ENV['COMPUTERNAME']}"
  </record>
</filter>

Kubernetesの場合、マニュフェストファイルのenvに以下のように
記載すると、Node名、Pod名、PodのIP、Podのサービスアカウントが
環境変数として使用できます。

      env:
        - name: MY_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: MY_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName

複数行ログ

in_tailのformatオプションにmultilineを指定することで複数行モードになる
今まで外部プラグイン(fluent-plugin-tail-multiline)として提供されていた機能が、
Fluentd v0.10.45でマージされている。

Fluentdのin_tailプラグインで複数行のログをよむ方法
複数行のログをキーワード監視&通知することで運用を少し改善しよう

ログファイルの中に複数のフォーマットがある時に利用可能なパーサプラグイン

fluent-plugin-multi-format-parser

uwsgi(ユーウィズギー)

<source>
  @type tail
  path /var/log/foo/bar.log
  pos_file /var/log/td-agent/foo-bar.log.pos
  tag foo.bar
  format /^\[(?<meta>[^\]]*)\] (?<host>[^ ]*) \(\) {[^}]*} \[(?<time>[^\]]*)\] (?<method>\S+) (?<path>[^ ]+) => generated (?<bytes>[^ ]*) bytes in (?<response_time>[^ ]*) msecs \(HTTP\/*.* (?<code>[^ ]*)\) .*$/
</source>

Fluentularで確認

uwsgi.PNG

Mroonga(むるんが)

Mroongaログ

Groonga ログ

ログフォーマット

#{TIME_STAMP}|#{L}| #{MESSAGE}
<source>
  @type tail
  path /var/log/foo/bar.log
  pos_file /var/log/td-agent/foo-bar.log.pos
  tag foo.bar
  format /^(?<timestamp>[^\|]+)\|(?<log-level>[^\|]*)\|(?<pid>[^\|]*)\|(?<message>[^\|]*)?$/
</source>

Mroongaクエリーログ

Groonga ログ

ログフォーマット

#{TIME_STAMP}|#{MESSAGE}
#{TIME_STAMP}|#{ID}|>#{QUERY}
#{TIME_STAMP}|#{ID}|:#{ELAPSED_TIME} #{PROGRESS}
#{TIME_STAMP}|#{ID}|<#{ELAPSED_TIME} #{RETURN_CODE}

TestString

2011-07-05 06:25:19.458752|test message
2011-07-05 06:25:19.458756|45ea3034|>select Properties --limit 0
2011-07-05 06:25:19.458829|45ea3034|:000000000072779 select(19)
2011-07-05 06:25:19.458875|45ea3034|<000000000119062 rc=0

まだ動作未確認

<parse>
  @type multiline
  format_firstline /(?<date>[^\|]+)\|(?<message>[^\|]*)?/
  format1 /(?<date>[^\|]+)\|(?<process-id>[^\|]*)\|\>(?<query>[^\|]*)?/
  format2 /(?<date>[^\|]+)\|(?<process-id>[^\|]*)\|\:(?<elapsed-time>[^\|]*)\s(?<progress>[^\|]*)?/
  format3 /(?<date>[^\|]+)\|(?<process-id>[^\|]*)\|\<(?<elapsed-time>[^\|]*)\src\=(?<retrun-code>[^ ]*)?/
</parse>

Maxscale

Regular Expression

(?<date>[^]]+)\s\s\s(?<log-level>[^\]]*):\s(?<message>[^\|]*)?

Test String

2018-12-17 23:20:00   warning: Protocol module 'mysqlbackend' has been deprecated, use 'mariadbbackend' instead.

Configuration・・・Fluentularの出力結果

<source>
  type tail
  path /var/log/foo/bar.log
  pos_file /var/log/td-agent/foo-bar.log.pos
  tag foo.bar
  format /(?<date>[^]]+)\s\s\s(?<log-level>[^\]]*):\s(?<message>[^\|]*)?/
</source>

JSON文字列

Regular Expression

\{\s[^ ]*\s(?<UserId>[^ ]*),\s[^ ]*\s(?<UserToken>[^ ]*),\s[^ ]*\s(?<SecurityCode>[^ ]*),\s[^ ]*\s(?<Password>[^ ]*),\s[^ ]*\s(?<UserPhone>[^ ]*),\s[^ ]*\s(?<drop>[^ ]*)\s[^ ]*\s(?<UserLastName>[^ ]*)\s\}

TestString

{ "UserId": "3", "UserToken": "4578965235874158", "SecurityCode": "111", "Password": "12345", "UserPhone": "555-555-1531", "drop": "-1", "UserLastName": "Smith" }

Configuration・・・Fluentularの出力結果

<source>
  type tail
  path /var/log/foo/bar.log
  pos_file /var/log/td-agent/foo-bar.log.pos
  tag foo.bar
  format /\{\s[^ ]*\s(?<UserId>[^ ]*),\s[^ ]*\s(?<UserToken>[^ ]*),\s[^ ]*\s(?<SecurityCode>[^ ]*),\s[^ ]*\s(?<Password>[^ ]*),\s[^ ]*\s(?<UserPhone>[^ ]*),\s[^ ]*\s(?<drop>[^ ]*)\s[^ ]*\s(?<UserLastName>[^ ]*)\s\}/
</source>

参考URL

Fluentdで複数行のログを読む

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5