Fluentd入門:インストールから設定ファイルの基本まで

スポンサーリンク

Fluentd はログの収集・変換・転送を行うオープンソースのデータコレクターです。アプリケーションのログファイルを読み取り、フィルタリングや整形を加えて、ファイル・データベース・クラウドサービスなどに転送できます。

Fluentdの基本的な仕組み

[ログソース] → source → filter → match → [転送先]
ブロック 役割
source データの入力元(ファイル・HTTP・syslog など)
filter データの変換・加工・フィルタリング
match データの出力先(ファイル・転送・stdout など)

インストール

macOS(Homebrew)

brew install fluent/tap/td-agent

または公式の fluentd gem を使う場合:

gem install fluentd
fluentd --setup ./fluent

Ubuntu / Debian

curl -fsSL https://toolbelt.treasuredata.com/sh/install-ubuntu-noble-fluent-package5-lts.sh | sh

起動確認

fluentd --version

設定ファイルの基本構造

設定ファイルは /etc/fluent/fluentd.conf(td-agentの場合は /etc/td-agent/td-agent.conf)に置きます。

<source>
  @type  入力プラグイン名
  tag    タグ名
</source>

<filter タグパターン>
  @type  フィルタプラグイン名
</filter>

<match タグパターン>
  @type  出力プラグイン名
</match>

source:入力プラグイン

tail(ファイルの末尾を監視)

<source>
  @type tail
  path /var/log/myapp/app.log
  pos_file /var/log/fluentd/myapp.log.pos
  tag myapp.log
  <parse>
    @type none
  </parse>
</source>
設定 説明
path 監視するファイルパス(* でワイルドカード指定可)
pos_file 読み込み位置を記録するファイル(再起動後も継続読み込み)
tag このソースに付けるタグ

parse の種類

# パースしない
<parse>
  @type none
</parse>

# JSON ログ
<parse>
  @type json
</parse>

# Apache / Nginx のアクセスログ
<parse>
  @type apache2
</parse>

# 正規表現でパース
<parse>
  @type regexp
  expression /^(?<time>\d{4}-\d{2}-\d{2}) (?<level>\w+) (?<message>.+)$/
  time_key time
  time_format %Y-%m-%d
</parse>

http(HTTPでデータを受け取る)

<source>
  @type http
  port 9880
  bind 0.0.0.0
  tag http.input
</source>

起動後に以下でデータを送信できます。

curl -X POST -d '{"message":"hello"}'   -H "Content-Type: application/json"   http://localhost:9880/http.input

forward(別のFluentdからデータを受け取る)

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

filter:データの変換・加工

record_transformer(フィールドを追加・変更)

<filter myapp.**>
  @type record_transformer
  <record>
    hostname "#{Socket.gethostname}"
    environment production
  </record>
</filter>

grep(条件でレコードを絞り込む)

<filter myapp.**>
  @type grep
  <regexp>
    key level
    pattern /ERROR|WARN/
  </regexp>
</filter>

逆に特定パターンを除外する場合:

<filter myapp.**>
  @type grep
  <exclude>
    key message
    pattern /healthcheck/
  </exclude>
</filter>

parser(文字列フィールドをパース)

<filter myapp.**>
  @type parser
  key_name message
  <parse>
    @type json
  </parse>
</filter>

match:出力プラグイン

stdout(標準出力に表示・デバッグ用)

<match myapp.**>
  @type stdout
</match>

file(ファイルに書き出す)

<match myapp.**>
  @type file
  path /var/log/fluentd/myapp.%Y%m%d
  append true
  <format>
    @type json
  </format>
</match>

forward(別のFluentdに転送)

<match myapp.**>
  @type forward
  <server>
    host 192.168.1.100
    port 24224
  </server>
  <buffer>
    flush_interval 5s
  </buffer>
</match>

null(捨てる)

<match debug.**>
  @type null
</match>

タグのパターンマッチ

パターン マッチする例
myapp.log myapp.log のみ
myapp.* myapp.accessmyapp.error(1階層)
myapp.** myapp.web.access など(複数階層)
** すべてのタグ

実用的な設定例

ファイルログを監視してERROR/WARNだけ別ファイルに保存する例です。

<source>
  @type tail
  path /var/log/myapp/app.log
  pos_file /var/log/fluentd/myapp.pos
  tag myapp.app
  <parse>
    @type regexp
    expression /^\[(?<time>[^\]]+)\] (?<level>\w+) (?<message>.+)$/
    time_key time
    time_format %Y-%m-%d %H:%M:%S
  </parse>
</source>

<filter myapp.**>
  @type record_transformer
  <record>
    hostname "#{Socket.gethostname}"
  </record>
</filter>

<filter myapp.**>
  @type grep
  <regexp>
    key level
    pattern /ERROR|WARN/
  </regexp>
</filter>

<match myapp.**>
  @type file
  path /var/log/fluentd/myapp-errors.%Y%m%d
  append true
  <format>
    @type json
  </format>
</match>

動作確認・デバッグのコツ

設定ファイルの構文チェック

fluentd --dry-run -c /etc/fluent/fluentd.conf

フォアグラウンドで起動してログを確認

fluentd -c /etc/fluent/fluentd.conf -v

HTTPソースで手動テスト

curl -X POST -d '{"level":"ERROR","message":"test error"}'   -H "Content-Type: application/json"   http://localhost:9880/test.input

まとめ

要素 役割 よく使うプラグイン
source 入力 tailhttpforward
filter 変換・絞り込み record_transformergrepparser
match 出力 filestdoutforwardnull
  • sourcefiltermatch の流れでデータが動く
  • タグで sourcefiltermatch を紐付ける
  • ** で複数階層のタグをまとめてマッチできる
  • デバッグ時は @type stdoutfluentd -v が便利

curlを使ってHTTPソースにデータを送る方法は「curl入門:GET・POST・PUT・認証・クッキーの使い方まとめ」を参照してください。