fluentdのgrepプラグインを利用してログをフィルタリングする方法を確認してみます。
例えば、apacheのアクセスログでコード200以外のログを出力するように設定してみます。
[root@node01 td-agent]# td-agent-gem install fluent-plugin-grep
WARN: Unresolved specs during Gem::Specification.reset:
json (>= 1.4.3)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
Fetching: fluent-plugin-grep-0.3.4.gem (100%)
Successfully installed fluent-plugin-grep-0.3.4
Parsing documentation for fluent-plugin-grep-0.3.4
Installing ri documentation for fluent-plugin-grep-0.3.4
Done installing documentation for fluent-plugin-grep after 0 seconds
1 gem installed
まずは、fluent-plugin-grepというプラグインをインストールします。
# less /etc/td-agent/td-agent.conf
・
・
・
<source>
type tail
format apache
path /opt/httpd/2.4.17/logs/access_log
pos_file /var/log/td-agent/access.pos
tag apache.access
</source>
<match apache.access>
type grep
input_key code
exclude ^200$
add_tag_prefix filtered
</match>
<match filtered.apache.access>
type file
path /var/log/td-agent/access
</match>
設定ファイルのsource、matchを編集します。
grepプラグインを利用して、codeというkeyが200以外(exclude)の場合、転送されたログにfilteredというタグを付与しています。
そして、タグを付与されたログをファイルに出力します。
[root@node01 td-agent]# service td-agent restart
Shutting down td-agent: [ OK ]
Starting td-agent: [ OK ]
設定ファイルを編集したので、サービスを再起動します。
172.17.9.91 - - [27/Nov/2015:00:49:25 +0900] "GET / HTTP/1.1" 200 45
172.17.9.91 - - [27/Nov/2015:00:49:35 +0900] "GET /44444 HTTP/1.1" 404 203
例えば上記のようなアクセスログがあった場合・・・
2015-11-27T00:49:35+09:00 filtered.apache.access {"host":"172.17.9.91","user":"-","method":"GET","path":"/44444","code":"404","size":"203"}
fluentdを通したログの出力はコード404のログのみになります。
例えば、apacheのアクセスログでコード200以外のログを出力するように設定してみます。
[root@node01 td-agent]# td-agent-gem install fluent-plugin-grep
WARN: Unresolved specs during Gem::Specification.reset:
json (>= 1.4.3)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
Fetching: fluent-plugin-grep-0.3.4.gem (100%)
Successfully installed fluent-plugin-grep-0.3.4
Parsing documentation for fluent-plugin-grep-0.3.4
Installing ri documentation for fluent-plugin-grep-0.3.4
Done installing documentation for fluent-plugin-grep after 0 seconds
1 gem installed
まずは、fluent-plugin-grepというプラグインをインストールします。
# less /etc/td-agent/td-agent.conf
・
・
・
<source>
type tail
format apache
path /opt/httpd/2.4.17/logs/access_log
pos_file /var/log/td-agent/access.pos
tag apache.access
</source>
<match apache.access>
type grep
input_key code
exclude ^200$
add_tag_prefix filtered
</match>
<match filtered.apache.access>
type file
path /var/log/td-agent/access
</match>
設定ファイルのsource、matchを編集します。
grepプラグインを利用して、codeというkeyが200以外(exclude)の場合、転送されたログにfilteredというタグを付与しています。
そして、タグを付与されたログをファイルに出力します。
[root@node01 td-agent]# service td-agent restart
Shutting down td-agent: [ OK ]
Starting td-agent: [ OK ]
設定ファイルを編集したので、サービスを再起動します。
172.17.9.91 - - [27/Nov/2015:00:49:25 +0900] "GET / HTTP/1.1" 200 45
172.17.9.91 - - [27/Nov/2015:00:49:35 +0900] "GET /44444 HTTP/1.1" 404 203
例えば上記のようなアクセスログがあった場合・・・
2015-11-27T00:49:35+09:00 filtered.apache.access {"host":"172.17.9.91","user":"-","method":"GET","path":"/44444","code":"404","size":"203"}
fluentdを通したログの出力はコード404のログのみになります。

コメント