LoginSignup
15
10

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-03-07

内容

https://github.com/kaizenplatform/fluent-plugin-bigquery を参考に、td-agent3Fluentd v1.0)で BigQuery にログを保存する設定をします。

準備

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

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

インストール

$ sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-bigquery
... 表示が多いので詳細は割愛
Done installing documentation for uber, declarative, declarative-option, representable, retriable, mime-types-data, mime-types, little-plugger, logging, jwt, memoist, os, signet, googleauth, google-api-client, fluent-plugin-bigquery after 159 seconds
16 gems installed

ちょっと時間がかかります

確認

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

*** LOCAL GEMS ***

fluent-plugin-bigquery (1.2.0)

fluent-plugin-bigquery@v2.0.0 のインストール(2018-03-07時点)

  • @type bigquery_insert
  • @type bigquery_load

を利用したいのですが、使えるのはv2.0.0からのようです。
上記の手順でインストールすると、v1.2.0 なので一手間かけてv2.0.0をインストールします。

追記

https://rubygems.org/gems/fluent-plugin-bigquery/versions/2.0.0.beta
gemに2.0.0.betaがあります。こちらをインストールすれば以下のGitHubからのインストールの必要はありません。

GitHubからインストールする準備

$ sudo /opt/td-agent/embedded/bin/fluent-gem install --no-document specific_install
Fetching: specific_install-0.3.3.gem (100%)
Successfully installed specific_install-0.3.3
1 gem installe

GitHubから最新の fluent-plugin-bigquery をインストール

$ sudo /opt/td-agent/embedded/bin/fluent-gem specific_install https://github.com/kaizenplatform/fluent-plugin-bigquery.git
/usr/bin/git
git installing from https://github.com/kaizenplatform/fluent-plugin-bigquery.git
Cloning into '/tmp/d20180307-1588-1vvfhc6'...
remote: Counting objects: 2133, done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 2133 (delta 60), reused 94 (delta 39), pack-reused 2008
Receiving objects: 100% (2133/2133), 461.14 KiB | 0 bytes/s, done.
Resolving deltas: 100% (799/799), done.
Checking connectivity... done.
WARNING:  open-ended dependency on rake (>= 0, development) is not recommended
  if rake is semantically versioned, use:
    add_development_dependency 'rake', '~> 0'
WARNING:  open-ended dependency on rr (>= 0, development) is not recommended
  if rr is semantically versioned, use:
    add_development_dependency 'rr', '~> 0'
WARNING:  open-ended dependency on test-unit (>= 0, development) is not recommended
  if test-unit is semantically versioned, use:
    add_development_dependency 'test-unit', '~> 0'
WARNING:  open-ended dependency on test-unit-rr (>= 0, development) is not recommended
  if test-unit-rr is semantically versioned, use:
    add_development_dependency 'test-unit-rr', '~> 0'
WARNING:  open-ended dependency on google-api-client (>= 0.11.0) is not recommended
  if google-api-client is semantically versioned, use:
    add_runtime_dependency 'google-api-client', '~> 0.11', '>= 0.11.0'
WARNING:  open-ended dependency on googleauth (>= 0.5.0) is not recommended
  if googleauth is semantically versioned, use:
    add_runtime_dependency 'googleauth', '~> 0.5', '>= 0.5.0'
WARNING:  open-ended dependency on multi_json (>= 0) is not recommended
  if multi_json is semantically versioned, use:
    add_runtime_dependency 'multi_json', '~> 0'
WARNING:  See http://guides.rubygems.org/specification-reference/ for help
  Successfully built RubyGem
  Name: fluent-plugin-bigquery
  Version: 2.0.0.beta
  File: fluent-plugin-bigquery-2.0.0.beta.gem
Successfully installe

確認

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

*** LOCAL GEMS ***

fluent-plugin-bigquery (2.0.0.beta, 1.2.0)

設定の例(bigquery_insert)

  • ApacheのアクセスログをBigQueryに送る例です
  • auth_method には json_key を使います
/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
  keep_time_key true
</source>

# <match> の内容が本題
<match apache.access>
  @type bigquery_insert

  auth_method json_key
  json_key /tmp/json_key.json

  project PROJECT_NAME
  dataset DATASET_NAME

  auto_create_table true
  table apache_access_%Y%m%d

  <buffer time>
    @type file
    path /var/log/td-agent/bigquery
    timekey 1d
  </buffer>

  <inject>
    time_key time
    time_type string
    time_format %Y-%m-%d %H:%M:%S
  </inject>

  schema [
    {"name": "time", "type": "TIMESTAMP"},
    {"name": "host", "type": "STRING"},
    {"name": "user", "type": "STRING"},
    {"name": "method", "type": "STRING"},
    {"name": "path", "type": "STRING"},
    {"name": "code", "type": "INTEGER"},
    {"name": "size", "type": "INTEGER"},
    {"name": "referer", "type": "STRING"},
    {"name": "agent", "type": "STRING"}
  ]
</match>

動作確認

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

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

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

BigQueryの状況を確認

  • 日付付きのテーブルが自動で作成され、そこにinsertされました
    • スクリーンショット 2018-03-07 12.11.16.png
  • BigQueryにたまったログ
#standardSQL
SELECT
  *
FROM
  DATASET_NAME.apache_access_20180307
ORDER BY
  time DESC

スクリーンショット 2018-03-07 12.10.05.png

設定の例(bigquery_load)

/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
  keep_time_key true
</source>

# <match> の内容が本題
<match apache.access>
  @type bigquery_load

  auth_method json_key
  json_key /tmp/json_key.json

  project PROJECT_NAME
  dataset DATASET_NAME

  auto_create_table true
  table apache_access_%Y%m%d

  <buffer time>
    @type file
    path /var/log/td-agent/bigquery
    timekey 1d

    flush_at_shutdown true
    flush_interval 25
  </buffer>

  <inject>
    time_key time
    time_type string
    time_format %Y-%m-%d %H:%M:%S
  </inject>

  schema [
    {"name": "time", "type": "TIMESTAMP"},
    {"name": "host", "type": "STRING"},
    {"name": "user", "type": "STRING"},
    {"name": "method", "type": "STRING"},
    {"name": "path", "type": "STRING"},
    {"name": "code", "type": "INTEGER"},
    {"name": "size", "type": "INTEGER"},
    {"name": "referer", "type": "STRING"},
    {"name": "agent", "type": "STRING"}
  ]
</match>

動作確認

bigquery_insert のときと同様

BigQueryの状況を確認

  • 基本的に bigquery_insert のときと変わらないので詳細は割愛
  • flush_XXX の間隔である程度たまってから送られていた

まとめ

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

その他

エラーが出た

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

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

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

エラーが出た2

fluent-plugin-bigquery もエラーを出していましたが、beta版なので気にしないことにしました
エラーは出ましたが、それっぽく動いていました

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