LoginSignup
16
9

More than 3 years have passed since last update.

td-agent3(Fluentd v1.0)でS3にログを保存する。2018-03-01

Last updated at Posted at 2018-03-02

内容

Amazon S3 Output Plugin を参考に、td-agent3Fluentd v1.0)で S3 にログを保存する設定をします。

準備

fluent-plugin-s3 のインストール

追記:
個別にインストールしなくても td-agent に含まれているようでした。
このインストールの手順を踏むことで最新のものが利用できる状態にはなります

  • fluent-plugin-s3 というプラグインが必要なのでインストールします。
  • インストールで使うコマンドは /opt/td-agent/embedded/bin/fluent-gem にあります

インストール

$ sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-s3
Fetching: fluent-plugin-s3-1.1.1.gem (100%)
Successfully installed fluent-plugin-s3-1.1.1
Parsing documentation for fluent-plugin-s3-1.1.1
Installing ri documentation for fluent-plugin-s3-1.1.1
Done installing documentation for fluent-plugin-s3 after 0 seconds
1 gem installed

確認

$ /opt/td-agent/embedded/bin/fluent-gem list fluent-plugin-s3

*** LOCAL GEMS ***

fluent-plugin-s3 (1.1.1, 1.1.0

設定の例

ApacheのアクセスログをS3に送る例です。

/etc/td-agent/td-agent.conf
# <source> についての詳細は割愛
<source>
  @type tail
  path /var/log/apache2/access.log
  tag apache.access
  pos_file /var/log/td-agent/apache-access-log.pos
  format apache2
</source>

# <match> の内容が本題
<match *.**>
  @type s3

  aws_key_id YOUR_AWS_KEY_ID
  aws_sec_key YOUR_AWS_SECRET_KEY
  s3_bucket YOUR_S3_BUCKET_NAME
  s3_region us-east-1
  path logs/${tag}/%Y-%m-%d/
  s3_object_key_format %{path}%{time_slice}_%{index}.%{file_extension}

  # if you want to use ${tag} or %Y/%m/%d/ like syntax in path / s3_object_key_format,
  # need to specify tag for ${tag} and time for %Y/%m/%d in <buffer> argument.

  <buffer tag,time>
    @type file
    path /var/log/td-agent/s3
    timekey 3600
    timekey_wait 10m
    chunk_limit_size 256m
  </buffer>
</match>

動作確認

以上の設定例で td-agent を起動してログ(/var/log/td-agent/td-agent.log)を確認します。
特に問題がなさそうであれば

$ while true; do curl localhost > /dev/null && sleep 30; done

などでアクセスログを出力して、S3にログがたまっていくか確認します。

S3の状況を確認

  • (検証に使っていたサーバのTZはUTCです)
  • (時間は5分くらい遅れてました :cry:

YOUR_S3_BUCKET_NAME/logs/apache.access/2018-03-02 の下に以下のようにgzなログが送られてきたのを確認できました。

スクリーンショット 2018-03-02 14.21.34.png

ログの内容は以下の通りです。

2018-03-02T03:00:12+00:00   apache.access   {"host":"127.0.0.1","user":null,"method":"GET","path":"/","code":200,"size":11576,"referer":null,"agent":"curl/7.47.0"}
2018-03-02T03:00:42+00:00   apache.access   {"host":"127.0.0.1","user":null,"method":"GET","path":"/","code":200,"size":11576,"referer":null,"agent":"curl/7.47.0"}
2018-03-02T03:01:12+00:00   apache.access   {"host":"127.0.0.1","user":null,"method":"GET","path":"/","code":200,"size":11576,"referer":null,"agent":"curl/7.47.0"}
...

まとめ

td-agent3Fluentd v1.0)で S3 にログを保存できました。

その他

エラーが出た

[error]: #0 Permission denied @ rb_sysopen - /var/log/apache2/access.log

のようなエラーが出るときはchmodします。

$ sudo chmod -R 755 /var/log/apache2/

EC2 IAMロール + td-agent

「EC2インスタンスで td-agent を動かしてS3にログを送る & そのインスタンにはS3に書き込める権限を持つIAMロール付き」

以上のような状況の場合はIAMロールがよしなにやってくれるので、以下の認証の設定が不要です。

  aws_key_id YOUR_AWS_KEY_ID
  aws_sec_key YOUR_AWS_SECRET_KEY

参考: Buffer section configurations

<buffer tag,time> の設定を変える

以下の設定にするとS3にログを送る間隔が短くなります

のところだけ抜粋
...
  <buffer tag,time>
    @type file
    path /var/log/td-agent/s3
    # timekey 3600
    # timekey_wait 10m
    timekey 60
    timekey_wait 60
    chunk_limit_size 256m
  </buffer
...

スクリーンショット 2018-03-02 14.30.30.png

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