Carpe Diem

備忘録

NginxのアクセスログをKibanaで可視化

概要

Nginxのアクセスログを用いて可視化の流れをまとめます。
mappingは手動で設定します。

環境

  • Ubuntu 14.04
  • fluentd 0.12.19
  • fluent-plugin-elasticsearch 1.3.0
  • Nginx 1.4.6
  • Elasticsearch 2.1.1
  • Kibana 4.3.1

構成

IP 名前 役割
192.168.33.100 web Webサーバ。ログは転送
192.168.33.101 kibana Kibana。転送されたログをKibanaへ

この構成のためにVagrantfileを以下のように編集します。

  config.vm.define :web do |web|
    web.vm.network :private_network, ip: "192.168.33.100"
    web.vm.provider "virtualbox" do |vb|
      vb.memory = "1024"
    end
  end

  config.vm.define :kibana do |kibana|
    kibana.vm.network :private_network, ip: "192.168.33.101"
    kibana.vm.provider "virtualbox" do |vb|
      vb.memory = "2048"
    end
  end

Webサーバの用意

Nginxインストール

$ sudo aptitude install nginx

Nginxログのパーミッション設定

$ sudo chmod o+rx /var/log/nginx/
$ sudo chmod o+r /var/log/nginx/access.log

fluentdのインストール

$ curl -L https://td-toolbelt.herokuapp.com/sh/install-ubuntu-trusty-td-agent2.sh | sh

fluentdの設定

<source>
  @type tail
  path /var/log/nginx/access.log
  pos_file /var/log/td-agent/nginx.access.log.pos
  format nginx
  tag nginx.access
</source>

<match nginx.access>
  @type forward
  buffer_type memory
  buffer_chunk_limit 8m
  buffer_queue_limit 64
  flush_interval 1s
  <server>
    host 192.168.33.101
    port 24224
  </server>
</match>

設定したら再起動しておきます。

$ sudo service td-agent restart

Kibanaサーバの用意

Kibanaインストール

Elasticsearch2.1 × Kibana4.3 の導入を参考にインストールします。

Elasticsearchのmapping設定

templateを設定します。ポイントは以下です。

  • "template": "nginx_access-*"nginx_access-*のインデックスに対して自動で以下のmappingを適用
  • 不要なフィールドは"dynamic": falseで設定させない
  • ほぼ決まっている値はnot_analyzedで解析コストを減らす
  • sizecodeなどの数字はintegerで解析コストを減らす

上記を元に以下のリクエストを投げます。

curl -XPUT "http://localhost:9200/_template/template_nginx_access" -d '
{
  "mappings": {
    "nginx_access": {
      "dynamic": false,
      "properties": {
        "path": {
          "index": "not_analyzed",
          "type": "string"
        },
        "referer": {
          "type": "string"
        },
        "agent": {
          "type": "string"
        },
        "@timestamp": {
          "format": "strict_date_optional_time||epoch_millis",
          "type": "date"
        },
        "code": {
          "type": "integer"
        },
        "method": {
          "index": "not_analyzed",
          "type": "string"
        },
        "size": {
          "type": "integer"
        },
        "host": {
          "type": "string"
        },
        "remote": {
          "type": "string"
        }
      }
    }
  },
  "template": "nginx_access-*"
}
'

fluentdのインストール

$ curl -L https://td-toolbelt.herokuapp.com/sh/install-ubuntu-trusty-td-agent2.sh | sh

プラグインのインストール

$ sudo td-agent-gem install fluent-plugin-elasticsearch

fluentdの設定

<source>
  @type forward
  port 24224
</source>

<match nginx.access>
  @type elasticsearch
  host 127.0.0.1
  port 9200
  type_name nginx_access
  logstash_format true
  logstash_prefix nginx_access
  logstash_dateformat %Y.%m.%d

  buffer_type memory
  buffer_chunk_limit 8m
  buffer_queue_limit 64
  flush_interval 1s
</match>

通常の設定とは異なる部分のみ簡単に説明すると以下です。

項目 役割
type_name elasticsearchのindexのtype名 nginx_access
logstash_format logstashのフォーマットと互換性を持たせる。
Kibanaでは推奨設定
true
logstash_prefix 保存するインデックス名のprefix
デフォルトはlogstash
nginx_access
logstash_dateformat 通常だとlogstash-YYMMDDのフォーマットでインデックスが保存される
今回だとnginx_access-%Y.%m.%d
%Y.%m.%d

設定したら再起動しておきます。

$ sudo service td-agent restart

設定が正しいとアクセスログが流し込まれるので、以下のように自動でインデックスが作成されdocsが増えていきます。

f:id:quoll00:20160113144040p:plain

Dashboard作成

まずはどのインデックスをグラフ化するかを設定します。以下のようにインデックスの名称を入力して読み込ませます。

f:id:quoll00:20160113144135p:plain

読み込みができるとDiscoverで検索できるようになります。

f:id:quoll00:20160113144841p:plain

総リクエスト数

VisualizeLine chartを選択して以下のように作ります。

f:id:quoll00:20160113144738p:plain

すると以下の様なグラフができます。

f:id:quoll00:20160113144857p:plain


path毎のリクエスト数

次はpath毎に分けます。分けるときはbucketを利用します。

f:id:quoll00:20160113144923p:plain

すると以下の様なグラフができます。

f:id:quoll00:20160113144938p:plain

pathの割合

割合はPie chartという円グラフを利用します。

f:id:quoll00:20160113145024p:plain

すると以下の様なグラフができます。

f:id:quoll00:20160113145035p:plain


status codeの割合

status codeはcodeを元にグラフを作ります。

f:id:quoll00:20160113145102p:plain

以下の様なグラフになります。

f:id:quoll00:20160113145115p:plain


ソース