13
elasticsearch,fluentd, Kibanaを使ったログ解析 July,13,2014 Tadayasu Yotsu

Log analysis by using elasticsearch,kibana and fluentd

Embed Size (px)

DESCRIPTION

Log analysis by using fluentd, elasticsearch and kibana.

Citation preview

Page 1: Log analysis by using elasticsearch,kibana and fluentd

!

elasticsearch,fluentd, Kibanaを使ったログ解析

July,13,2014 Tadayasu Yotsu

Page 2: Log analysis by using elasticsearch,kibana and fluentd

今回のゴールよくブログなどで紹介されているfluentdを使ってログを収集し、

elastcisearchでデータの解析を行い、kibanaで可視化するということをやってみる。

各softwareついては別途調べる。とりあえず動かして感覚をつかむことが今

回の目的。今回は1台だけで確認したので、fluentdはひとつ。

kibana

Page 3: Log analysis by using elasticsearch,kibana and fluentd

What is elasticsearch,fluentd,kibana ?

elasticsearch

オープンソースの検索エンジン。本家サイトでは「分散されたRestfulな検

索と分析のツール」と定義されている。よく比較されるのはApache

Solr。

Fluentd

オープンソースのログ収集ツール。イベントの受け取り、出力が全てプラグインとして実装しているため、柔軟にカスタマイズできるのが大きな特徴。

Kibana

elasticsearch内のデータを可視化するためのツール。洗練されたデザインが特徴。

Page 4: Log analysis by using elasticsearch,kibana and fluentd

動作環境動作環境

AWS EC2

OS:RHEL6.5

ruby:2.1.2

nginx:1.6.0

elasticsearch:1.2.2

kibana3.1.0

fluentd:0.10.52

Page 5: Log analysis by using elasticsearch,kibana and fluentd

elasticsearchのインストール

$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.2.2.tar.gz$ tar zxvf elasticsearch-1.2.2.tar.gz

インストール

設定 config/elasticsearch.ymlが設定ファイルなのでこのファイルを修正 今回はとりあえず以下の設定だけ入れておく。

index.number_of_shards: 1 #シャードの数index.number_of_replicas: 0 #レプリカの数# Path to directory where to store index data allocated for this node.path.data: /home/ec2-user/elasticsearch-1.2.2/data# Path to log files:path.logs: /home/ec2-user/elasticsearch-1.2.2/logs

Page 6: Log analysis by using elasticsearch,kibana and fluentd

elasticsearchの起動と確認$ ./elasticsearch[2014-07-16 08:12:29,980][INFO ][node ] [Conquest] version[1.2.2], pid[1590], build[9902f08/2014-07-09T12:02:32Z][2014-07-16 08:12:29,981][INFO ][node ] [Conquest] initializing ...[2014-07-16 08:12:29,989][INFO ][plugins ] [Conquest] loaded [], sites [][2014-07-16 08:12:34,817][INFO ][node ] [Conquest] initialized[2014-07-16 08:12:34,818][INFO ][node ] [Conquest] starting ...[2014-07-16 08:12:34,971][INFO ][transport ] [Conquest] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.31.38.39:9300]}[2014-07-16 08:12:38,089][INFO ][cluster.service ] [Conquest] new_master [Conquest][2STmYQQfQ5Ssz4_9RJXXOQ][ip-172-31-38-39.us-west-2.compute.internal][inet[/172.31.38.39:9300]], reason: zen-disco-join (elected_as_master)[2014-07-16 08:12:38,247][INFO ][discovery ] [Conquest] elasticsearch/2STmYQQfQ5Ssz4_9RJXXOQ[2014-07-16 08:12:38,281][INFO ][http ] [Conquest] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.31.38.39:9200]}[2014-07-16 08:12:38,320][INFO ][gateway ] [Conquest] recovered [0] indices into cluster_state[2014-07-16 08:12:38,320][INFO ][node ] [Conquest] started

起動

確認 9200番ポートに対してアクセスし、下記のようなレスポンスが返ってきたらOK

$ curl localhost:9200{ "status" : 200, "name" : "Conquest", "version" : { "number" : "1.2.2", "build_hash" : "9902f08efc3ad14ce27882b991c4c56b920c9872", "build_timestamp" : "2014-07-09T12:02:32Z", "build_snapshot" : false, "lucene_version" : "4.8" }, "tagline" : "You Know, for Search"}

Page 7: Log analysis by using elasticsearch,kibana and fluentd

Kibanaのインストール$ wget https://download.elasticsearch.org/kibana/kibana/kibana-3.1.0.tar.gz$ tar zxvf kibana-3.1.0.tar.gz $ mv kibana-3.1.0 /usr/locl

インストール

設定(config.jsを修正) 今回はとりあえず32行目あたりのelasticsearchの接続先指定の設定だけ入れておく。

elasticsearch: "http://ホスト名:9200",

$ wget http://nginx.org/download/nginx-1.6.0.tar.gz$ tar zxvf nginx-1.6.0.tar.gz $ cd nginx-1.6.0$ ./configure —without-http_rewrite_module ; make ; sudo make install

nginxをインストールし、kibanaにアクセスできるようにする。

$ sudo vi /usr/local/nginx/conf/nginx.confserver { listen 80; server_name localhost; #charset koi8-r; access_log /var/log/nginx.access.log; location / { root /usr/local/kibana-3.1.0/; index index.html index.htm; }

Page 8: Log analysis by using elasticsearch,kibana and fluentd

nginxのインストールと設定$ wget http://nginx.org/download/nginx-1.6.0.tar.gz$ tar zxvf nginx-1.6.0.tar.gz $ cd nginx-1.6.0$ ./configure —without-http_rewrite_module ; make ; sudo make install

nginxをインストールし、kibanaにアクセスできるようにする。

$ sudo vi /usr/local/nginx/conf/nginx.confhttp { ・・・省略・・・   //以下を追記。 logのformatをltsv形式にする。fluetndでの記述を簡単にするため。 log_format ltsv "host:$remote_addr" "\tuser:$remote_user" "\ttime:$time_local" "\treq:$request" "\tstatus:$status" "\tsize:$body_bytes_sent" "\treferer:$http_referer" "\tua:$http_user_agent" "\tforwardedfor:$http_x_forwarded_for" "\treqtime:$request_time" "\tapptime:$upstream_response_time";!server { listen 80; server_name ホスト名; #charset koi8-r; access_log /usr/local/nginx/logs/nginx.access.log ltsv; ※ltsv形式で指定したパスにアクセスログを出力するように設定 location / { root /usr/local/kibana-3.1.0/; index index.html index.htm; }

Page 9: Log analysis by using elasticsearch,kibana and fluentd

Kibanaの確認

$ sudo /usr/local/nginx/sbin/nginx

nginxの起動

ブラウザでhttp://<Nginxのサーバ接続先>/へアクセス  EC2のSecurity Groupsでポート9200を開放していなかったので、アクセスエラーに。。。開放したらアクセスOK。

Page 10: Log analysis by using elasticsearch,kibana and fluentd

fluentdのインストール

$ sudo yum install gcc zlib-devel openssl-devel libcurl-devel$ ./configure —prefix=/home/ec2-user/ruby$ make; make install ※インストール後にPATHは通しておく$ git clone https://github.com/fluent/fluentd.git$ cd fluentd$ gem install bundler$ bundle install$ rake build$ gem install pkg/fluentd-0.10.52.gem$ gem install fluent-plugin-elasticsearch

インストール OS標準のrubyはバージョンが古かったのでソースからruby-2.1.2をインストールし、fluentdもソースからインストールする。そして、fluentdからElasticsearchにログを転送するプラグインをインストールする。

Page 11: Log analysis by using elasticsearch,kibana and fluentd

fluentdの設定と起動設定

access_logというタグを持ったログがElasticsearchに転送されるようにfluent.confに記載<source> type tail path /usr/local/nginx/logs/nginx.access.log format ltsv time_format %d/%b/%Y:%H:%M:%S %z tag access_log pos_file /var/log/nginx.pos</source>!<match access_log> type elasticsearch host ホスト名 port 9200 type_name access_log logstash_format true logstash_prefix nginx_access logstash_dateformat %Y%m buffer_type memory buffer_chunk_limit 10m buffer_queue_limit 10 flush_interval 1s retry_limit 16 retry_wait 1s</match>

起動

$ fluetnd -c /home/ec2-user/fluentd/fluent.conf

Page 12: Log analysis by using elasticsearch,kibana and fluentd

動作確認ブラウザでブラウザでhttp://<Nginxのサーバ接続先>/へアクセスし、Sample Dashboardをクリックすると、access_logのデータが可視化されていることを確認できる。

Page 13: Log analysis by using elasticsearch,kibana and fluentd

感想

細かな動きはまだ把握できてないが、簡単にログを収集し、簡単に可視化するところまでいけるのが魅力。 !Webページへのアクセスだけではなく、ファイアウォールやsshのログを解析し、可視化すれば、どこからのアタックが多いかなど容易に検知することができそう。今度やってみる。