インフラエンジニア徒然日記

なんちゃってインフラエンジニアです。てきとーに日々のことを記載します。

fluentdを検証してみた

 

fluentdを検証してみたのでメモ書きとして残します。

・OS情報
RHEL7.1
カーネルバージョン:3.10.0-229.7.2.el7.x86_64

fluentdサーバ側:172.16.1.36
fluetndクライアント側:172.16.1.129


★★サーバ側設定

#curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh
#cd /etc/td-agent
#cp -p td-agent.conf td-agent.conf-`date -I`
#vim td-agent.conf

以下設定を投入
######################################################################
<source>
type forward
port 24224
bind 0.0.0.0
</source>

<match test.messages>
type copy #複数の処理を行いたい場合は、copyプラグインを使用する
<store>
type file #ファイルを作成するプラグインを使用する
path /var/log/td-agent/logs/messages #ファイルのパスを指定する
time_slice_format %Y%m%d #ファイル名の path 以降に追加される時間のフォーマット形式
time_slice_wait 10m #バッファをフラッシュするまでの待ち時間
flush_interval 60m #データフラッシュの間隔
flush_at_shutdown true #shutdown実行時にバッファからデータを出力する
format out_file #バッファをファイルに書き出すプラグインを使用
buffer_chunk_limit 32m #バッファチャンクのサイズを指定する。上限を超えた場合はバッファをフラッシュする
</store>
<store>
type grep #messageをgrepするプラグインを使用する
input_key message #grepの対象とするレコードを指定する
regexp1 message HOGE #messageレコードの中で「HOGE」と一致する文言を処理する
add_tag_prefix grep_filter #tagのプレフィックスに「grep_filter」を付与する
</store>
</match>


<match grep_filter.*>
type file
buffer_type file
path /var/log/fluentd/log.messages
symlink_path /var/log/fluentd/log.messages #シンボリックリンクファイルのパスを指定する
buffer_path /var/log/fluentd/log.messages.buf #バッファファイルのパスを指定する
</match>
######################################################################


★ログ保管用にディレクトリを追加する
#mkdir /var/log/td-agent/logs/
#mkdir /var/log/fluentd/

 

★fluentdをroot権限で動かすため、以下ファイルを作成・設定する
#vim /etc/sysconfig/td-agent
######################################################################
DAEMON_ARGS="--user root"
TD_AGENT_ARGS="/usr/sbin/td-agent --group root --log /var/log/td-agent/td-agent.log"
######################################################################

 

grep プラグインを追加
参考:https://github.com/sonots/fluent-plugin-grep

regexp/excludeで条件に一致した文字列に対してアクションを実行する
今回は条件に一致したら、test.messagesの「grep_filter」というタグをつける

#/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-grep
#systemctl restart td-agent


以下のログが出力されていれば正常起動(エラーがない)。
#cat /var/log/td-agent/td-agent.log
######################################################################
2016-11-04 15:35:39 +0900 [info]: reading config file path="/etc/td-agent/td-agent.conf"
2016-11-04 15:35:39 +0900 [info]: starting fluentd-0.12.29
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-mixin-config-placeholders' version '0.4.0'
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-mixin-plaintextformatter' version '0.2.6'
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-plugin-grep' version '0.3.4'
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-plugin-kafka' version '0.3.1'
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-plugin-mongo' version '0.7.15'
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-plugin-rewrite-tag-filter' version '1.5.5'
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-plugin-s3' version '0.7.1'
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-plugin-scribe' version '0.10.14'
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-plugin-td' version '0.10.29'
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-plugin-td-monitoring' version '0.2.2'
2016-11-04 15:35:39 +0900 [info]: gem 'fluent-plugin-webhdfs' version '0.4.2'
2016-11-04 15:35:39 +0900 [info]: gem 'fluentd' version '0.12.29'
2016-11-04 15:35:39 +0900 [info]: adding match pattern="test.messages" type="copy"
2016-11-04 15:35:39 +0900 [warn]: time_slice_wait is ignored if flush_interval is specified: <store>
type file
path /var/log/td-agent/logs/messages
time_slice_format %Y%m%d
time_slice_wait 10m
flush_interval 60m
flush_at_shutdown true
format out_file
buffer_chunk_limit 32m
buffer_path /var/log/td-agent/logs/messages.*
</store>
2016-11-04 15:35:39 +0900 [info]: adding match pattern="grep_filter.*" type="file"
2016-11-04 15:35:39 +0900 [info]: adding source type="forward"
2016-11-04 15:35:39 +0900 [info]: using configuration file: <ROOT>
<source>
type forward
port 24224
bind 0.0.0.0
</source>
<match test.messages>
type copy
<store>
type file
path /var/log/td-agent/logs/messages
time_slice_format %Y%m%d
time_slice_wait 10m
flush_interval 60m
flush_at_shutdown true
format out_file
buffer_chunk_limit 32m
buffer_path /var/log/td-agent/logs/messages.*
</store>
<store>
type grep
input_key message
regexp1 message HOGE
add_tag_prefix grep_filter
</store>
</match>
<match grep_filter.*.*>
type file
buffer_type file
path /var/log/fluentd/log.messages
symlink_path /var/log/fluentd/log.messages
buffer_path /var/log/fluentd/log.messages.buf
</match>
</ROOT>
2016-11-04 15:35:39 +0900 [info]: listening fluent socket on 0.0.0.0:24224

######################################################################

 

ちなみに「grep」のpluginがない場合は起動に失敗する。

# systemctl restart td-agent
######################################################################
Job for td-agent.service failed. See 'systemctl status td-agent.service' and 'journalctl -xn' for details.
######################################################################

 

/var/log/td-agent/td-agent.logを確認して、何が原因かを確認する
※今回は「grep」のプラグインがわからないとエラーがでて起動失敗しているためプラグインを追加する。
######################################################################
2016-11-04 15:30:45 +0900 [error]: config error file="/etc/td-agent/td-agent.conf" error="Unknown output plugin 'grep'. Run 'gem search -rd fluent-plugin' to find plugins"
2016-11-04 15:30:45 +0900 [info]: process finished code=256
2016-11-04 15:30:45 +0900 [warn]: process died within 1 second. exit.
######################################################################

 

★★クライアント側

#curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh
#cd /etc/td-agent
#cp -p td-agent.conf td-agent.conf-`date -I`
#vim td-agent.conf

以下設定を投入
######################################################################
<source>
type tail #tailプラグインを利用する(linuxのtailのような動きをする)
path /var/log/messages
format syslog #syslogのログファイルをJSON形式にパースできるsyslogプラグインを指定
pos_file /var/log/td-agent/messages.pos #tailプラグインでどこまで読み込んだかを記憶する
tag test.messages #タグ名を指定する
</source>


#hostname追加
<filter test.*>
type record_transformer #tag名がfilterで指定したファイルに対してレコードを追加する
<record>
hostname ${hostname} #ホスト名を追記
tag ${tag} #タグ名を追記
</record>
</filter>


## Output
<match test.*>
type forward
<server>
name test01
host 172.16.1.36
port 24224
</server>
</match>
######################################################################


★fluentdをroot権限で動かすため、以下ファイルを作成・設定する
#vim /etc/sysconfig/td-agent
######################################################################
DAEMON_ARGS="--user root"
TD_AGENT_ARGS="/usr/sbin/td-agent --group root --log /var/log/td-agent/td-agent.log"
######################################################################


#systemctl restart td-agent

以下のログが出力されていれば正常起動(エラーがない)。
#cat /var/log/td-agent/td-agent.log
######################################################################
2016-11-04 15:40:21 +0900 [info]: reading config file path="/etc/td-agent/td-agent.conf"
2016-11-04 15:40:21 +0900 [info]: starting fluentd-0.12.29
2016-11-04 15:40:21 +0900 [info]: gem 'fluent-mixin-config-placeholders' version '0.4.0'
2016-11-04 15:40:21 +0900 [info]: gem 'fluent-mixin-plaintextformatter' version '0.2.6'
2016-11-04 15:40:21 +0900 [info]: gem 'fluent-plugin-kafka' version '0.3.1'
2016-11-04 15:40:21 +0900 [info]: gem 'fluent-plugin-mongo' version '0.7.15'
2016-11-04 15:40:21 +0900 [info]: gem 'fluent-plugin-rewrite-tag-filter' version '1.5.5'
2016-11-04 15:40:21 +0900 [info]: gem 'fluent-plugin-s3' version '0.7.1'
2016-11-04 15:40:21 +0900 [info]: gem 'fluent-plugin-scribe' version '0.10.14'
2016-11-04 15:40:21 +0900 [info]: gem 'fluent-plugin-td' version '0.10.29'
2016-11-04 15:40:21 +0900 [info]: gem 'fluent-plugin-td-monitoring' version '0.2.2'
2016-11-04 15:40:21 +0900 [info]: gem 'fluent-plugin-webhdfs' version '0.4.2'
2016-11-04 15:40:21 +0900 [info]: gem 'fluentd' version '0.12.29'
2016-11-04 15:40:21 +0900 [info]: adding filter pattern="test.*" type="record_transformer"
2016-11-04 15:40:21 +0900 [info]: adding match pattern="test.*" type="forward"
2016-11-04 15:40:21 +0900 [info]: adding forwarding server 'test01' host="172.16.1.36" port=24224 weight=60 plugin_id="object:3fecb37c76b4"
2016-11-04 15:40:21 +0900 [info]: adding source type="tail"
2016-11-04 15:40:21 +0900 [info]: using configuration file: <ROOT>
<source>
type tail
path /var/log/messages
format syslog
pos_file /var/log/td-agent/messages.pos
tag test.messages
</source>
<filter test.*>
type record_transformer
<record>
hostname ${hostname}
tag ${tag}
</record>
</filter>
<match test.*>
type forward
<server>
name test01
host 172.16.1.36
port 24224
</server>
</match>
</ROOT>
2016-11-04 15:40:21 +0900 [info]: following tail of /var/log/messages

######################################################################


実際に転送されたログを確認します。
★test02側(fluentdクライアント)のログ
/var/log/messages(抜粋)
######################################################################
Nov 4 16:05:38 test02 nm-dispatcher: Dispatching action 'dhcp4-change' for eth0
######################################################################


★test02側(fluentdサーバ)のログ
/var/log/td-agent/logs/messages.20161104.b54073f64c0093526(抜粋)
※デフォルトだとログファイル名の後ろに「日付+バッファファイルの識別子」が付与される
参考:http://docs.fluentd.org/articles/out_file#path-required
######################################################################
2016-11-04T16:05:38+09:00 test.messages {"host":"test02","ident":"nm-dispatcher","message":"Dispatching action 'dhcp4-change' for eth0","hostname":"test02","tag":"test.messages"}
######################################################################

 

上記のようにJSON形式のパースされており、またログの最後尾に、クライアント側で設定した
ホスト名とタグ名が付与されていることが確認できます。
######################################################################

type record_transformer
<record>
hostname ${hostname}
tag ${tag}
</record>
"hostname":"test02","tag":"test.messages"
######################################################################

 

つづいて、サーバ側で設定した以下の設定が有効になっているか確認します。
######################################################################
<store>
type grep
input_key message
regexp1 message HOGE
add_tag_prefix grep_filter
</store>

######################################################################


条件に一致した場合は、タグに「grep_filter」を付与します。
そのあと、サーバの最下部で設定した通り、/var/log/fluentd/log.messagesに出力されるか確認します。
######################################################################
<match grep_filter.*>
type file
buffer_type file
path /var/log/fluentd/log.messages
symlink_path /var/log/fluentd/log.messages
buffer_path /var/log/fluentd/log.messages.buf
</match>
######################################################################


★動作確認
######################################################################
[root@test02 td-agent]# logger "HOGE"
[root@test02 td-agent]# cat /var/log/messages
Nov 4 16:32:39 test02 root: HOGE

★/var/log/fluentd/log.messagesを確認
[root@test01 ~]# cat /var/log/fluentd/log.messages
2016-11-04T16:32:39+09:00 grep_filter.test.messages {"host":"test02","ident":"root","message":"HOGE","hostname":"test02","tag":"test.messages"}
上記のとおりmessageにHOGEが含まれていた場合は、別ファイルに吐き出されることを確認できました。

 

補足:
symlink_path は buffer_path で指定したログファイルへのシンボリックリンクとなるため、ファイル名を固定したい場合(監視とか)に重宝する
buffer_pathがフラッシュされて別ファイルに名前が変わっても自動的にシンボリックリンクを張りなおしてくれるため、かなりの便利機能
ただし、buffer_pathに指定したログファイルがフラッシュされると、新規にbuffer_pathが作成されるまで(クライアント側からログが転送されるまで)は
リンク切れとなるため、要注意。