Kibanaを入れなければならない
システムトラブルでログを追ってくのがつらたんなのでKibanaとやらを入れて可視化して楽にしてきたいと思います。
Kibanaとは
www.elastic.co
See the Value in Your Data
* Flexible analytics and visualization platform
* Real-time summary and charting of streaming data
* Intuitive interface for a variety of users
* Instant sharing and embedding of dashboards
ざくり言うと、いろんなデータを可視化するよというものです。
今回はCentOS 6.7の鯖があったのでそれにぶちこんでいきます。
KIbana入れるにはElasticsearch入れなければならず、ElasticsearchはJavaがいるということでJava入れます。
yum -y install java-1.8.0-openjdk
Elasticsearch入れる
最新版(2.1.1)を入れていきます。
yumで入れられるようですが、GPG入れたりなんなりしなくちゃならないけどドキュメント読んでがんばりましょう。
https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-repositories.html
基本的に書いてあるとおりにやるだけです。
GPGキー
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
/etc/yum.repos.d/ に elasticsearch.repo 作る
ファイル作ったら以下を書き込みます。
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
yum install elasticsearch
ぶったたきます。
yum install elasticsearch
起動確認
動くか確かめましょう。サービスで起動させてみます。
service elasticsearch start
elasticsearch を起動中: [ OK ]
動いたっぽいです。止めるときはstopで。
自動起動は
chkconfig elasticsearch on
やってみる
ドキュメントに Run curl -X GET http://localhost:9200/ って書いてあるのでぶったたいてみます。
curl -X GET http://localhost:9200/
{
"name" : "Jane Foster",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.1.1",
"build_hash" : "40e2c53a6b6c2972b3d13846e450e66f4375bd71",
"build_timestamp" : "2015-12-15T13:05:55Z",
"build_snapshot" : false,
"lucene_version" : "5.3.1"
},
"tagline" : "You Know, for Search"
}
よくわからないけど動いてるようです。
そもそもElasticsearchがなんなのかよくわかってないですが、ぐぐってみると要は全文検索エンジンで、REST APIでデータを入出力できるので便利ですという感じっぽいです。
なのでログ解析とかに向いてるってことですかね。
それをKibanaで可視化するという感じっぽいです。
Kibana入れる
wgetで落として解凍しましょう。
wget https://download.elastic.co/kibana/kibana/kibana-4.3.1-linux-x64.tar.gz
tar -xvzf kibana-4.3.1-linux-x64.tar.gz
config/kibana.ymlいじる
ドキュメントに
* Open config/kibana.yml in an editor
* Set the elasticsearch.url to point at your Elasticsearch instance
と書いてあるのでそのとおりにやりましょう。
起動
./bin/kibana
log [10:37:54.770] [info][status][plugin:kibana] Status changed from uninitialized to green - Ready
log [10:37:54.790] [info][status][plugin:elasticsearch] Status changed from uninitialized to yellow - Waiting for Elasticsearch
log [10:37:54.800] [info][status][plugin:kbn_vislib_vis_types] Status changed from uninitialized to green - Ready
log [10:37:54.803] [info][status][plugin:markdown_vis] Status changed from uninitialized to green - Ready
log [10:37:54.809] [info][status][plugin:metric_vis] Status changed from uninitialized to green - Ready
log [10:37:54.814] [info][status][plugin:spyModes] Status changed from uninitialized to green - Ready
log [10:37:54.821] [info][status][plugin:statusPage] Status changed from uninitialized to green - Ready
log [10:37:54.830] [info][status][plugin:table_vis] Status changed from uninitialized to green - Ready
log [10:37:54.843] [info][listening] Server running at http://0.0.0.0:5601
log [10:37:59.853] [info][status][plugin:elasticsearch] Status changed from yellow to yellow - No existing Kibana index found
log [10:38:04.378] [info][status][plugin:elasticsearch] Status changed from yellow to green - Kibana index ready
なんか動いてるっぽいですね。
ブラウザで localhost:5601 にアクセスしてみると
動いてるっぽいですね。
ApacheのログをKibanaでみる
ぐぐってみるとどうやらこういうことのようです。
Apache -> td-agent(fluentdのいろいろ入ってるやつ) -> Elasticsearch -> Kibana
なのでtd-agent入れていきます。
td-agent入れる
ドキュメントみて入れていきましょう。
docs.fluentd.org
Before Installation
インスコするまえにやっとけということでやっておきましょう。
docs.fluentd.org
入れる
入れましょう。
curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh
起動
立ち上げましょう。
/etc/init.d/td-agent start
Starting td-agent: [ OK ]
ドキュメントみると以下がサポートされてます。とりあえずstartしましょう。
$ /etc/init.d/td-agent start
$ /etc/init.d/td-agent stop
$ /etc/init.d/td-agent restart
$ /etc/init.d/td-agent status
試す
ドキュメントに書いてあるのでとりあえず書いてみましょう。
curl -X POST -d 'json={"json":"message"}' http://localhost:8888/debug.test
デフォルトだと /var/log/td-agent/td-agent.log に書いてあるのでみてみると
2015-12-20 11:33:22 +0900 debug.test: {"json":"message"}
書いてありましたね!
fluent-plugin-elasticsearch入れる
Fluentdで受けたログをElasticSearchに保存するプラグイン入れます。
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-elasticsearch
ApacheのログをElasticSearchに書く
そのものずばりのすばらしいページがあるのでこの通り書きましょう。
http://enomotodev.github.io/post/install-elasticsearch/
http://enomotodev.github.io/post/install-fluentd/
こんな感じで書きました。
/etc/td-agent/td-agnet.conf
<source>
# 入力に in_tail プラグインを指定
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 elasticsearch
type_name access_log
host localhost
port 9200
logstash_format true
logstash_prefix apache-log
logstash_dateformat %Y%m%d
include_tag_key true
tag_key @log_name
flush_interval 10s
</match>
確認
/etc/init.d/td-agent restart
curl -XGET http://localhost:9200/_aliases?pretty
{
".kibana" : {
"aliases" : { }
},
"apache-log-20151220" : {
"aliases" : { }
}
}
こんなのが返ってくれば大丈夫です。
Kibanaでみる
この通りにしましょう。
Index name or pattern に apache-log-* を入れれば表示されるはずです。
http://enomotodev.github.io/post/install-elasticsearch
MySQLのスロークエリを可視化する
とりあえずスロークエリ吐き出すようにしておきましょう。
my.cnfに追加
slow_query_log=1
long_query_time=0.5
slow_query_log_file=/etc/httpd/logs/slow_query.log
log_queries_not_using_indexes=1
service mysqld restart とかしといてください。
fluent-plugin-mysqlslowquery入れる
入れましょう。
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-mysqlslowquery
/etc/td-agent/td-agnet.conf いじる
とりあえずこんな感じにしときます。
<source>
type mysql_slow_query
path /etc/httpd/logs/slow_query.log
tag mysql.slow_query
</source>
<match mysql.slow_query>
type elasticsearch
type_name slow_query_log
host localhost
port 9200
logstash_format true
logstash_prefix slowquery-log
logstash_dateformat %Y%m%d
include_tag_key true
tag_key @log_name
flush_interval 1s
</match>
あとは Kibana の Index name or pattern に slow_query_log* とか入れると出てくるはずです。
まとめ
とりあえず動かすこと重点でパラメータの意味とかまだわからないですが、とりあえず動いたのでこれからいろいろいじっていきたいなと思いましたね。
こちらからは以上です。