LoginSignup
41

More than 5 years have passed since last update.

MacにKibanaとtd-agentをインストールする

Last updated at Posted at 2014-09-06

ElasticSearchのインストール

brew install elasticsearch
ln -sfv /usr/local/opt/elasticsearch/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.elasticsearch.plist

Kibanaをダウンロード

zipで落としてきてもいいけど、git cloneしてきた。

git clone https://github.com/elasticsearch/kibana.git
cd kibana
npm install
grunt

nginxのインストール

brew install nginx
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

Kibanaが見られるようにする

Kibanaのディレクトリをdocrootにしたserverディレクティブを追加。

vi /usr/local/etc/nginx/nginx.conf
    server {
        listen       9999;
        server_name  localhost;

        location / {
            root   /[kibanaをcloneしてきたパス]/kibana/src;
            index  index.html index.htm;
        }
    }

nginxの再起動。

launchctl stop homebrew.mxcl.nginx
launchctl start homebrew.mxcl.nginx

これで http://localhost:9999/ でKibanaが見られる。

td-agentをインストール

デーモンとして起動するのが簡単そうなtd-agentを使うことにした。

↓ここからダウンロードしてインストール

sudo launchctl load /Library/LaunchDaemons/td-agent.plist

ログは /var/log/td-agent/td-agent.log にある。

fluent-plugin-elasticsearchをインストール

fluentdのpluginはfluent-gemコマンドで入れるらしい。

/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-elasticsearch

/etc/td-agent/td-agent.confにelasticsearch用の設定を追加。

<match foo.bar.*>
  type elasticsearch
  host localhost
  port 9200
  logstash_format true
  logstash_prefix foobar
</match>

logstash_format true logstash_prefix foobar というのがポイントで、これがあることでKibanaでタイムシリーズとしていい感じに見られる。(foobar-YYYY.MM.DD というインデックスができて @timestamp がきちんと入ってくれる)

最後にtd-agentを再起動

sudo launchctl stop td-agent
sudo launchctl start td-agent

(なぜかstartしなくてもstopだけで勝手にstartもしてくれるが)

logstashダッシュボードを見る

Kibanaを開くと、最初に何をやっていいかよくわからないのだが、logstash形式のダッシュボードのサンプルから始めると良い。ここの (Logstash Dashboard) というリンク。

Kibana_3_-_Introduction.png

Kibana_3_-_Logstash_Search.png

Kibana_3_-_Logstash_Search.png

以上

とはいえ、入れるログが無いと楽しくないので、自分の場合はマウスやキーボードのログを入れている。

それについては次の記事で説明する。

書いた→MacのキーボードやマウスのログをKibanaで可視化する

以下はfluent-plugin-elasticsearchを入れるときにハマりかけたログ

/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-elasticsearch

とやると、こんなエラーが。

Building native extensions.  This could take a while...
ERROR:  Error installing fluent-plugin-elasticsearch:
    ERROR: Failed to build gem native extension.

    /opt/td-agent/embedded/bin/ruby extconf.rb
checking for curl-config... yes
checking for rb_thread_blocking_region()... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/opt/td-agent/embedded/bin/ruby
    --with-curl-dir
    --without-curl-dir
    --with-curl-include
    --without-curl-include=${curl-dir}/include
    --with-curl-lib
    --without-curl-lib=${curl-dir}/lib
/opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:541:in `try_link0'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:556:in `try_link'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:742:in `try_func'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:1027:in `block in have_func'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:918:in `block in checking_for'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:351:in `block (2 levels) in postpone'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:321:in `open'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:351:in `block in postpone'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:321:in `open'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:347:in `postpone'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:917:in `checking_for'
    from /opt/td-agent/embedded/lib/ruby/2.1.0/mkmf.rb:1026:in `have_func'
    from extconf.rb:47:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/patron-0.4.18 for inspection.
Results logged to /opt/td-agent/embedded/lib/ruby/gems/2.1.0/extensions/x86_64-darwin-13/2.1.0/patron-0.4.18/gem_make.out

調べてみると、XCodeのCommand Line Toolを入れないといけないとか(入れてた気がするけど)。昔は xcode-select install で良かったらしいが、今はAppleのサイトからインストールしないといけないらしい。

Command Line Toolをインストールしてもずっと同じエラーで失敗してたのだが、

sudo /opt/td-agent/embedded/bin/ruby -rmkmf -e 'have_func("rb_thread_blocking_region")'

とやるとちゃんと成功するのでどうしたもんかと思ってもう一度

sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-elasticsearch

とやると、何故かインストールできた。不思議。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41