いけむランド

はてダからやってきました

Fluentd + ElasticSearch + Kibana3 を mac で試してみる (追記あり)

流行ってるみたいだったので試しに mac に環境を構築してみました。


基本的にはググって出てくる debian/ubuntu 向けのインストールを参考にしてもらえばいいと思います。

Fluentd

gem でぶっこみましょう。
設定ファイルは上記の記事から丸パクリです。

$ gem install fluentd fluent-plugin-elasticsearch
$ fluentd --setup io~/.fluent
$ vi ~/.fluent/fluent.conf
$ cat ~/.fluent/fluent.conf
  <source>
    type tail
    format apache
    path /Users/fd0/log/access.log
    tag apache.access
  </source>
  <match apache.access>
    index_name adminpack
    type_name apache
    type elasticsearch
    include_tag_key true
    tag_key @log_name
    host localhost
    port 9200
    logstash_format true
    flush_interval 3s
  </match>
  <match debug.**>
    type stdout
  </match>
fluentd --config ~/.fluent/fluent.conf                                                            
2013-12-03 08:19:08 +0900 [info]: starting fluentd-0.10.40
2013-12-03 08:19:08 +0900 [info]: reading config file path="/Users/fd0/.fluent/fluent.conf"
2013-12-03 08:19:08 +0900 [info]: gem 'fluent-plugin-elasticsearch' version '0.1.4'
2013-12-03 08:19:08 +0900 [info]: gem 'fluentd' version '0.10.40'
2013-12-03 08:19:08 +0900 [info]: using configuration file: <ROOT>
  <source>
    type tail
    format apache
    path /Users/fd0/log/access.log
    tag apache.access
  </source>
  <match apache.access>
    index_name adminpack
    type_name apache
    type elasticsearch
    include_tag_key true
    tag_key @log_name
    host localhost
    port 9200
    logstash_format true
    flush_interval 3s
  </match>
  <match debug.**>
    type stdout
  </match>
</ROOT>
2013-12-03 08:19:08 +0900 [info]: adding source type="tail"
2013-12-03 08:19:08 +0900 [warn]: 'pos_file PATH' parameter is not set to a 'tail' source.
2013-12-03 08:19:08 +0900 [warn]: this parameter is highly recommended to save the position to resume tailing.
2013-12-03 08:19:08 +0900 [info]: adding match pattern="apache.access" type="elasticsearch"
2013-12-03 08:19:08 +0900 [info]: adding match pattern="debug.**" type="stdout"

Elasticsearch

最初は homebrew でぶっこんだのですが、どうにも fluentd からデータが渡って来ないように見えるため、twitter に呟きを放流すると公式から持ってきた方が楽という助言をいただきました。



$ cd ~/Downloads
$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.tar.gz
$ tar xf elasticsearch-0.90.7.tar.gz
$ cd /usr/local
$ mv ~/Downloads/elasticsearch-0.90.7
$ ln -s elasticsearch-0.90.7 elasticsearch
$ cd
$ /usr/local/elasticsearch/bin/elasticsearch -f
[2013-12-03 08:15:02,516][INFO ][node                     ] [Essex, Nathaniel] version[0.90.7], pid[22180], build[36897d0/2013-11-13T12:06:54Z]
[2013-12-03 08:15:02,517][INFO ][node                     ] [Essex, Nathaniel] initializing ...
[2013-12-03 08:15:02,523][INFO ][plugins                  ] [Essex, Nathaniel] loaded [], sites []
[2013-12-03 08:15:05,167][INFO ][node                     ] [Essex, Nathaniel] initialized
[2013-12-03 08:15:05,168][INFO ][node                     ] [Essex, Nathaniel] starting ...
[2013-12-03 08:15:05,314][INFO ][transport                ] [Essex, Nathaniel] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/192.168.11.10:9300]}
[2013-12-03 08:15:08,367][INFO ][cluster.service          ] [Essex, Nathaniel] new_master [Essex, Nathaniel][CGh8bM1vSA62juUkwKzBAw][inet[/192.168.11.10:9300]], reason: zen-disco-join (elected_as_master)
[2013-12-03 08:15:08,399][INFO ][discovery                ] [Essex, Nathaniel] elasticsearch/CGh8bM1vSA62juUkwKzBAw
[2013-12-03 08:15:08,412][INFO ][http                     ] [Essex, Nathaniel] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/192.168.11.10:9200]}
[2013-12-03 08:15:08,413][INFO ][node                     ] [Essex, Nathaniel] started
[2013-12-03 08:15:08,432][INFO ][gateway                  ] [Essex, Nathaniel] recovered [0] indices into cluster_state

Kibana3

【追記】Elasticsearch の plugin に kibana があるため、そちらを使う方がまとまってて良さそうです。



$ cd /usr/local/elasticsearch
$ bin/plugin -i elasticsearch/kibana
-> Installing elasticsearch/kibana...
Trying https://github.com/elasticsearch/kibana/archive/master.zip...
Downloading .................................................................................DONE
Installed elasticsearch/kibana into /usr/local/elasticsearch/plugins/kibana
Identified as a _site plugin, moving to _site structure ...


【追記】plugin でインストールした場合は以下は不要です。
最初にググった時に kinaba と入力してハマったのは内緒ですが、git でぶっこみましょう。
デフォルトのポートを使うため、特に修正は必要ないと思われます。

$ cd /usr/local
$ git clone https://github.com/elasticsearch/kibana.git

Apache

【追記】plugin でインストールした場合は以下も不要です。
/etc/apache2/httpd.conf に Alias を設定しました。

Alias /kibana3 /usr/local/kibana


apachectl で起動します。
kinaba ディレクトリの permission をユーザに合わせておかないと 403 が出るかもしれません。

$ apachectl start

apache-loggen

アクセスログを生成させます。

% gem install apache-loggen
% apache-loggen --limit=1000 ~/log/access.log

確認

ファイルがつくられると fluentd のログに以下のメッセージが出るはずです。

2013-12-03 08:19:08 +0900 [info]: following tail of /Users/fd0/log/access.log


また elasticsearch のログにも以下のようなメッセージが出るはずです。

[2013-12-03 08:28:53,575][INFO ][cluster.metadata         ] [Essex, Nathaniel] [logstash-2013.12.02] creating index, cause [auto(bulk api)], shards [5]/[1], mappings []
[2013-12-03 08:28:54,863][INFO ][cluster.metadata         ] [Essex, Nathaniel] [logstash-2013.12.02] update_mapping [apache] (dynamic)


【追記】plugin の場合は以下の URL を指定して kibana3 にアクセスしてみます。

$ open http://127.0.0.1:9200/_plugin/kibana/src/index.html


plugin を使っている場合は elasticsearch のログにその旨が表示されます。

[2013-12-04 20:46:21,932][INFO ][plugins                  ] [Native] loaded [], sites [kibana]


kibana3 にアクセスしてみます。

$ open http://127.0.0.1/kibana3


f:id:fd0:20131203091356p:plain
↑ src/ を見てねって書いてあるので、そちらに移動します。


f:id:fd0:20131203091412p:plain
↑ logstash interface に移動します。


f:id:fd0:20131203091544p:plain
↑ loggen で生成したログが反映されていることが確認できました。


∩( ・ω・)∩ ばんじゃーい