designetwork

ネットワークを軸としたIT技術メモ

Beats(Filebeat)のログをFluentdで受け取りtagルーティングする

Beatsはバッファ・再送機能(確認応答)を持つ軽量なログシッパーで、ログの発生元となるサーバーにインストールすることにより、Elasticsearchでのログ解析を容易にする。

普段はFluentd(td-agent)をメインで使用しているのだが、ログ発生元のサーバにtd-agentをインストールするのは、依存パッケージ等の問題で面倒だと感じていた。Beatsの一つであるFilebeatなら、単一パッケージで簡単に導入でき、メリットが大きい。

ただし、Beats(Filebeat)を導入しても既存のFluentdでのログパース構成は崩したくないため、BeatsのログをFluentdのtagルーティングに取り込むための設定を検証した。

Filebeatログ送信設定

Fluentdでのtagルーティングを想定して、filebeatでは以下の通りfieldsを設定する。

$ sudo vi /etc/filebeat/filebeat.yml
filebeat.prospectors:
- input_type: log
  paths: ["/var/log/messages"]
#  symlinks: true
  fields:
    tagtype: linux
    tagapps: syslog
    taghost: centos7-m1
output.logstash:
  hosts: ["localhost:5044"]
#logging.level: debug
logging.metrics.enabled: false

fieldsの使用用途はFilter用などのオプショナルなものとして定義されているため、今回のようなケースに適していると考えられる。

www.elastic.co

fields内の項目名は自由に定義できる。私はtag用途なのでtagxxxxとして、他との混在を防ぎ明確化しておく。

宛先はとりあえず同一サーバ内のFluentd。

オプション設定

symlinks: true シンボリックリンクの場合に有効化
logging.level: debug デバッグ時に有効化(デフォルトinfo)
logging.metrics.enabled: false No non-zero metricsのログ防止

FluentdでのBeatsログ受信

Fluentd(td-agent)でのBeats(Filebeat)ログの受け取りにはfluent-plugin-beatsを使用する。

qiita.com

また、ログパース・保存のために以下の通りプラグインをインストールする。

sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-beats --no-document
sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-forest --no-document
sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-elasticsearch --no-document
sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-record-reformer --no-document

以下の設定でFilebeatのログを収集する。(td-agent.confからinclude)

$ vi /etc/td-agent/conf/td_beats.conf
### Log Collect from Beats
<source>
  @type beats
  tag "beats.collect"
</source>

### Reform for tag routing
<match beats.collect>
  @type record_reformer
  tag ${fields['tagtype']}.${fields['tagapps']}.${fields['taghost']}
</match>

### For Parse
<filter linux.syslog.*>
  @type parser
  format syslog
  key_name message
</filter>

### General match
<match *.*.*>
  type forest
  subtype copy
  <template>
    <store>
      @type elasticsearch
      host localhost
      port 9200
      logstash_format true
      logstash_prefix ${tag_parts[0]}.${tag_parts[1]}
      type_name ${tag_parts[0]}
      flush_interval 20
    </store>
    <store>
      @type file
      path /var/log/td-agent/${tag_parts[0]}/${tag_parts[1]}_${tag_parts[2]}.log
    </store>
  </template>
</match>

以下、設定解説

Log Collect from Beats

Beatsプラグインでログ受信する。デフォルトでポート5044が使用される。

Reform for tag routing

tagをfieldsの内容で書き換える。ここでBeatsのログとしての処理が終わり、新規tagで再度Fluentd内でルーティングされる。(matchディレクティブのため)

For Parse

受信したログをパースする。ここでは/var/log/messagesのログなのでシンプルにsyslogフォーマットで展開する。tagで汎用化・共通化して設定量を削減するのがポイント。

General match

パースが完了したログをElasticsearch, fileに送信・保存する。今回は同一ファイルで設定したが、別ファイルにしてincludeの最後で適用されるようにしておくのが良い。ファイルバッファ・チャンク設定等の最適化は未実装。

ログ受信結果

想定通りにログを受信・パース・保存できている。

$ tail /var/log/td-agent/linux/syslog_centos7-m1.log.20170709.b553d9a17802a264d
2017-07-09T11:57:37+09:00   linux.syslog.centos7-m1 {"host":"CentOS7-M1","ident":"systemd","message":"Stopping LSB: data collector for Treasure Data..."}
2017-07-09T11:57:38+09:00   linux.syslog.centos7-m1 {"host":"CentOS7-M1","ident":"td-agent","message":"Stopping td-agent: td-agent[  OK  ]"}
2017-07-09T11:57:38+09:00   linux.syslog.centos7-m1 {"host":"CentOS7-M1","ident":"systemd","message":"Starting LSB: data collector for Treasure Data..."}
2017-07-09T11:57:38+09:00   linux.syslog.centos7-m1 {"host":"CentOS7-M1","ident":"td-agent","message":"Starting td-agent: [  OK  ]#015td-agent[  OK  ]"}
2017-07-09T11:57:38+09:00   linux.syslog.centos7-m1 {"host":"CentOS7-M1","ident":"systemd","message":"Started LSB: data collector for Treasure Data."}

まとめ - Beats(Filebeat)のログをFluentdで受け取りtagルーティングする

Filebeatのfiledsの項目を利用することで、Fluentd内で使用するtagを設定し、通常のFluentdのログと同様にtagルーティングできるようにした。これにより、Filebeatでのログ送信時も従来のFluentdで集約・パース・保存 or Elasticsearch送信できる。