Fluentedインストールでハマった
ハマりポイントは以下の2点。
1.収集対象のログファイルおよびログファイルがあるディレクトリの権限
2.Fluentedが生成する一時ファイルの置き場
一応動くようになったので手順をまとめる。
【記事概要】
ApacheのアクセスログをFluented使って収集し、ローカルファイルに溜め込む。
→S3に溜め込むなど難しいことはしない。
【概要図】
【前提】
・AWS(EC2)にてAmazonLinuxのインスタンスを立ち上げ済み。
・セキュリティグループ設定は以下の通り。
【インストール手順】
0.サーバに入った後でrootユーザでログイン、「yum update」を行う。
1.WebサーバにApacheインストール
(1)httpdの最新版を探す
yum search httpd
(2)(1)で探したhttpdをインストールする。 (※AmazonLinuxの場合は「httpd.x86_64」をインストール)
yum install httpd.x86_64 -y
(3)Apache起動用ユーザを作り、起動する。
[参考]
http://honana.com/apache/apache_22/user
groupadd httpd useradd -g httpd -d /var/empty/httpd -s /sbin/nologin httpd vi /usr/local/httpd/conf/httpd.conf /etc/init.d/httpd start
「httpd.conf」
# # If you wish httpd to run as a different user or group, you must run # httpd as root initially and it will switch. # # User/Group: The name (or #number) of the user/group to run httpd as. # . On SCO (ODT 3) use "User nouser" and "Group nogroup". # . On HPUX you may not be able to use shared memory as nobody, and the # suggested workaround is to create a user www and use that user. # NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET) # when the value of (unsigned)Group is above 60000; # don't use Group #-1 on these systems! # #User apache #Group apache User httpd Group httpd
2.WebサーバにFluentedインストール [参考]http://docs.fluentd.org/articles/install-by-rpm
(1)curlコマンドを使って「td-agent」を入れる。 ※cのコンパイラ、Rubyの実行環境等をインストールする必要はない。
curl -L http://toolbelt.treasuredata.com/sh/install-redhat.sh | sh
(2)とりあえず動作確認(起動&終了)。エラーにならなかったらOK。
[root@ip-xxx-xxx-xxx-xxx ~]# /etc/init.d/td-agent start Starting td-agent: [ OK ]
[root@ip-xxx-xxx-xxx-xxx ~]# /etc/init.d/td-agent stop Shutting down td-agent: [ OK ]
3.WebサーバのFluented設定ファイルを削除し新規作成
(1)既存の設定ファイルを削除。
[root@ip-xxx-xxx-xxx-xxx ~]# cd /etc/td-agent/ [root@ip-xxx-xxx-xxx-xxx td-agent]# ls -al total 24 drwxr-xr-x 4 root root 4096 May 18 09:35 . drwxr-xr-x 73 root root 4096 May 18 08:50 .. drwxr-xr-x 2 root root 4096 May 18 08:49 logrotate.d drwxr-xr-x 2 root root 4096 May 18 08:49 prelink.conf.d -rw-r--r-- 1 root root 234 May 18 09:35 td-agent.conf -rw-r--r-- 1 root root 1982 Apr 1 06:30 td-agent.conf.tmpl [root@ip-xxx-xxx-xxx-xxx td-agent]# rm -rf td-agent.conf
(2)新しくファイルを作る。
→収集対象は「/var/log/tmp/httpd/httpd-access」ファイルとする。
[root@ip-xxx-xxx-xxx-xxx td-agent]# vim td-agent.conf
<source> type tail path /var/log/httpd/access_log tag apache.access pos_file /var/log/td-agent/httpd-access_log.pos format apache2 </source> <match apache.access> type file path /var/log/tmp/httpd/httpd-access </match>
5.ディレクトリの権限変更
(1)ログファイル周りは以下の構成・権限にする。
「Apache起動前・td-agent.conf編集前」
[root@ip-xxx-xxx-xxx-xxx log]# pwd /var/log [root@ip-xxx-xxx-xxx-xxx log]# tree -fpug --charset=o . |-- [drwx------ root root ] ./httpd |-- [drwxr-xr-x td-agent td-agent] ./td-agent | |-- [drwxrwxrwx td-agent td-agent] ./td-agent/buffer | `-- [-rw-r--r-- td-agent td-agent] ./td-agent/td-agent.log
※AmazonLinuxにtreeコマンドはインストールされていないので、必要であればyumコマンドでインストールする。
「ディレクトリ・一時ファイル作成および権限変更後」
[root@ip-xxx-xxx-xxx-xxx log]# pwd /var/log [root@ip-xxx-xxx-xxx-xxx log]# tree -fpug --charset=o . |-- [drw-r-xr-x root root ] ./httpd★ | |-- [-rw-r--r-- root root ] ./httpd/access_log | `-- [-rw-r--r-- root root ] ./httpd/error_log |-- [drwxrwxrwx td-agent td-agent] ./td-agent★ | |-- [drwxrwxrwx td-agent td-agent] ./td-agent/buffer | |-- [-rwxrwxrwx td-agent td-agent] ./td-agent/httpd-access_log.pos★ | `-- [-rwxrwxrwx td-agent td-agent] ./td-agent/td-agent.log★ |-- [drwxrwxrwx root root ] ./tmp★ | `-- [drw-rwxrwx root root ] ./tmp/httpd★ | |-- [-rwxrwxrwx td-agent td-agent] ./tmp/httpd/httpd-access★ | `-- [-rwxrwxrwx td-agent td-agent] ./tmp/httpd/httpd-access.20140518.b4f9a9641ada71f3d★
→★の箇所は変更および作成した箇所。今回の作業に関係ないファイルは省略した。
※実際にログが格納されるファイルは「httpd-access.20140518.b4f9a9641ada71f3d」だったので、おそらく定義体でのファイル名に対するタイムスタンプの設定が足りていない(と思われる)。
ポイントとしては、
・httpdディレクトリ(収集対象のログがあるディレクトリ)に読み取り権限をつける。
・td-agent、tmpディレクトリはrootユーザでのフルコントロールとする。
→tmpディレクトリは「td-agent」ユーザにすると以下の例外が出て怒られた。
2014-05-18 11:35:51 +0000 [error]: unexpected error error_class=Errno::EACCES error=#<Errno::EACCES: Permission denied - /var/log/tmp/httpd/httpd-access.20140518.b4f9a9641ada71f3d> 2014-05-18 11:35:51 +0000 [error]: /usr/lib64/fluent/ruby/lib/ruby/gems/1.9.1/gems/fluentd-0.10.45/lib/fluent/plugin/buf_file.rb:24:in `initialize' (略)
(2)td-agent周りは特に変更する必要は無い。
[root@ip-10-132-134-118 log]# cd /etc/td-agent/ [root@ip-10-132-134-118 td-agent]# tree -fpug --charset=o . |-- [drwxr-xr-x root root ] ./logrotate.d | `-- [-rw-r--r-- root root ] ./logrotate.d/td-agent.logrotate |-- [drwxr-xr-x root root ] ./prelink.conf.d | `-- [-rw-r--r-- root root ] ./prelink.conf.d/td-agent.conf |-- [-rw-r--r-- root root ] ./td-agent.conf `-- [-rw-r--r-- root root ] ./td-agent.conf.tmpl 2 directories, 4 files
[備考]
(1)「td-agent」コマンドを使用して権限のつけ忘れチェックを行う。正常時のメッセージは以下の通り。(コマンドをkillして終了する)
[root@ip-xxx-xxx-xxx-xxx var]# td-agent 2014-05-18 11:29:10 +0000 [info]: starting fluentd-0.10.45 2014-05-18 11:29:11 +0000 [info]: reading config file path="/etc/td-agent/td-agent.conf" 2014-05-18 11:29:11 +0000 [info]: gem 'fluent-mixin-config-placeholders' version '0.2.4' 2014-05-18 11:29:11 +0000 [info]: gem 'fluent-mixin-plaintextformatter' version '0.2.6' 2014-05-18 11:29:11 +0000 [info]: gem 'fluent-plugin-flume' version '0.1.1' 2014-05-18 11:29:11 +0000 [info]: gem 'fluent-plugin-mongo' version '0.7.3' 2014-05-18 11:29:11 +0000 [info]: gem 'fluent-plugin-rewrite-tag-filter' version '1.4.1' 2014-05-18 11:29:11 +0000 [info]: gem 'fluent-plugin-s3' version '0.3.7' 2014-05-18 11:29:11 +0000 [info]: gem 'fluent-plugin-scribe' version '0.10.10' 2014-05-18 11:29:11 +0000 [info]: gem 'fluent-plugin-td' version '0.10.18' 2014-05-18 11:29:11 +0000 [info]: gem 'fluent-plugin-td-monitoring' version '0.1.1' 2014-05-18 11:29:11 +0000 [info]: gem 'fluent-plugin-webhdfs' version '0.2.2' 2014-05-18 11:29:11 +0000 [info]: gem 'fluentd' version '0.10.45' 2014-05-18 11:29:11 +0000 [info]: using configuration file: <ROOT> <source> type tail path /var/log/httpd/access_log tag apache.access pos_file /var/log/td-agent/httpd-access_log.pos format apache2 </source> <match apache.access> type file path /var/log/tmp/httpd/httpd-access </match> </ROOT> 2014-05-18 11:29:11 +0000 [info]: adding source type="tail" 2014-05-18 11:29:11 +0000 [info]: adding match pattern="apache.access" type="file" 2014-05-18 11:29:11 +0000 [info]: following tail of /var/log/httpd/access_log ^C2014-05-18 11:29:14 +0000 [info]: shutting down fluentd 2014-05-18 11:29:14 +0000 [info]: process finished code=0 [root@ip-xxx-xxx-xxx-xxx var]#
6.Fluetnedにより生成された、Apacheのログファイルを読み込んだファイルを確認
→事前に何回かブラウザからApacheにアクセスしておく。
[root@ip-xxx-xxx-xxx-xxx ~]# cat /var/log/tmp/httpd/httpd-access.20140518.b4f9a9641ada71f3d og 2014-05-18T10:17:02+00:00 apache.access {"host":"xxx.xxx.xxx.xxx","user":null,"method":"GET","path":"/","code":403,"size":3839,"referer":null,"agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36"} 2014-05-18T11:37:05+00:00 apache.access {"host":"xxx.xxx.xxx.xxx","user":null,"method":"GET","path":"/","code":403,"size":3839,"referer":null,"agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36"} 2014-05-18T11:39:20+00:00 apache.access {"host":"xxx.xxx.xxx.xxx","user":null,"method":"GET","path":"/","code":403,"size":3839,"referer":null,"agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36"}
【感想】
・ログ収集を行う前提であらかじめミドルウェアetcを入れておき、ディレクトリの権限を指定しておく必要があると感じた。
→インフラ用のデザインシートが必要な理由がわかった気がする。
・動作確認では欲張らないようにしよう
- 作者: 五十嵐啓人,伊野亘輝,近藤宇智朗,渡邊恵太,須藤耕平,中島聡,A-Listers,はまちや2,川添貴生,片山育美,池田拓司,濱崎健吾,佐藤太一,曾川景介,久保渓,門脇恒平,登尾徳誠,伊藤直也,mala,後藤秀宣,若原祥正,奥野幹也,大林源,WEB+DB PRESS編集部
- 出版社/メーカー: 技術評論社
- 発売日: 2013/08/24
- メディア: 大型本
- この商品を含むブログを見る