LoginSignup
2
0

More than 1 year has passed since last update.

fluentdクイックツアー

Last updated at Posted at 2018-03-08

概要

fluentdをインストールして動作確認する。

社内セキュリティに幽閉されてる人はproxy設定をする。

手順

(1) proxyの設定(curlを使うため)

$ tail ~/.bashrc
PROXY=http://ユーザ名:パスワード@IPアドレス:ポート/
export http_proxy=$PROXY
export http_proxy=$PROXY

(2) インストール(centos)

$ curl -L http://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh

※Amazon Linuxの場合は少し違う。以下参考
https://docs.fluentd.org/v1.0/articles/install-by-rpm#step-0:-before-installation

(3) 設定

設定ファイルのデフォルトは/etc/td-agent/td-agent.confです。
今回はローカルで設定ファイルを作成して、-cで指定して実行してみます。

$ vi  td-agent.conf
$ cat td-agent.conf
<source>
  type tail
  # [path] 監視対象となるログファイル
  path /tmp/hoge_input.log
  # [format] 上記の出力で想定する書式. (?<name>pattern) の記述はRubyの「名前付きキャプチャ」。
  # 以下のように'time'を指定すると、出力側でその日付で出力される。
  format /^(?<time>\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d) (?<level>\w+): (?<message>.*)$/
  # [tag]タグ。<match>のところで使用する。
  tag hoge.01
  # [pos_file] 監視対象のログファイルを見てる位置。予めディレクトリ作成する必要あり。
  pos_file /tmp/hoge.pos
</source>

<match hoge.**>
  type file
  path /tmp/hoge_output.log
</match>

(4) 実行

$ td-agent -vv -c ./td-agent.conf

別窓で/tmp/hoge_input.logにログを出力する風に文字列を出力してみます。

$ echo "2018-11-11 11:11:11 error: どうやらエラーです。" >> /tmp/hoge_input.log

出力を確認してみます。

$ cat /tmp/hoge_output*
2018-11-11T11:11:11+00:00       hoge.01 {"level":"error","message":"どうやらエラーです"}

/tmo/hoge_output側に出力されました。
「2018-11-11T11:11:11+00:00」は名前付きキャプチャの"time"で取得した値です。
なので、メッセージのほうには"time"は(冗長なので?)省略されているっぽいです。

(4) メモ

  • ログの形式がjsonとか一般的な形式ならば「format json」と書けばよい。
  • time_formatは例えばログが「{"time":"2018--11--11 11:11:11","message":"何とかエラーです。"」 といった場合は「time_format "%Y--%m--%d %H:%M%S"」と指定する。そうするとその通りに解析してくれる。
  • timeの文字列がtime_formatにちゃんとマッチしていないと出力側のログにでる時間が 「1970-01-01T00:33:38+00:00」とかになってしまう。
  • time_formatは「format json」以外にも例のように自ら指定したformatでも"time"キーがあれば有効となる。
  • time_formatに使える値は
    https://docs.ruby-lang.org/en/2.4.0/Time.html#method-c-strptime
    を確認。

  • 出力(matchのpath)はflushされるまで以下のようなディレクトリ状態になる。

$ ll hoge_output.log
合計 8
-rw-r--r--. 1 vagrant vagrant 150 2018/03/07 06:25:05 buffer.q566cc9eb0270286108c3d767992f308a.log
-rw-r--r--. 1 vagrant vagrant  68 2018/03/07 06:25:05 buffer.q566cc9eb0270286108c3d767992f308a.log.meta

flush_intervalを短く設定すればすぐxxx.logに出力される。

<match hoge.**>
  type file
  path /tmp/hoge_output.log
  flush_interval 10s ★ここを設定
</match>
2
0
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
2
0