創屋ぷれす

NorikraとFluentdを動かしてみた

NorikraとFluentdを動かしてみた

Esperを内部で使用しているNorikraで、動的クエリを使ってみる。
Norikra付属のクライアントの代わりにFluentdでデータを流し込むようにする。

Fluentd
http://docs.fluentd.org/articles/before-install
Norikra
http://www.slideshare.net/tagomoris/rubykaigi-2013-111130

構成としては

  • Norikraサーバー on JRuby
  • Flutend + Norikraクライアントプラグイン

になる模様。今回は1台のサーバ内でそれぞれを稼働させてみた。

実行環境はOracle Virtual Boxを使って、Windows上でUbuntu TLS14.04 の仮想環境を作成済み。

Fluentdのインストール

Fluentdインストール前の設定

Before Installing Fluentd
http://docs.fluentd.org/articles/before-install

ntpdの設定(強く推奨)

ログの時刻が違うとまずいので、ntpdをインストールし、手動で時刻合わせをする
[参考] http://hatcy840.hatenablog.com/entry/2013/09/17/001212

$ sudo apt-get install ntp
$ sudo service ntp stop
$ sudo ntpdate ntp.nict.jp

ntpの設定ファイル /etc/ntp.conf を編集

$ sudo vi /etc/ntp.conf

*.ubuntu.pool.ntp.org をコメントアウトし、server ntp.nict.jp を追加する。

#server 0.ubuntu.pool.ntp.org
#server 1.ubuntu.pool.ntp.org
#server 2.ubuntu.pool.ntp.org
#server 3.ubuntu.pool.ntp.org
server ntp.nict.jp

ntpサービスの再起動

$ sudo service ntp start

ファイルデスクリプタの最大数を増やす

デスクリプタの最大数を ulimit -n で確認

$ ulimit -n
1024

1024だと不十分とのことなので、/etc/security/limits.conf に設定を追記。

$ sudo vi /etc/security/limits.conf
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536

設定後はマシンを再起動する。再度確認。

$ ulimit -n
65536

カーネルパラメータの変更

ネットワーク負荷が高くなると考えられる場合は、カーネルパラメータも変更しておく。
/etc/sysctl.conf ファイルに直接書き込んで再起動して反映するか、sysctl -w 変数=パラメータ で設定する。

$ sudo vi /etc/sysctl.conf
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240    65535

パラメータが設定されているか sysctl -n で確認。

$ sysctl -n net.ipv4.tcp_tw_recycle
1
$ sysctl -n net.ipv4.tcp_tw_reuse
1
$ sysctl -n net.ipv4.ip_local_port_range
10240   65535

設定値については以下を参考
[参考]https://hiroki.jp/2009/11/11/604/
[参考]http://qiita.com/smallpalace/items/b0b351cebc453287e10e
[参考]http://b.l0g.jp/linux/ip-local-port-range/

Fluentdインストール

Treasure Dataのaptレポジトリから td-agent (Fluentd)をインストールする

$ sudo apt-get install curl
$ curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-trusty-td-agent2.sh | sh
...
Installing default conffile /etc/td-agent/td-agent.conf ...
Starting td-agent:  * td-agent
Processing triggers for libc-bin (2.19-0ubuntu6) ...

/etc/init.d/td-agent スクリプトでtd-agentを再起動してステータスを見てみる。

$ sudo /etc/init.d/td-agent restart
$ sudo /etc/init.d/td-agent status
 * td-agent is running

操作コマンド

$ sudo /etc/init.d/td-agent start
$ sudo /etc/init.d/td-agent stop
$ sudo /etc/init.d/td-agent restart
$ sudo /etc/init.d/td-agent status

設定の変更は /etc/td-agent/td-agent.conf で行う。
動作確認のため、初期設定されている http://localhost:8888/ へサンプルデータをポストしてみる。

$ curl -X POST -d 'json={"json":"message"}' http://localhost:8888/debug.test
$ tail /var/log/td-agent/td-agent.log 
2016-03-24 11:46:16 +0900 debug.test: {"json":"message"}

Norikraクライアントのインストール

FluentdにNorikraクライアント(fluentdのplugin)をインストール

$ sudo /usr/sbin/td-agent-gem install fluent-plugin-norikra --no-ri --no-rdoc
Successfully installed fluent-plugin-norikra-0.3.0
1 gems installed

Norikraのインストール

JRubyのインストール

NorikraサーバはJRubyで動作するため、JRubyをインストールする
JRubyはrbenvを使ってインストールする。

ビルドに必要なライブラリをインストールする

$ sudo apt-get install build-essential
$ sudo apt-get install libssl-dev libreadline-dev zlib1g-dev
$ sudo apt-get install git

rbenvをインストールする(git から cloneする)

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ source ~/.bashrc
$ rbenv --version

ruby-buildをインストールする(git から cloneする)

$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

rubyの最新版(2.3.0)をインストールする

$ rbenv install -l
$ rbenv install 2.3.0
$ rbenv global 2.3.0
$ rbenv rehash
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]

jrubyの最新版(9.0.5.0)をインストールする

$ rbenv install jruby-9.0.5.0
$ rbenv global jruby-9.0.5.0
$ rbenv rehash
$ ruby -v
jruby 9.0.5.0 (2.2.3) 2016-01-26 7bee00d OpenJDK 64-Bit Server VM 24.95-b01 on 1.7.0_95-b00 +jit [linux-amd64]

Norikraのインストール

Norikra
http://norikra.github.io/

JRubyに切り替える

$ rbenv global jruby-9.0.5.0
$ rbenv rehash

gem install norikra でインストール

$ gem install norikra --no-ri --no-rdoc
Successfully installed norikra-1.3.1-java
23 gems installed

norikra start でNorikraサーバを起動

$ norikra start
2016-03-24 17:12:25 +0900 [INFO] : thread configurations, engine:{:inbound=>{:threads=>0, :capacity=>0}, :outbound=>{:threads=>0, :capacity=>0}, :route_exec=>{:threads=>0, :capacity=>0}, :timer_exec=>{:threads=>0, :capacity=>0}}, rpc:{:threads=>2}, web:{:threads=>2}
2016-03-24 17:12:25 +0900 [INFO] : logging configurations, level:nil, dir:nil, filesize:nil, backups:nil, bufferlines:nil
2016-03-24 17:12:25 +0900 [WARN] : status file path (--stats) NOT specified
2016-03-24 17:12:25 +0900 [WARN] : TARGETS AND QUERIES WILL NOT BE SAVED ON SHUTDOWN !
2016-03-24 17:12:25 +0900 [INFO] : Loading UDF plugins
2016-03-24 17:12:26 +0900 [INFO] : Loading Listener plugins
2016-03-24 17:12:26 +0900 [INFO] : Listener loaded, name:Norikra::Listener::Stdout
2016-03-24 17:12:26 +0900 [INFO] : Listener loaded, name:Norikra::Listener::Loopback
2016-03-24 17:12:26 +0900 [INFO] : RPC server 0.0.0.0:26571, 2 threads
2016-03-24 17:12:26 +0900 [INFO] : WebUI server 0.0.0.0:26578, 2 threads
2016-03-24 17:12:26 +0900 [INFO] : Norikra server started.

サーバ外からアクセスしたい場合は、iptablesでポートを空けておく

$ sudo iptables -A INPUT -p tcp -m state --state NEW --dport 26578 -j ACCEPT

WEBブラウザで http://{IPアドレス}:26578/ にアクセスしてみる。Norikraの設定画面が表示されればOK
ctrl + c で norikra を停止する。

Norikraをデーモンモードで起動

デーモンモードでnorikraを起動する。

$ sudo mkdir /etc/norikra
$ sudo mkdir /var/log/norikra
$ sudo mkdir /var/run/norikra
$ sudo chmod 777 /etc/norikra
$ sudo chmod 777 /var/log/norikra
$ sudo chmod 777 /var/run/norikra
$ norikra start -Xmx256m -d --stats=/etc/norikra/norikra.json --logdir=/var/log/norikra --pidfile=/var/run/norikra/norikra.pid

Fluented->Norikraの設定

FluentdからNorikra連携のサンプルとして、httpdのaccess.logをfluentdでNorikraに送り、logの内容を集計してみる。

Norikraにターゲットとフィールドを設定

ターゲット登録

デーモンモードでNorikraを実行した状態から、別ターミナルを起動してターゲットを作成する。

$ norikra-client target open httpd_accesslog

WEBGUIで、作成したTargetsにhttpd_accesslogが登録されたことを確認する

フィールド登録

※ターゲットのデータ型(フィールド)を指定する場合。以下の様な方法で設定する。

$ echo '{"host":"string", "ident":"string" , "user":"string" , "request_time":100 , "request_first_line":"string" , "response_status":100 , "response_size":100 , "refer":"string" , "user_agent":"string" , "cookie":"string" }' | norikra-client event send httpd_accesslog

WEBGUIで、作成したTargetsのFieldsに上記が設定されたことを確認する

apacheのインストール

Norikraへ渡すログを出力するために、apacheをインストールする

$ sudo apt-get install apache2

外部から80ポートにアクセスできるようにする。

$ sudo iptables -A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT

fluentdの設定

$ sudo vi /etc/td-agent/td-agent.conf
<source>
  type tail
  format apache2
  path /var/log/apache2/access.log
  pos_file /var/log/td-agent/apache_access.pos
  tag httpd.accesslog
</source>

<match httpd.accesslog>
  type norikra
  norikra localhost:26571
  retry_limit 3
  target_map_tag    true
</match>

設定を反映

$ sudo service td-agent reload
Reloading td-agent:  * td-agent

ログの読み取りパーミッションを追加

$ sudo chmod o+r /var/log/apache2
$ sudo chmod o+r /var/log/apache2/access.log

WEB GUIでクエリ名 hosst_count として登録

SELECT host, COUNT(*)
FROM httpd_accesslog.win:time_batch(1 min)
GROUP BY host

WEBブラウザからNorikraサーバのlocalhost(またはサーバIP)にアクセスし、apacheにアクセスログを出力させる。

WEBUIでイベントが処理されていることを確認する。

Fluented->Norikra->Fluentd->メール送信

クエリ条件にマッチしたイベントについて、メールを送信してみる。

[参考] http://blog.picomon.jp/tech/?p=485

fluentdの設定

fluentdに to_s, mail プラグインをインストールする

$ sudo /usr/sbin/td-agent-gem install fluent-plugin-to_s --no-ri --no-rdoc
Fetching: fluent-plugin-to_s-0.0.3.gem (100%)
Successfully installed fluent-plugin-to_s-0.0.3
1 gem installed
$ sudo /usr/sbin/td-agent-gem install fluent-plugin-mail --no-ri --no-rdoc
Fetching: fluent-plugin-mail-0.2.4.gem (100%)
Successfully installed fluent-plugin-mail-0.2.4
1 gem installed

fluentdの設定を追加する。norikraからのイベントを受け取って、メールを送信する設定。

<source>
  type    norikra
  norikra Norikra接続先

  <fetch>
    method     sweep
    tag        query_name
    tag_prefix norikra.query
    interval   5s
  </fetch>
</source>
<match norikra.query.alert.**>
  type to_s
  tag_prefix mail
  field_name detail
</match>
<match mail.norikra.query.alert.**>
  type     mail
  host     localhost
  user     USERNAME( ex. hoge@gmail.com)
  password PASSWORD
  to       メール送信先アドレス
  subject  ALERT from Norikra
  out_keys tag,detail
</match>

fluentd を reloadではなく再起動(restart)してpluginを認識させる

$ sudo service td-agent restart

Norikraの設定

WEB GUIでクエリ名 alert.host_count として登録

SELECT host, COUNT(*)
FROM httpd_accesslog.win:time_batch(1 min)
GROUP BY host
HAVING COUNT(*) > 10

確認

WEBブラウザからNorikraサーバのlocalhost(またはサーバIP)に何回か連続でアクセスし、apacheにアクセスログを出力させる。

メールが送信されてくれば成功
メールが来ない場合は、flutendのログ、norikraのログを確認する

創屋のホームページはこちらから

Comments are closed.