http://dev.classmethod.jp/cloud/td-agent2-amazon-linux/
上記ページに沿って行った。CentOSでも同じ手順。
手動で実行するならば以下の通りに実行する
[treasuredata] name=TreasureData baseurl=http://packages.treasure-data.com/redhat/$basearch gpgcheck=0
yum install td-agent
| CentOS 6 | td-agent 0.10.55 |
| CentOS 7 | td-agent 0.12.12 |
| AmazonLinux 15.09 | td-agent 0.12.12 |
sourceで入力を定義して、matchで処理を行う。matchで複数の処理はできないので別々のプラグインで複数処理をしたい場合はtagをつける。
<match apache.access> type file path /var/tmp/apache_all.log tag next.apache.access </match> <match next.apache.access> type file path /var/tmp/apache_all2.log </match>
include conf.d/*.conf
<source>
type tail
tag var.tmp
path "/var/tmp/#{ENV['TD_HOSTNAME']}"
format none
</source>
ダブルクォーテーションで囲まないと展開されないので注意。 matchには使えない・・・・これでは意味ない "#{Socket.gethostname}"でホスト名利用可能
<match raw.dummy>
type file
path "/var/tmp/#{ENV['HOME']}/test.log"
</match>
インクルードには使えた。
@include "#{ENV['TD_HOSTNAME']}.conf"
HOMEはtd-agentの実行ユーザーのディレクトリとなる。デフォルトでは/var/lib/td-agent/
# http://localhost:8888/<tag>?json=<json> <source> type http port 8888 </source>
type forwardの場合はhttpアクセスはできないがそのポートで待ち受けすることになる。
http://www.fluentd.org/guides/recipes/apache-add-hostname
<filter web.*>
type record_transformer
<record>
service_name ${tag_parts[1]}
</record>
</filter>
<filter apache.access> type grep exclude1 statuscode (200|301|302|304) </filter>
#!/opt/td-agent/embedded/bin/ruby ENV["GEM_HOME"]="/opt/td-agent/embedded/lib/ruby/gems/2.1.0/" ENV["GEM_PATH"]="/opt/td-agent/embedded/lib/ruby/gems/2.1.0/" #ENV["FLUENT_CONF"]="/etc/td-agent/td-agent.conf" ENV["FLUENT_CONF"]="/etc/td-agent/test.rb" ENV["FLUENT_PLUGIN"]="/etc/td-agent/plugin" ENV["FLUENT_SOCKET"]="/var/run/td-agent/td-agent.sock" load "/opt/td-agent/embedded/bin/fluentd"
['hoge','fuga'].each do |i|
match ("foo#{i}.#{ENV['HOSTNAME']}") {
type :stdout
}
end
source {
type :tail
path "/var/tmp/hoge.log"
}
td-agent -c /etc/td-agent/test.rb --dry-run
<match foohoge.**> type stdout </match> <match foofuga.**> type stdout </match>
| type名 | 簡単な概要 |
| null | 転送せずに捨てる |
| forest | タグ名を置換変数化できるので、まとめて同じような設定をしたいときに使う |
| rewrite_tag_filter | 正規表現でタグづけできる |
| record_modifier | 新たに属性を追加できる。たとえばApacheログにホスト名を付与したりとか |
<source>
type tail
format apache
path /var/log/httpd/*_access_log
tag apache.access
pos_file /tmp/fluentd-apache.pos
</source>
<match apache.access>
type s3
aws_key_id
aws_sec_key
s3_bucket bucket_name
s3_endpoint bucket_name.s3-website-ap-northeast-1.amazonaws.com
path logs/
buffer_path /var/tmp/fluentd
time_slice_format %Y%m%d/%H_apache.log
time_slice_wait 30m
flush_interval 60s // この感覚でS3にputするので一日1440リクエストで危うくクラウド破産!
</match>
<source>
type tail
path /var/log/httpd/error_log
format apache_error
tag apache.error
pos_file /tmp/apache_error.pos
</source>
# 送り先を Fluentd の標準ログへ出力します
<match apache.error>
type stdout
</match>
<source> type tail path /var/log/httpd/access_log pos_file /var/tmp/access_log.pos tag httpd format none </source> # 送り先を Fluentd の標準ログへ出力します <match httpd> type stdout </match>
| プラグイン名 | |
| copy | 転送やファイル保存など複数に保存したいときに |
| rewrite_tag_filter | 条件に応じてタグを書き換えることができる |
| forest | 同じようなタグに一括で適用したい場合に非常に便利 |
| fluent-plugin-map | レコードの内容書き換え |
| fluent-plugin-record-reformer | 同じくレコード書き換え |
<source> type tail format none path /var/tmp/%Y%m%d%H.log tag tail_ex_test pos_file /tmp/td-agent/tail_ex_test.pos refresh_interval 10 </source>
日付のフォーマットはrubyのもの参照! 上記の例だと2015083122.logが監視ファイル名となる。
| フォーマット名 | 入力文字例 | 備考 |
| none | 入力そのまま | |
| none_with_hostname | 入力文字列にhost情報 | |
| ltsv | domain:example.com | ラベル付きのTSV |
| apache2 | apacheのcombined | カスタマイズしてたらNG |
| apache.error | apacheのerrorログ | カスタマイズしてたらNG |
| csv,tsv | example.com,/hoge | keys domain,pathなどとキーを別個定義 |
formatを自分で作る場合rubyの正規表現の知識が必須。
日付の部分の正規表現がとてもめんどくさい。\[(?<time>[^\]]+)\]がその正規表現。フォーマットも指定しないとだめ。
format /^(?<host>[^ ]+) [^ ]+ [^ ]+ \[(?<time>[^\]]+)\] (?<message>[^ ]+).*$/ time_format %d/%b/%Y:%T %z
http://diary.tachibanakikaku.com/2013/12/fluentdformat.html
#!/usr/bin/env ruby # -*- coding: utf-8 -*- require 'time' require 'fluent/log' require 'fluent/config' require 'fluent/engine' require 'fluent/parser' $log ||= Fluent::Log.new # debug log = '' format = // time_format = '' parser = Fluent::TextParser::RegexpParser.new(format, 'time_format' => time_format) puts parser.call(log)
/usr/lib64/fluent/ruby/bin/ruby fluenttest.ruby
Fluentular: a Fluentd regular expression editor http://fluentular.herokuapp.com/
http://y-ken.hatenablog.com/entry/fluentd-syslog-permission
yumインストールした場合はtd-agentが管理するrubyでインストールする必要がある。
sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-zabbix
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-forest
/etc/td-agent/plugin/in_xxx.rb or out_xxx.rb
posファイルを変更してもダメ!tailプラグインしかないのがイタイ。 結局ファイルを上書きすることで解決だが、一気に読み込むため以下のエラーが出てしまう。
2015-09-01 16:28:23 +0900 [warn]: Size of the emitted data exceeds buffer_chunk_limit. 2015-09-01 16:28:23 +0900 [warn]: This may occur problems in the output plugins ``at this server.`` 2015-09-01 16:28:23 +0900 [warn]: To avoid problems, set a smaller number to the buffer_chunk_limit 2015-09-01 16:28:23 +0900 [warn]: in the forward output ``at the log forwarding server.``
outputのbuffer_chunk_limitを100Mにしたら、エラーは消えた。
emblukという新しいソリューションが出ているので今後はそちらに期待。
最近のバージョンではこちらを使う。
<filter foo.bar> type grep regexp1 message cool regexp2 hostname ^web\d+\.example\.com$ exclude1 message uncool
/filter>
複数条件がある場合 regexpの場合はAND条件になり、excludeの場合はor条件になる。基本的にexcludeで使っていくべきだろう。
いまいち使えないのでFilterを検討する!
/usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-rewrite
<match test.log>
type rewrite
remove_prefix test
add_prefix reformed
<rule>
key message
pattern hoge
replace fuga
</rule>
</match>
<source>
type dummy
tag raw.dummy
dummy {"message":"[WARN] warning[tab]message[tab]"}
</source>
<filter raw.**>
type grep
regexp1 message WARN
</filter>
<filter raw.**>
type record_transformer
enable_ruby true
<record>
tag ${tag}
hostname "#{Socket.gethostname}"
replaced ${message.gsub(/tab/,'\t')}
</record>
</filter>
<match raw.**>
type stdout
</match>