echo ${MY_OUTPUT} >>blog

AWSやその他勉強した内容をぶちまける

Apache→fluentd→DynamoDB でハマったこと

最近、fluentdに触る機会があったので、Apache→fluentd→DynamoDBを試したら
思いの外ハマったのでφ(..)メモメモ

環境

今回はEC2上のAmazonLinuxを使いました。
環境詳細は以下

- AMI         :Amazon Linux ami-cbf90ecb
- インスタンスタイプ :t2.micro
- セキュリティグループ:インバウンドSSH,HTTP許可、アウトバウンド全許可
- その他       :デフォルト

Apacheインストール〜起動

Apacheが入っていなかったので、インストールします。

$ sudo yum  -y install httpd

さらに、警告がウザいので以下のみ設定します。

$ cat /etc/httpd/conf/httpd.conf | grep ServerName
ServerName [ホスト名]:80

そして起動します。

$ sudo service httpd start
Starting httpd:                                            [  OK  ]

HTTPリクエストを送ってレスポンスが帰ってくることを確認します。

$ curl -I localhost
HTTP/1.1 403 Forbidden
Date: xxx, xx xxx xxx xx:xx:xx GMT
Server: Apache/2.2.29 (Amazon)
Accept-Ranges: bytes
Content-Length: 3839
Connection: close
Content-Type: text/html; charset=UTF-8



DynamoDBのテーブル作成

以下の設定以外はデフォルトでテーブルを作成します。

f:id:t_wkm2:20150808003003p:plain
f:id:t_wkm2:20150808003010p:plain

fluentdインストール

まずは以下の公式サイトの手順通りにfluentdをインストールします。

Installing Fluentd Using rpm Package | Fluentd

f:id:t_wkm2:20150806004431p:plain

☆ハマりポイント1 AmazonLinux サポート外


"Amazon Linux is NOT supported"

おお、、マジすか、、

気を取り直し、最新版のrpmのパスを調べてインストールしました。

$ sudo rpm -ivh http://packages.treasuredata.com.s3.amazonaws.com/2/redhat/6/x86_64/td-agent-2.2.1-0.el6.x86_64.rpm
http://packages.treasuredata.com.s3.amazonaws.com/2/redhat/6/x86_64/td-agent-2.2.1-0.el6.x86_64.rpm を取得中
警告: td-agent-2.2.1-0.el6.x86_64.rpm: ヘッダー V4 DSA/SHA1 Signature、鍵 ID a12e206f: NOKEY
準備しています... ################################# [100%]
更新中 / インストール中...
1:td-agent-2.2.1-0.el6 ################################# [100%]
adding 'td-agent' group...
adding 'td-agent' user...
Installing default conffile...
Configure td-agent to start, when booting up the OS...

fluent-plugin-dynamodbインストール

fluentd にはDynamoDB用のプラグインがデフォルトでインストールされていないので、
別途インストールします。

$ sudo td-agent-gem install fluent-plugin-dynamodb
WARN: Unresolved specs during Gem::Specification.reset:
json (>= 1.4.3)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
Fetching: fluentd-0.10.61.gem (100%)
Successfully installed fluentd-0.10.61
Fetching: jmespath-1.0.2.gem (100%)
Successfully installed jmespath-1.0.2
Fetching: aws-sdk-core-2.1.11.gem (100%)
Successfully installed aws-sdk-core-2.1.11
Fetching: aws-sdk-resources-2.1.11.gem (100%)
Successfully installed aws-sdk-resources-2.1.11
Fetching: aws-sdk-2.1.11.gem (100%)
Successfully installed aws-sdk-2.1.11
Fetching: fluent-plugin-dynamodb-0.1.11.gem (100%)
Successfully installed fluent-plugin-dynamodb-0.1.11
Parsing documentation for aws-sdk-2.1.11
Installing ri documentation for aws-sdk-2.1.11
Parsing documentation for aws-sdk-core-2.1.11
Installing ri documentation for aws-sdk-core-2.1.11
Parsing documentation for aws-sdk-resources-2.1.11
Installing ri documentation for aws-sdk-resources-2.1.11
Parsing documentation for fluent-plugin-dynamodb-0.1.11
Installing ri documentation for fluent-plugin-dynamodb-0.1.11
Parsing documentation for fluentd-0.10.61
Installing ri documentation for fluentd-0.10.61
Parsing documentation for jmespath-1.0.2
Installing ri documentation for jmespath-1.0.2
Done installing documentation for aws-sdk, aws-sdk-core, aws-sdk-resources, fluent-plugin-dynamodb, fluentd, jmespath after 7 seconds
6 gems installed

fluentd設定

td-agent.confを以下のように設定しました。

$ cat /etc/td-agent/td-agent.conf
・・・
(前略)
・・・
<source>
  type tail
  format apache
  path /var/log/httpd/access_log
  tag apache.access
</source>
<match apache.access>
  type dynamodb
  aws_key_id XXXXXXXXXXXXXXXX
  aws_sec_key XXXXXXXXXXXXXXXXXXXXXXXXXXX
  dynamo_db_endpoint dynamodb.ap-northeast-1.amazonaws.com
  dynamo_db_table apache-access-log
</match>

aws_key_id, aws_sec_keyはマスクしてあります。

fluentd起動

いよいよ fluentd を起動します。

$ sudo service td-agent start
Starting td-agent:                                         [  OK  ]

OKが出ましたが油断できません。
正しく起動できたかログを確認します。

$ sudo tail /var/log/td-agent/td-agent.log
・・・
(略)
・・・
xxxx-xx-xx xx:xx:xx +0000 [error]: Permission denied @ rb_sysopen - /var/log/httpd/access_log
xxxx-xx-xx xx:xx:xx +0000 [error]: suppressed same stacktrace

☆ハマりポイント2 Apacheのログファイルへのアクセス権限不足

見事にエラーが出ました。
どうやらApacheのログへの権限が足りていないようですので、確認します。

$ sudo ls -l /var/log/httpd/
合計 8
-rw-r--r-- 1 root root 671 xx月 xx xx:xx access_log
-rw-r--r-- 1 root root 991 xx月 xx xx:xx error_log

読み取り権限付いとるやんけ。。
ここでしばらく悩みましたが、 var/log/httpd に読み取り権限と実行権限が
必要であることに気が付き、設定した結果うまくいきました。

$ sudo ls -ld /var/log/httpd/
drwx------ 2 root root 4096 xx月 xx xx:xx /var/log/httpd/
$ sudo chmod 755 /var/log/httpd
$ sudo ls -ld /var/log/httpd/
drwxr-xr-x 2 root root 4096 xx月 xx xx:xx /var/log/httpd/
$ sudo tail /var/log/td-agent/td-agent.log
・・・
(略)
・・・
xxxx-xx-xx xx:xx:xx +0000 [info]: following tail of /var/log/httpd/access_log

テスト

設定が完了したので、Apacheにリクエストを送って
DynamoDBにログが出力されるかテストします。

まず、HTTPリクエストを送ってログを発生させます。

$ curl -I localhost
HTTP/1.1 403 Forbidden
Date: xxx, xx xxx xxx xx:xx:xx GMT
Server: Apache/2.2.29 (Amazon)
Accept-Ranges: bytes
Content-Length: 3839
Connection: close
Content-Type: text/html; charset=UTF-8


次にコンソールでDynamoDBにログが飛んでいるか確認します。
f:id:t_wkm2:20150809122738p:plain
むむむ、飛んでませんねぇ。

fluentd のログを確認します。

$ sudo tail /var/log/td-agent/td-agent.log
xxxx-xx-xx xx:xx:xx +0000 [error]: error on forwerder thread error="Broken pipe"
xxxx-xx-xx xx:xx:xx +0000 [error]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/process.rb:251:in `write'
xxxx-xx-xx xx:xx:xx +0000 [error]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/process.rb:251:in `to_msgpack'
xxxx-xx-xx xx:xx:xx +0000 [error]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/process.rb:251:in `block in run'
xxxx-xx-xx xx:xx:xx+0000 [error]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/process.rb:249:in `each'
xxxx-xx-xx xx:xx:xx +0000 [error]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/process.rb:249:in `run'

ログだけだと、原因がいまいちわかりませんでした。

☆ハマりポイント3 DynamoDBプラグインAWS-SDKの2.x未対応

ログのメッセージで引っ掛けて調べたところ、どうやらDynamoDBプラグイン
AWS-SDKの2.xに対応していないことが原因のようです。
※ソースは以下
 Working with DynamoDB Local · Issue #12 · gonsuke/fluent-plugin-dynamodb · GitHub

一応、インストールされているAWS-SDKのバージョンを確認します。

$ sudo td-agent-gem list aws-sdk
WARN: Unresolved specs during Gem::Specification.reset:
      json (>= 1.4.3)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.

*** LOCAL GEMS ***

aws-sdk (2.1.13)
aws-sdk-core (2.1.13)
aws-sdk-resources (2.1.13)
aws-sdk-v1 (1.64.0)

なぜか、両バージョンがインストールされているので、
2.1.13 のみアンインストールします。

$ sudo td-agent-gem uninstall aws-sdk aws-sdk-core aws-sdk-resources -v "~>2"
WARN: Unresolved specs during Gem::Specification.reset:
      json (>= 1.4.3)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.

You have requested to uninstall the gem:
	aws-sdk-2.1.13

fluent-plugin-dynamodb-0.1.11 depends on aws-sdk (>= 1.5.2)
If you remove this gem, these dependencies will not be met.
Continue with Uninstall? [yN]  y
Successfully uninstalled aws-sdk-2.1.13
Successfully uninstalled aws-sdk-resources-2.1.13
Remove executables:
	aws.rb

in addition to the gem? [Yn]  Y
Removing aws.rb
Successfully uninstalled aws-sdk-core-2.1.13
$ sudo td-agent-gem list aws-sdk
WARN: Unresolved specs during Gem::Specification.reset:
      json (>= 1.4.3)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.

*** LOCAL GEMS ***

aws-sdk-v1 (1.64.0)

無事、1.64.0 のみになったので、fluentd を再起動して、
エラーが出ないことを確認します。

$ sudo service td-agent restart
/opt/td-agent/embedded/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- aws-sdk (LoadError)
	from /opt/td-agent/embedded/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluent-plugin-dynamodb-0.1.11/lib/fluent/plugin/out_dynamodb.rb:15:in `initialize'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/plugin.rb:108:in `new'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/plugin.rb:108:in `new_impl'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/plugin.rb:57:in `new_output'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/agent.rb:125:in `add_match'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/agent.rb:60:in `block in configure'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/agent.rb:54:in `each'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/agent.rb:54:in `configure'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/root_agent.rb:82:in `configure'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/engine.rb:93:in `configure'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/engine.rb:77:in `run_configure'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/supervisor.rb:457:in `run_configure'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/supervisor.rb:181:in `dry_run'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/supervisor.rb:130:in `start'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/lib/fluent/command/fluentd.rb:167:in `<top (required)>'
	from /opt/td-agent/embedded/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:73:in `require'
	from /opt/td-agent/embedded/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:73:in `require'
	from /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.12/bin/fluentd:6:in `<top (required)>'
	from /opt/td-agent/embedded/bin/fluentd:23:in `load'
	from /opt/td-agent/embedded/bin/fluentd:23:in `<top (required)>'
	from /usr/sbin/td-agent:7:in `load'
	from /usr/sbin/td-agent:7:in `<main>'

おもいっきりエラーが出ました。
どうやら AWS-SDK が読み込めていないようです。

おそらく、"aws-sdk-v1"という名前が認識できない原因な気がするので、
"aws-sdk"の1.xをインストールして、fluentdを再起動します。
※そもそも、"aws-sdk-v1"と"aws-sdk"の1.xがどう違うのかはわかってないですが、、

$ sudo td-agent-gem install aws-sdk -v "~>1"
WARN: Unresolved specs during Gem::Specification.reset:
      json (>= 1.4.3)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
Fetching: aws-sdk-1.64.0.gem (100%)
Successfully installed aws-sdk-1.64.0
Parsing documentation for aws-sdk-1.64.0
Installing ri documentation for aws-sdk-1.64.0
Done installing documentation for aws-sdk after 0 seconds
1 gem installed
$ sudo td-agent-gem install aws-sdk -v "~>1"
WARN: Unresolved specs during Gem::Specification.reset:
      json (>= 1.4.3)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
Fetching: aws-sdk-1.64.0.gem (100%)
Successfully installed aws-sdk-1.64.0
Parsing documentation for aws-sdk-1.64.0
Installing ri documentation for aws-sdk-1.64.0
Done installing documentation for aws-sdk after 0 seconds
1 gem installed
$ sudo service td-agent restart
Shutting down td-agent:                                    [  OK  ]
Starting td-agent:                                         [  OK  ]

無事、再起動できたので、再びHTTPリクエストを送って
ログを発生させ、DynamoDBにログが飛んでいるか確認します。

$ curl -I localhost
HTTP/1.1 403 Forbidden
Date: xxx, xx xxx xxx xx:xx:xx GMT
Server: Apache/2.2.29 (Amazon)
Accept-Ranges: bytes
Content-Length: 3839
Connection: close
Content-Type: text/html; charset=UTF-8

f:id:t_wkm2:20150809141218p:plain
無事、DynamoDBにログを出力できました。


手順全部を乗っけたらかなり長くなってしまいました。
これだと書くのに時間かかるから、次から要点を絞って書こう。。。

追記

インストール時にinstall-redhat-td-agent2.shを使用することもできるようです。

install-redhat-td-agent2.sh はtd.repo(fluentdのリポジトリ設定)をインストールし、
yum check-update
yum install -y td-agent
を実行します。

このインストール方法では、常に最新のバージョンをインストールできます。
※旧バージョンを選択できないともいいますが。

$ curl -L https://td-toolbelt.herokuapp.com/sh/install-redhat-td-agent2.sh | sudo sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   662  100   662    0     0    911      0 --:--:-- --:--:-- --:--:--   910
This script requires superuser access to install rpm packages.
You will be prompted for your password by sudo.
読み込んだプラグイン:priorities, update-motd, upgrade-helper
amzn-main/latest                                                                 | 2.1 kB     00:00     
amzn-updates/latest                                                              | 2.3 kB     00:00     
treasuredata/latest/x86_64                                                       | 2.9 kB     00:00     
treasuredata/latest/x86_64/primary_db                                            | 9.8 kB     00:00     

at.x86_64                                        3.1.10-44.13.amzn1                         amzn-updates
authconfig.x86_64                                6.1.12-23.25.amzn1                         amzn-updates
aws-amitools-ec2.noarch                          1.5.7-1.0.amzn1                            amzn-updates
aws-apitools-common.noarch                       1.1.0-1.9.amzn1                            amzn-updates
aws-cfn-bootstrap.noarch                         1.4-9.4.amzn1                              amzn-updates
aws-cli.noarch                                   1.7.42-1.20.amzn1                          amzn-updates
cloud-init.noarch                                0.7.6-2.5.amzn1                            amzn-updates
cryptsetup.x86_64                                1.6.3-2.19.amzn1                           amzn-updates
cryptsetup-libs.x86_64                           1.6.3-2.19.amzn1                           amzn-updates
curl.x86_64                                      7.40.0-3.52.amzn1                          amzn-updates
e2fsprogs.x86_64                                 1.42.12-4.35.amzn1                         amzn-updates
e2fsprogs-libs.x86_64                            1.42.12-4.35.amzn1                         amzn-updates
ec2-net-utils.noarch                             0.4-1.23.amzn1                             amzn-updates
ec2-utils.noarch                                 0.4-1.23.amzn1                             amzn-updates
glibc.x86_64                                     2.17-55.143.amzn1                          amzn-updates
glibc-common.x86_64                              2.17-55.143.amzn1                          amzn-updates
gnupg2.x86_64                                    2.0.28-1.30.amzn1                          amzn-updates
gpgme.x86_64                                     1.4.3-5.15.amzn1                           amzn-updates
kernel.x86_64                                    3.14.48-33.39.amzn1                        amzn-updates
kernel-tools.x86_64                              3.14.48-33.39.amzn1                        amzn-updates
krb5-libs.x86_64                                 1.12.2-14.43.amzn1                         amzn-updates
libcap-ng.x86_64                                 0.7.3-5.13.amzn1                           amzn-updates
libcom_err.x86_64                                1.42.12-4.35.amzn1                         amzn-updates
libcurl.x86_64                                   7.40.0-3.52.amzn1                          amzn-updates
libgcrypt.x86_64                                 1.5.3-12.18.amzn1                          amzn-updates
libjpeg-turbo.x86_64                             1.2.90-5.10.amzn1                          amzn-updates
libselinux.x86_64                                2.1.10-3.22.amzn1                          amzn-updates
libss.x86_64                                     1.42.12-4.35.amzn1                         amzn-updates
nspr.x86_64                                      4.10.8-1.33.amzn1                          amzn-updates
nss.x86_64                                       3.19.1-3.71.amzn1                          amzn-updates
nss-softokn.x86_64                               3.16.2.3-9.36.amzn1                        amzn-updates
nss-softokn-freebl.x86_64                        3.16.2.3-9.36.amzn1                        amzn-updates
nss-sysinit.x86_64                               3.19.1-3.71.amzn1                          amzn-updates
nss-tools.x86_64                                 3.19.1-3.71.amzn1                          amzn-updates
nss-util.x86_64                                  3.19.1-1.41.amzn1                          amzn-updates
openssh.x86_64                                   6.2p2-8.44.amzn1                           amzn-updates
openssh-clients.x86_64                           6.2p2-8.44.amzn1                           amzn-updates
openssh-server.x86_64                            6.2p2-8.44.amzn1                           amzn-updates
pcre.x86_64                                      8.21-7.7.amzn1                             amzn-updates
python27.x86_64                                  2.7.9-4.115.amzn1                          amzn-updates
python27-bcdoc.noarch                            0.16.0-1.9.amzn1                           amzn-updates
python27-boto.noarch                             2.38.0-1.7.amzn1                           amzn-updates
python27-botocore.noarch                         1.1.5-1.21.amzn1                           amzn-updates
python27-configobj.noarch                        4.7.2-7.15.amzn1                           amzn-updates
python27-devel.x86_64                            2.7.9-4.115.amzn1                          amzn-updates
python27-jmespath.noarch                         0.7.1-1.9.amzn1                            amzn-updates
python27-libs.x86_64                             2.7.9-4.115.amzn1                          amzn-updates
python27-pip.noarch                              6.1.1-1.20.amzn1                           amzn-updates
python27-setuptools.noarch                       12.2-1.30.amzn1                            amzn-updates
rpm.x86_64                                       4.11.2-2.64.amzn1                          amzn-updates
rpm-build-libs.x86_64                            4.11.2-2.64.amzn1                          amzn-updates
rpm-libs.x86_64                                  4.11.2-2.64.amzn1                          amzn-updates
rpm-python27.x86_64                              4.11.2-2.64.amzn1                          amzn-updates
ruby20.x86_64                                    2.0.0.645-1.27.amzn1                       amzn-updates
ruby20-irb.noarch                                2.0.0.645-1.27.amzn1                       amzn-updates
ruby20-libs.x86_64                               2.0.0.645-1.27.amzn1                       amzn-updates
rubygem20-bigdecimal.x86_64                      1.2.0-1.27.amzn1                           amzn-updates
rubygem20-psych.x86_64                           2.0.0-1.27.amzn1                           amzn-updates
rubygems20.noarch                                2.0.14-1.27.amzn1                          amzn-updates
sudo.x86_64                                      1.8.6p3-19.19.amzn1                        amzn-updates
time.x86_64                                      1.7-38.9.amzn1                             amzn-updates
tzdata.noarch                                    2015b-1.33.amzn1                           amzn-updates
tzdata-java.noarch                               2015b-1.33.amzn1                           amzn-updates
unzip.x86_64                                     6.0-2.9.amzn1                              amzn-updates
yum.noarch                                       3.4.3-137.57.amzn1                         amzn-updates
yum-metadata-parser.x86_64                       1.1.4-8.12.amzn1                           amzn-updates
読み込んだプラグイン:priorities, update-motd, upgrade-helper
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ td-agent.x86_64 0:2.2.1-0.el2015 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

========================================================================================================
 Package               アーキテクチャー    バージョン                   リポジトリー               容量
========================================================================================================
インストール中:
 td-agent              x86_64              2.2.1-0.el2015               treasuredata               57 M

トランザクションの要約
========================================================================================================
インストール  1 パッケージ

総ダウンロード容量: 57 M
インストール容量: 199 M
Downloading packages:
td-agent-2.2.1-0.el2015.x86_64.rpm                                               |  57 MB     00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : td-agent-2.2.1-0.el2015.x86_64                                          1/1 
adding 'td-agent' group...
adding 'td-agent' user...
Installing default conffile...
Configure td-agent to start, when booting up the OS...
  検証中                  : td-agent-2.2.1-0.el2015.x86_64                                          1/1 

インストール:
  td-agent.x86_64 0:2.2.1-0.el2015                                                                      

完了しました!