Fluentd + ElasticSearch + Kibana3でグラフ化
前々回にElastcSearchをインストールしましたが、
今回はFluentdとKibana3をインストールしてグラフ化してみます。
まずはFluentdのインストールメモ。
※参考
http://docs.fluentd.org/categories/installation
■Fluentdインストール
[root@onedari01 ~]# curl -L http://toolbelt.treasuredata.com/sh/install-redhat.sh | sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 672 100 672 0 0 2355 0 --:--:-- --:--:-- --:--:-- 0
This script requires superuser access to install rpm packages.
You will be prompted for your password by sudo.
Loaded plugins: product-id, security, subscription-manager
Updating Red Hat repositories.
pgdg92 | 2.1 kB 00:00
treasuredata | 951 B 00:00
treasuredata/primary | 10 kB 00:00
treasuredata 29/29
Skipping security plugin, no data
Setting up Update Process
No Packages marked for Update
Loaded plugins: product-id, security, subscription-manager
Updating Red Hat repositories.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package td-agent.i386 0:1.1.18-0 set to be updated
--> Processing Dependency: td-libyaml for package: td-agent
--> Processing Dependency: libyaml-1024.so.4 for package: td-agent
--> Running transaction check
---> Package td-libyaml.i386 0:0.1.4-1 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================================
Package Arch Version Repository Size
================================================================================================
Installing:
td-agent i386 1.1.18-0 treasuredata 63 M
Installing for dependencies:
td-libyaml i386 0.1.4-1 treasuredata 123 k
Transaction Summary
================================================================================================
Install 2 Package(s)
Upgrade 0 Package(s)
Total download size: 63 M
Downloading Packages:
(1/2): td-libyaml-0.1.4-1.i386.rpm | 123 kB 00:00
(2/2): td-agent-1.1.18-0.i386.rpm | 63 MB 01:55
------------------------------------------------------------------------------------------------
Total 551 kB/s | 63 MB 01:56
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : td-libyaml 1/2
Installing : td-agent 2/2
adding 'td-agent' group...
adding 'td-agent' user...
Installing default conffile ...
prelink detected. Installing /etc/prelink.conf.d/td-agent-ruby.conf ...
Configure td-agent to start, when booting up the OS...
duration: 169(ms)
Installed products updated.
Installed:
td-agent.i386 0:1.1.18-0
Dependency Installed:
td-libyaml.i386 0:0.1.4-1
Complete!
[root@onedari01 ~]#
[root@onedari01 ~]# /etc/init.d/td-agent start
Starting td-agent: [ OK ]
[root@onedari01 ~]#
[root@onedari01 ~]#
ここまででFluentdを起動できました。
■プラグイン検索方法
[root@onedari01 ~]# /usr/lib/fluent/ruby/bin/fluent-gem search -r fluent-plugin
Fluentdに必要なプラグインを片っ端からインストールします。
■インストール方法
[root@onedari01 ~]# /usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-tail-asis [root@onedari01 ~]# /usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-parser [root@onedari01 ~]# /usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-grepcounter [root@onedari01 ~]# /usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-file-alternative [root@onedari01 ~]# /usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-elasticsearch [root@onedari01 ~]# /usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-geoip
■geoipを使用するための準備
IPアドレスから国を判別する設定を追加します。
参考
http://y-ken.hatenablog.com/entry/fluent-plugin-geoip-v0.0.4
[root@onedari01 ~]# yum install geoip-devel Loaded plugins: security Setting up Install Process No package geoip-devel available. Nothing to do [root@onedari01 ~]# [root@onedari01 ~]# vi /etc/yum.repos.d/git.repo [dag] name=Dag RPM Repository for Redhat EL5 baseurl=http://apt.sw.be/redhat/el5/en/$basearch/dag gpgcheck=1 enabled=1 gpgkey=http://dag.wieers.com/packages/RPM-GPG-KEY.dag.txt [root@onedari01 ~]# [root@onedari01 ~]# yum install geoip-devel 3963 14/01/21 14:52:18 /usr/lib/fluent/ruby/bin/fluent-gem search -r fluent-plugin-geoip 3964 14/01/21 14:52:29 /usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-geoip
yumでのインストールに失敗したので設定してからやり直ししています。
■Fluentdの設定
[root@onedari01 ~]# cd /etc/td-agent/
[root@onedari01 td-agent]# vi td-agent.conf
<source>
type tail_asis
path /var/log/messages
pos_file /tmp/test.pos
tag one.dari01
</source>
<match one.dari01>
type copy
<store>
type parser
add_prefix parsed
format /^(?<time>[^ ]* *[^ ]* [^ ]*) (?<hostname>[^ ]*) (?<user>[^ ]*)\: "(?<ip>[^\"]*)" "(?<BUST>[^\"]*)" "(?<WASTE>[^\"]*)" "(?<HIP>[^\"]*)" (?<message>.*)$/
time_format %b %e %H:%M:%S
key_name message
</store>
</match>
<match parsed.one.dari01>
type copy
<store>
type geoip
geoip_lookup_key ip
enable_key_country_code geoip_country
remove_tag_prefix parsed.
add_tag_prefix es.
</store>
</match>
<match es.one.dari01>
type copy
<store>
index_name adminpack
type_name onedari
type elasticsearch
include_tag_key true
tag_key @log_name
host localhost
port 9200
logstash_format true
flush_interval 3s
</store>
</match>
[root@onedari01 td-agent]# service td-agent restart
Shutting down td-agent: [ OK ]
Starting td-agent: [ OK ]
[root@onedari01 td-agent]#
formatでログの形式を正規表現で指定しています。
format /^(?<time>[^ ]* *[^ ]* [^ ]*) (?<hostname>[^ ]*) ・・・・
ここがうまくできてないとログがでてきません。
■KIBANA3の設定
[root@onedari01 work]# wget https://download.elasticsearch.org/kibana/kibana/kibana-3.0.0milestone4.tar.gz --2014-02-08 17:36:09-- https://download.elasticsearch.org/kibana/kibana/kibana-3.0.0milestone4.tar.gz Resolving download.elasticsearch.org... 174.129.36.224, 54.225.135.194, 2406:da00:ff00::36e1:87c2, ... Conneting to download.elasticsearch.org|174.129.36.224|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 991116 (968K) [application/octet-stream] Saving to: `kibana-3.0.0milestone4.tar.gz' 100%[===========================================================================================================================>] 991,116 419K/s in 2.3s 2014-02-08 17:36:12 (419 KB/s) - `kibana-3.0.0milestone4.tar.gz' saved [991116/991116]
これをApacheのドキュメントルートに設置すればOKです。
■確認
syslogにログを出力してみてKIBANAで表示されるか確認します。
[root@onedari01 logs]# logger \"202.11.122.22\" \"90\" \"53\" \"63\" \"Oh\"
適当にIPアドレスと適当な数字を入れています。
以下のコマンドでインデックスができているかを確認します。
[root@onedari01 logs]# curl http://localhost:9200/_aliases?pretty=1
{
"logstash-2014.02.08" : {
"aliases" : { }
}
}
logstash-2014.xx.xxというものが表示されればFluentdでログをとっていることがわかります。
表示されなければFluentdの正規表現を疑った方がいいです。
KIBANAでは一日ごとにインデックスを作成しているのでlogstash-YYYY.MM.DDというインデックスが
一日ごとにできていきます。
IPアドレスは適当なものを入れていますが、JPとして識別されたようです。
KIBANAの表示で地図を指定すれば国に色がついてどこのIPアドレスか分かって面白いです。
今回はsyslogのメッセージを表示してみましたが、
apacheのアクセスログなんかを表示してみればいいですね。
以上
関連する投稿:
- 2012-01-21:「キレ男さん」100インストール突破!
- 2014-02-01:秒速でElasticsearchをインストールするメモ
- 2013-12-14:Tamper Dataの使い方メモ
- 2014-02-11:Fluentdの正規表現のチェック
- 2013-03-23:NeXposeインストール
