embulk使ってみる
fluentd + elasticsearch + kibanaをやってみたいなと思い既存のログからデータ突っ込めないかなと思い探してみたらちょうどよさそうだったので試してみました。
fluentdのバッチ版と言われているようです
手順もほとんど下記見ながらやればできます
インストール
curl -o ~/bin/embulk -L "http://dl.embulk.org/embulk-latest.jar" chmod +x ~/bin/embulk
サンプルデータを入れる
ひな型生成
example サブコマンドでサンプルデータができます
# サンプルのひな型生成 bin/embulk example ./sample # sample/example.ymlの中身を読み込んで自動的に設定ファイルを作成してくれる bin/embulk guess sample/example.yml -o config.yml # プレビュー bin/embulk preview config.yml
config.ymlの中身変更
中身はin,out,execの3つにわかれているよう
fluentdの設定と似てますね
inでCSVファイルを取り込みoutで出力
サンプルのデフォルトはstdoutになってます
- 「out」セクションをelasticsearchにします
githubの例文を見ながら設定したけどindex_typeが何か分かっていないので取りあえず適当につけた
out:
type: elasticsearch
nodes:
- {host: localhost, port: 9300}
index: <index name>
index_type: <index type>
- プラグインのインストール
embulk gem install embulk-output-elasticsearch
- いざrun!!!
怒られました
Unsupported major.minor version 52.0
どうやらjavaのバージョンが足りないようです。
java1.8にして再び挑戦!
embulk run config.yml
今度は無事入りました
既存のログを突っ込んでみる
nginxのアクセスログを突っ込もうと思ったけど適当そうなプラグインを見つけることができなかったので一旦tsvに変換してCSVプラグインを使うことに
なので、nginxのログをtsvに変換するスクリプトを書いて対応
生成したtsvに対して下記configでドン!
in:
type: file
path_prefix: /home/vagrant/sample/log/
parser:
charset: UTF-8
newline: CRLF
type: csv
delimiter: "\t"
escape: ''
columns:
- {name: remote_ip, type: string}
- {name: time, type: timestamp, format: '%d/%b/%Y:%H:%M:%S'}
- {name: method, type: string}
- {name: path, type: string}
- {name: protocol, type: string}
- {name: code, type: long}
- {name: byte, type: long}
- {name: referer, type: string}
- {name: ua, type: string}
- {name: response, type: double}
- {name: remote_ip, type: string}
- {name: cookie, type: string}
exec: {}
out:
type: elasticsearch
nodes:
- {host: localhost, port: 9300}
index: embulk_access_log
index_type: embulk
無事入りました
fluent-plugin-stdin
fluentdのプラグインでもできるみたいです
試しにnginxのアクセスログを突っ込んでみます
- stdin.conf
<source> type stdin format /^(?<host>[^ ]*) [^ ]* (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?<request_time>[^ ]*)" "(?<forwarded_for>[^\"]*)" "(?<cookie>[^\"]*)")?$/ time_format %d/%b/%Y:%H:%M:%S %z </source> <match stdin.**> type elasticsearch type_name stdin.test host 192.168.20.13 port 9200 logstash_format true logstash_prefix logstash flush_interval 5s </match>
直接コマンドを打ちます
cat access_log | /usr/sbin/td-agent -c /etc/td-agent/stdin.conf -q --no-supervisor
どちらの方法でも大丈夫そう
これでばっちり後入れ可能になりました
あとはグラフいじってみよう