LoginSignup
10
14

More than 5 years have passed since last update.

td-agentをAmazon Linuxに導入する

Last updated at Posted at 2016-10-26

ただの手順書です

事前準備

インストールするのに事前条件があるので確認し、満たしていないなら対応します。

まず、ファイルディスクプタの最大値を調べます。

$ ulimit -n
1024

65535という条件を満たしていないので、以下で設定し、再起動します。

sudo vi /etc/security/limits.conf

以下を末尾に追加
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536

なお再起動したくない場合は、rootになってから ulimit -n 65536 を行うと値がかえれますが、再起動後も反映されていることを確かめるために再起動をおすすめします。

次に、ネットワーク設定を行います。

sudo vi /etc/sysctl.conf

以下を末尾に追加
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240    65535

その後、以下で反映させます。
sudo sysctl -p

td-agentのインストール

Installing Fluentd Using rpm Packageを参考にしてインストールします。

以下を実行します。
curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh

インストール後起動します
sudo /etc/init.d/td-agent start

サーバリブートしても自動で起動するようにします
sudo chkconfig td-agent on

BigQueryに接続できるようにする

ユーザの行動ログをBigQueryで分析!RubyからFluentdを通して、BigQueryへ任意のログをJSON形式で保存するを参考に設定する

まず必要なプラグインをインストールします

sudo td-agent-gem install fluent-plugin-bigquery
sudo td-agent-gem install fluent-plugin-forest

設定ファイルを変更する

# バックアップする
sudo cp /etc/td-agent/td-agent.conf /etc/td-agent/td-agent.conf.bk

# 編集する
sudo vi /etc/td-agent/td-agent.conf 

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

<filter action.**>
  @type record_transformer
  <record>
    tag ${tag}
  </record>
</filter>

<match action.**>
  @type copy
  <store>
    @type forest
    subtype bigquery
    <template>
      method insert

      auth_method json_key
      json_key xxx.json

      buffer_type file
      buffer_path /var/log/td-agent/buffer/${escaped_tag}

      project xxx
      dataset xxx
      table ${escaped_tag}_%Y%m%d
      auto_create_table true
      convert_hash_to_json true

      time_format %s
      time_field time
      schema_path /etc/td-agent/action_schema.json
    </template>
  </store>
  <store>
  @type stdout
  </store>
</match>

スキーマの作成

sudo vi /etc/td-agent/action_schema.json

[
  {
    "name": "tag",
    "type": "STRING"
  },
  {
    "name": "time",
    "type": "INTEGER"
  },
  {
    "name": "message",
    "type": "STRING"
  }
]

td-agentを再起動する

sudo /etc/init.d/td-agent restart

試しに、ログをなげてみます。BigQueryに入っていれば完了です。

echo '{"message": {"key": "value"}}' | /opt/td-agent/embedded/bin/fluent-cat action.new_log

最後に

今回の設定はバッファが溢れてデータロスとか考えていないので、必要な場合は、S3などにバックアップしてください

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