apache logをfluentdで収集してmongoとS3に突っ込むまで。
最終的な構成↓
EC2 Webサーバ2台に fluentをインストール。
MySQLのSlaveサーバにmongoとfluentをインストール。
インストール手順はこちら参照。
やっぱりログは圧縮してS3にぶっ込んでおいたほうがいいよね!と途中で思い直したので、S3のプラグインもインストール。
/usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-s3
DBのSecurityGroupでport24224と27017をWebのsgからアクセスできるように設定。
S3にuploadするfluentdの設定ファイルはこんな感じ。
vim /etc/td-agent/td-agent.conf # Input <source> type forward port 24224 </source> # Output <match filtered.**> type copy # localhost backup <store> type file path /var/log/fluent/access_log </store> # S3 backup <store> type s3 aws_key_id [ your aws key ] aws_sec_key [ your sec key ] s3_bucket [ your s3 bucket name ] s3_endpoint s3-ap-northeast-1.amazonaws.com path apache_logs/ # S3に格納するディレクトリ先 buffer_path /var/log/fluent/s3 # S3にflushするまでのbufferファイル格納先 time_slice_format %Y%m%d-%H # 1時間毎にflush time_slice_wait 10m # flushまでの待ち時間 utc </store> </match>
Webサーバのfluent設定ファイルは以下。
# select source file <source> type tail format apache path /var/log/httpd/access_log pos_file /var/log/td-agent/access_log.pos tag apache.access </source> # log rewrite # 集計対象を除外する <match apache.access> type rewrite remove_prefix apache.access add_prefix filtered <rule> key path pattern ^\/(?:image|css|js|assets|wordpress) ignore true </rule> <rule> key code pattern ^(?!200)\\d+$ ignore true </rule> </match> # 出力 <match filtered.**> type copy #db2サーバにファイル転送 <store> type forward send_timeout 60s recover_wait 30s heartbeat_interval 1s <server> name web1 host xx.xx.xx.xx port 24224 </server> </store> # mongoDBに登録 <store> # 出力先 type mongo # DB名とコレクション名 database apache collection access # MongoDB接続先とポート host xx.xx.xx.xx port 27017 # インターバル flush_interval 10s </store> </match>
これでWebのアクセスログがDB(Slaveサーバ)のmongoDBに入りつつ、1時間おきにS3にログアーカイブをアップロードする構成ができました。