AWSのRDSはLinuxサーバではない為、こちらからいろいろな設定ができません。
その為、どのようにPostgresのログを送信すればよいか、いろいろ試行錯誤したのでその備忘録です。 前提として、RDSに接続できるLinuxインスタンスにtd-agentが入っている状態が必要です。
fluent-plugin-rds-pgsql-log
https://github.com/shinsaka/fluent-plugin-rds-pgsql-log RDSのpostgresのログを取得するには上記のプラグインを利用します。
このプラグインはAWS RDSのログを取得し、td-agentのsourceとして利用できるようにするプラグインです。
ただ導入にあたり、依存関係に「AWS SDK」があるのですが、パッケージで導入したtd-agentでは fluentd-gem fluent-plugin-rds-pgsql-log にて導入ができませんので、以下のように導入します。
まず、td-agentに入っているrubyからaws-sdkをインストール
/usr/lib64/fluent/ruby/bin/gem install aws-sdk
fluent-plugin-rds-pgsql-logのプラグインを直接プラグインディレクトリにほおりこむ
wget https://raw.githubusercontent.com/shinsaka/fluent-plugin-rds-pgsql-log/master/lib/fluent/plugin/in_rds_pgsql_log.rb -P /etc/td-agent/plugin
これでpostgresのログをTDに送信できました。
<match td.postgres.log> type tdlog apikey ***************************** auto_create_table database postgres table log buffer_type file buffer_path /var/log/td-agent/buffer/td flush_interval 180s </match> <source> type rds_pgsql_log region ap-northeast-1 db_instance_identifier prod-auth access_key_id ****************** secret_access_key ********************************* refresh_interval 30 tag td.postgres.log pos_file /var/log/td-agent/pgsql-prod-auth-log-pos.dat </source>
こんな感じのデータが送信されます
{"message_level"=>"LOG", "time"=>"1436510427", "ident"=>"postgres", "host"=>"10.0.0.21(22112)", "database"=>"[unknown]", "user"=>"[unknown]", "pid"=>"6080", "log_file_name"=>"error/postgresql.log.2015-07-10-00", "message"=>" connection received: host=10.0.0.21 port=53253"}
host部分がSQL発行元IPになる
管理の上ではhost部分はRDSの名前になっているのが望ましいので、host部分を強制的に変更します。 データをリフォームする為に「fluent-plugin-record-reformer」というプラグインを利用しました。
https://github.com/sonots/fluent-plugin-record-reformer
wget https://raw.githubusercontent.com/sonots/fluent-plugin-record-reformer/master/lib/fluent/plugin/out_record_reformer.rb -P /etc/td-agent/plugin
最終的なtd-agent.confは以下のようになりました。
<match td.postgres.log> type record_reformer renew_record false enable_ruby false tag reformed.td.postgres.log # 強制的に文言を変更する host rds-postgres01 ident postgres </match> <match reformed.td.postgres.log> type tdlog apikey ***************************** auto_create_table database postgres table log buffer_type file buffer_path /var/log/td-agent/buffer/td flush_interval 180s </match> <source> type rds_pgsql_log region ap-northeast-1 db_instance_identifier prod-auth access_key_id ****************** secret_access_key ********************************* refresh_interval 30 tag td.postgres.log pos_file /var/log/td-agent/pgsql-prod-auth-log-pos.dat </source>