Kibana4 + dstat の話
あらまし
以前に、『dstatをkibanaで可視化+3.0.0milestone5新機能』という エントリでKibana3+dstatを使った可視化について書いたのですが、この中で構築の肝としてfluent-plugin-mapがありました。 Kibana3だと同一グラフ内に同一レコードの複数のfieldを含めることが出来なかったので、 fluent-plugin-mapを使って同一@timestamp+各fieldという別々のレコードとして分割登録しました。 このやり方は@kazeburoさんも記事にされたことにより 同様の方法を利用する人が意外といるようなのですが、 Kibana4では同一グラフ内に同一レコードの複数fieldを含めることが出来るのでレコード分割する必要がありません。 性能はわかりませんがdocument数や容量は分割する場合と比べると減ると思います。 ただしfluent-plugin-dstatはネストしたjsonを返すので平らにしてあげる必要があってfluent-plugin-flatten-hashを使います。
設定とか
fluentdの設定は以下。
<source> @type dstat tag dstat option -ams delay 5 </source> <match dstat> @type flatten_hash add_tag_prefix flattened. separator _ </match> <match flattened.dstat> @type elasticsearch type_name dstat host localhost port 9200 logstash_format true logstash_prefix dstat flush_interval 3s </match
それからMapping template
$ vi template_dstat.json { "template": "dstat-*", "mappings": { "dstat": { "properties": { "@timestamp" : { "type" : "date", "format" : "dateOptionalTime" }, "dstat_dsk/total_read" : { "type" : "double" }, "dstat_dsk/total_writ" : { "type" : "double" }, "dstat_memory_usage_buff" : { "type" : "double" }, "dstat_memory_usage_cach" : { "type" : "double" }, "dstat_memory_usage_free" : { "type" : "double" }, "dstat_memory_usage_used" : { "type" : "double" }, "dstat_net/total_recv" : { "type" : "double" }, "dstat_net/total_send" : { "type" : "double" }, "dstat_paging_in" : { "type" : "double" }, "dstat_paging_out" : { "type" : "double" }, "dstat_swap_free" : { "type" : "double" }, "dstat_swap_used" : { "type" : "double" }, "dstat_system_csw" : { "type" : "double" }, "dstat_system_int" : { "type" : "double" }, "dstat_total_cpu_usage_hiq" : { "type" : "double" }, "dstat_total_cpu_usage_idl" : { "type" : "double" }, "dstat_total_cpu_usage_siq" : { "type" : "double" }, "dstat_total_cpu_usage_sys" : { "type" : "double" }, "dstat_total_cpu_usage_usr" : { "type" : "double" }, "dstat_total_cpu_usage_wai" : { "type" : "double" }, "hostname" : { "type" : "string", "index" : "not_analyzed" } } } } } $ curl -XPUT 'localhost:9200/_template/tmptelate_dstat' -d @template_dstat.json
templateは特定のパスに置いておくと自動的に読まれるのですが、
再起動が必要になるので最近はtemplate APIを呼ぶようにしてます。
変更結果を確認もできますし。
(既存のindex mappingが即時変更されるわけではありません)
mappingでなくfluent-plugin-typecastを使って先に型を定義してしまう方法もあります。
私はmappingでdoc_valuesやStringにnot_analyzedを設定することが多いのでElasticsearchでは共通的にmapping作成するようにしてます。
mappingを作るにはまず一旦設定なしでデータを入れてみてできたmappingを見て変更するとわりと簡単にできます。
curl localhost:9200/<index>?pretty
Kibana4の設定
Visualize Line chartの設定画面です。
add metricsでY軸を複数設定できます。
複数表示できましたね。
cpuやmemory, swapはArea chartの方がいいかもしれませんね。
設定方法は一緒です。
Vertical bar chartなんてのも。
レコード分割せずに複数のfieldを同一グラフに表示できました。
結論
dstat + fluentd + kibana4(Elasticsearh)ではレコード分割する必要がない。
(ただし構成上の話で性能はわかりません)
sample
github.com ansible-hands-onを再利用して構築検証用Vagrantfile作りました。
番外
grafana + influxdbの場合はレコード分割が必要(?自力では調べてない)なので その場合はwinebarrelさんの記事を参考にするといいと思います。