Skip to content

k2works/logging_introduction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ログ収集・可視化入門

目的

ログ収集・分析環境を構築できるようになる。

前提

ソフトウェア バージョン 備考
OS X 10.8.5
vagrant     1.6.3
fluentd     0.10.5
Elasticsearch 1.3.2
JDK 1.7.0
kibana     3.1.0

構成

詳細

サンプルアプリケーションのインストール

$ gem install rails -v 4.1.4
$ gem install spree
$ spree install my_store -A
$ cd my_store/
$ bundle install
$ rails g spree:install

起動時エラーを回避するため_Gemfile.rock_を編集して_bundle install_

    sprockets-rails (2.1.3)

店舗画面

http://localhost:3000/

管理画面

http://localhost:3000/admin/

アカウント: spree@example.com
パスワード: spree123

サンプルシステムのプロビジョニング

$ cd cookbooks/case01/
$ vagrant up
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

上記のメッセージが出てプロビジョニングに失敗した場合は以下の作業でネームサーバを更新する。

$ vagrant ssh host1
$ echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null
$ exit
$ vagrant provision host1
・・・
$ vagrant up host2

本番環境へのデプロイ

$ cd my_store
$ cap production deploy
$ ssh vagrant@192.168.33.10
vagrant@192.168.33.10's password:vagrant
$ cd /var/www/my_store
$ RAILS_ENV=production rake db:seed
$ RAILS_ENV=production rake spree_sample:load
$ touch tmp/restart.txt

サンプルシステム構成

サーバ IPアドレス 備考
アプリケーションサーバ 192.168.33.10
ロギングサーバ 192.168.33.20

ECサイト
http://192.168.33.10

ECサイト管理画面
http://192.168.33.10/admin

Fluentd-ui
http://192.168.33.10:9292

Elasticsearch
http://192.168.33.20:9200/_plugin/kopf

Kibana
http://192.168.33.20/#/dashboard/file/guided.json

td-agent動作確認

$ cd cookbooks/case02
$ vagrant up
$ vagrant ssh
$ echo '{"message":"Hello World."}' | /usr/lib64/fluent/ruby/bin/fluent-cat debug.test
$ tail /var/log/td-agent/td-agent.log
・・・
4-09-11 09:07:48 +0000 debug.test: {"message":"Hello World."}

fluentd-uiを使う

$ sudo /etc/init.d/fluentd-ui start

_http://192.168.33.10:9292/_にアクセスする。

001

アカウント名:admin パスワード:changeme

002

td-agentをセットアップを選択

003

作成

004

クックブックの構成

cookbooks/case02/Vagrantfile

chef.run_list = %w[
  recipe[logging-introduction-case02::default]
  recipe[logging-introduction-case02::base]
  recipe[logging-introduction-case02::app]
  recipe[logging-introduction-case02::app_config]
  recipe[logging-introduction-case02::rvm_config]
  recipe[logging-introduction-case02::fluentd_config]
  recipe[logging-introduction-case02::fluentd-ui]
]

cookbooks/case02/recipes/default.rb

仮想マシン共通セットアップ

cookbooks/case02/recipes/base.rb

td-agentインストール
その他必要なパッケージ

cookbooks/case02/recipes/app.rb

Webサーバー・DBサーバーインストール

cookbooks/case02/recipes/app_config.rb

Webサーバー・DBサーバー設定

cookbooks/case02/recipes/rvm_config.rb

rvm設定

cookbooks/case02/recipes/fluentd_config.rb

td-agent設定

cookbooks/case02/recipes/fluentd-ui.rb

fluentd-uiインストール・デーモン設定

Fluentdの設定ファイル

systemディレクティブ

<system>
 # ログ出力レベルを設定(trace, debug,info,warn,error,fatal)
 log_level info
 # 連続した同一エラー出力を抑制する
 suppress_repeated_stacktrace true
 # 指定時間内の同一エラー出力を制御する
 emit_error_log_interval              60s
 # 起動時に設定ファイルの標準ログ出力を制御する
 suppress_config_dump             true
</system>

sourceディレクティブ

<source>
  # プラグインを指定
  type tail
  # 読み込むログファイルを指定
  path /var/log/messages
  # タグ指定
  ## Fluentdプラグイン間でメッセージをルーティングする際の識別に利用
  tag system.messages
  # ログフォーマット定義
  ## 正規表現,apache,apache2,nginx,syslog,tsv,csv,ltsv,json,
  ## none(行全体をmessageキーの値として取り込む場合)のいづれかを指定
  format syslog
  time_format %b %d %H:%M:%S
  pos_file /tmp/fluentd--1410421801.pos
</source>

matchディレクティブ

# systemにマッチするタグをFluentdの標準出力ログに出力する設定
<match system.*>
  type stdout
</match>

includeディレクティブ

# 絶対パスでの記述
include /path/to/config.conf

# 相対パスで記述する場合には、呼び出し元の設定ファイルが
# 置かれたディレクトリを起点としてファイルを読み込みます
include extra.conf

# 複数ファイルの読み込みはワイルドカード指定を使います
include config.d/*.conf

# 外部URLから設定を読み込みます
include http://example.com/fluentd.conf

起動の確認

$ cd cookbooks/case03
$ vagrant up
$ vagrant ssh
$ $ curl -XGET http://localhost:9200/
{
  "ok" : true,
  "status" : 200,
  "name" : "case01",
  "version" : {
    "number" : "0.90.12",
    "build_hash" : "26feed79983063ae83bfa11bd4ce214b1f45c884",
    "build_timestamp" : "2014-02-25T15:38:23Z",
    "build_snapshot" : false,
    "lucene_version" : "4.6"
  },
  "tagline" : "You Know, for Search"
}

クラスタの状態を確認

$ curl -XGET http://localhost:9200/_cluster/health?pretty
{
  "cluster_name" : "elasticsearch",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0
}

設定

環境変数

cookbooks/case03/berks-cookbooks/elasticsearch/templates/default/elasticsearch-env.sh.erb

システム設定

ファイルディスクリプタ

$ cat /etc/security/limits.d/10-elasticsearch.conf
elasticsearch     -    nofile    64000
elasticsearch     -    memlock   unlimited

Elasticsearchの設定

cookbooks/case03/berks-cookbooks/elasticsearch/templates/default/elasticsearch.yml.erb

インデックスとデータの操作

インデックスの作成

$ curl -XPOST http://localhost:9200/test_index
{"ok":true,"acknowledged":true}

データの取り込み

$ curl -XPUT http://localhost:9200/test_index/apache_log/1 -d '
{
"host":"localhost",
"timestamp": "06/May/2014:06:11:48 +0000",
"verb": "GET",
"request": "/category/finace",
"httpversion": "1.1",
"response": "200",
"bytes": "51"
}
'
{"ok":true,"_index":"test_index","_type":"apache_log","_id":"1","_version":1}

データの削除

$ curl -XDELETE http://localhost:9200/test_index/apache_log/1
{"ok":true,"found":true,"_index":"test_index","_type":"apache_log","_id":"1","_version":2}

検索

全件検索

$ curl -XGET http://localhost:9200/test_index/_search -d '
> {
> "query" : {
> "match_all" : {}
> }
> }
> '
{"took":65,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test_index","_type":"apache_log","_id":"1","_score":1.0, "_source" :
{
"host":"localhost",
"timestamp": "06/May/2014:06:11:48 +0000",
"verb": "GET",
"request": "/category/finace",
"httpversion": "1.1",
"response": "200",
"bytes": "51"
}
}]}}

query string query

$ curl -XGET http://localhost:9200/test_index/_search -d '
> {
> "query" : {
> "query_string" : {
> "query" : "request:/category/finace AND response:200"
> }
> }
> }
> '
{"took":72,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0253175,"hits":[{"_index":"test_index","_type":"apache_log","_id":"1","_score":1.0253175, "_source" :
{
"host":"localhost",
"timestamp": "06/May/2014:06:11:48 +0000",
"verb": "GET",
"request": "/category/finace",
"httpversion": "1.1",
"response": "200",
"bytes": "51"
}
}]}}

ファセット

$ curl -XGET http://localhost:9200/test_index/_search -d '
> {
> "query" : {
> "match_all" : {}
> },
> "facets" : {
> "request_facet" : {
> "terms" : {
> "field" : "request",
> "size" : 10
> }
> }
> }
> }
> '
{"took":26,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test_index","_type":"apache_log","_id":"1","_score":1.0, "_source" :
{
"host":"localhost",
"timestamp": "06/May/2014:06:11:48 +0000",
"verb": "GET",
"request": "/category/finace",
"httpversion": "1.1",
"response": "200",
"bytes": "51"
}
}]},"facets":{"request_facet":{"_type":"terms","missing":0,"total":2,"other":0,"terms":[{"term":"finace","count":1},{"term":"category","count":1}]}}}

ファセットタイプ

タイプ 説明
terms インデックスお値ごとにドキュメント数を集計
range インデックスの値を元に指定された範囲ごとにドキュメント数を集計
histogram インデックスの数値データを元に指定された間隔ごとにドキュメント数を集計
statistical インデックスの数値フィールドの統計値[min,max,ドキュメント数]
query 指定されたクエリのドキュメント数を集計

インデックスの削除

$ curl -XDELETE http://localhost:9200/test_index
{"ok":true,"acknowledged":true}

マッピング定義

マッピングの指定

$ curl -XPUT http://localhost:9200/test_index -d '
> {
> "mappings": {
> "apache_log": {
> "properties": {
> "request": {
> "type": "string",
> "index": "not_analyzed"
> }
> }
> }
> }
> }
> '

マッピングの確認

$ curl -XGET http://localhost:9200/test_index/_mapping
{"test_index":{"apache_log":{"properties":{"request":{"type":"string","index":"not_analyzed","norms":{"enabled":false},"index_options":"docs"}}}}}

インデックス管理ツールとプラグイン

Curator

インストール

$ sudo pip install elasticsearch-curator
$ sudo pip install argparse

バージョンの確認

$ curator -v
curator 1.2.2

日数を指定した削除

$ curator --host localhost delete --older-than 30

容量を指定した削除

$ curator --host localhost delete --disk-space 1024

日数を指定したクローズ

$ curator --host localhost close --older-than 7

Bloom filter無効化

$ curator --host localhost bloom --older-than 2

日数を指定したオプティマイズ

$ curator --host localhost optimize --older-than 2

プラグイン

elasticserch-head

$ plugin -i mobz/elasticsearch-head

http://192.168.33.10:9200/_plugin/head/

005

elasticsearch-kopf

$ plugin -i lmenezes/elasticsearch-kopf

http://192.168.33.10:9200/_plugin/kopf/

006

marvel

$ plugin -i elasticsearch/marvel/latest
$ sudo /etc/init.d/elasticsearch restart

http://192.168.33.10:9200/_plugin/marvel/

007

サンプルデータのインポート
サーバ/インフラエンジニア養成読本 Kibanaテストデータを参考にローカルでデータを作成したデータを登録する。

$ curl -s -XPOST 192.168.33.10:9200/_bulk --data-binary @events.json > /dev/null

以下のページをブラウザで指定する。

http://192.168.33.10/#/dashboard/file/guided.json

008

参照

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published