td-agentの設定ファイルはXML形式ではなく、rubyのDSL形式で記述することもできる。
(共存はできない)
似たような個所を置きかえるのであればDSLにしたほうが良いと思ってトライ。
sysloghash = { "syslog" => "td.syslog.messages"}
#{ "syslog" => "td.syslog.messages"}.each do |key,value|
sysloghash.each do |key,value|
source {
type :tail
path "/var/log/messages"
format "#{key}"
tag "#{value}"
pos_file "/tmp/td-agent/syslog_messages.pos"
}
end
# apche setting
apache_hash = { "access" => "apache", "error" => "apache_error"}
apache_hash.each do |key,value|
source {
type :tail
path "/var/log/httpd/*_#{key}_log"
format "#{value}"
tag "apache_#{key}"
pos_file "/tmp/td-agent/apache_#{key}.pos"
}
end
match ('td.syslog.messages') {
type :copy
store {
type "mongo"
database "syslog"
collection "syslog"
host "localhost"
port "27017"
}
}
可読性が悪くなるような気もするが、ループ処理ができるのが楽。
最終的にこれらはパースされてXML形式の設定ファイルが出力される。
(なので既存との比較も/var/log/td-agent/td-agent.logを起動時に見ていれば比較できる)
<ROOT>
<source>
type tail
path /var/log/messages
format syslog
tag td.syslog.messages
pos_file /tmp/td-agent/syslog_messages.pos
</source>
<source>
type tail
path /var/log/httpd/*_access_log
format apache
tag apache_access
pos_file /tmp/td-agent/apache_access.pos
</source>
<source>
type tail
path /var/log/httpd/*_error_log
format apache_error
tag apache_error
pos_file /tmp/td-agent/apache_error.pos
</source>
<match td.syslog.messages>
type copy
<store>
type mongo
database syslog
collection syslog
host localhost
port 27017
</store>
</match>
</ROOT>