本記事では,NginxのAccessLogとMySQLのSlowQueryLogを可視化してみます.
Fluentdにはリアルタイムにログを収集して活用できることや,様々なログフォーマットの違いを吸収してJSONで同じように扱えることなどのメリットがあります.
導入と設定
- Ubuntu 12.04
- ruby 1.9.3
+------------------------+ +---------------------------------------+
| Nginx ---+ | | |
| | | | |
| +--> Fluentd | =====> | Fluentd --> ElasticSearch --> Kibana3 |
| | | | |
| MySQL ---+ | | |
+------------------------+ +---------------------------------------+
Web Server Log Server
...snip...
http {
log_format ltsv 'time:$time_local\t'
'msec:$msec\t'
'host:$remote_addr\t'
'forwardedfor:$http_x_forwarded_for\t'
'req:$request\t'
'method:$request_method\t'
'uri:$request_uri\t'
'status:$status\t'
'size:$body_bytes_sent\t'
'referer:$http_referer\t'
'ua:$http_user_agent\t'
'reqtime:$request_time\t'
'upsttime:$upstream_response_time\t'
'cache:$upstream_http_x_cache\t'
'runtime:$upstream_http_x_runtime\t'
'vhost:$host';
access_log /var/log/nginx/access.log ltsv;
...snip...
...snip...
slow_query_log=ON
slow_query_log_file=/var/log/mysql/slow_query.log
long_query_time=1
...snip...
# apt-get install openjdk-7-jdk
$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.6.deb
# dpkg -i elasticsearch-0.90.6.deb
# service elasticsearch start
$ sudo -i
# curl -L http://toolbelt.treasure-data.com/sh/install-ubuntu-precise.sh | sh
/etc/td-agent/td-agent.conf
を編集します.なおtd-agentは自前のRubyインタプリタを使用しているため,/usr/lib/fluent/ruby/bin/gem
などtd-agentとともに導入されたgemを使ってプラグインの導入を行ってください(でないと認識されない).
- Web Server
# gem install fluent-plugin-mysqlslowquery
<source>
type tail
format ltsv
path /var/log/nginx/access.log
tag nginx.access
time_format %d/%b/%Y:%H:%M:%S %z
</source>
<source>
type mysql_slow_query
path /var/log/mysql/mysql-slow.log
tag mysql.slow_query
</source>
<match **>
type forward
<server>
host {IP of Log Server}
port 24224
</server>
flush_interval 1s
</match>
- Log Server
# gem install fluent-plugin-elasticsearch
<source>
type forward
port 24224
</source>
<match nginx.*>
index_name adminpack
type_name nginx
type elasticsearch
include_tag_key true
tag_key @log_name
host localhost
port 9200
logstash_format true
flush_interval 3s
</match>
<match mysql.*>
index_name adminpack
type_name mysql
type elasticsearch
include_tag_key true
tag_key @log_name
host localhost
port 9200
logstash_format true
flush_interval 3s
</match>
type stdout
を利用して標準出力を見たり,/var/log/td-agent/td-agent.log
を確認しましょう.
# service td-agent start
git clone
して,/sample
にある設定ファイルを参考に,index.html
にブラウザでアクセスできるようにします.
$ git clone https://github.com/elasticsearch/kibana.git
使い方
http://{IP of Log Server}
にアクセスすると下のようなページが表示されるので,真ん中下あたりの赤丸で囲んでいるLogstash Dashboard
をクリックします.