LoginSignup
2
2

More than 5 years have passed since last update.

HTTP→Fluentd→S3&MySQL

Posted at

PHPフロントエンドから内部ログをS3とMySQL(MariaDB)に格納するメモ。
S3の後にMySQLに入れようとして引っかかったので、その記録。

・使用するFluentdプラグインのインストール
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-s3
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-forest
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-uri_decoder
sudo yum install mysql-devel
sudo yum install gcc-c++
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-mysql
※mysql-develとgcc-c++はfluent-plugin-mysqlのインストールに必要

・送信側PHP
HTTP通信のレイテンシーを考えるとローカルにFluentd入れて経由させるべきなのかもしれないけど、
稼働中の本番環境を変更したくなかったのでHTTPで送信
$keywordはurl_encode()されている。

    $postdata = array(
       'keyword' => $keyword,
       'hogehoge' => $hogehoge,
       'mogemoge' => $mogemoge
    );
    $postjson = json_encode($postdata);
    $postoption = array('http' => array(
       'method' => 'POST',
       'content' => "json=".$postjson
    ));
    $posturl = "http://fluentdsrv:24225/geo.internal_log";
    $postrlt = file_get_contents($posturl, false, stream_context_create($postoption));

・受信側Fluentd
最初はmatchのタグを変えて
uri_decode→S3保存→MySQL保存
でやろうとしていたのだけど、S3とMySQLがシーケンシャルにできないことが判明。
http://qiita.com/nagais/items/ca96af840b8061102551
>fluentdは一つのイベントに対して、先にマッチするものがあると後のmatch句は効かない。
今思うとremove_prefix,add_prefix後のタグでS3保存が動いていたので、後続のmatchが動いていなかったのだろう。
type forest利用時の注意点かと。
ともあれ、シーケンシャルに動作できないのならtype copyを使用して複数で記載。

<source>
  type http
  port 24225
</source>
<match geo.internal_log>
  type uri_decode
  key_names keyword
  remove_prefix geo
  add_prefix decoded
</match>
<match decoded.internal_log>
  type copy
  <store>
    type forest
    subtype s3
    <template>
      aws_key_id IDだよ
      aws_sec_key 鍵だよ
      s3_bucket バケツ名だよ
      s3_region ap-northeast-1
      s3_object_key_format %{path}%{time_slice}_%{index}.log.gz
      buffer_path /var/log/fluent/s3/internal/
      store_as gzip

      path logs/internal/
      time_slice_format %Y%m%d/%H_internal
      time_slice_wait 10m
    </template>
  </store>

  <store>
    @type mysql_bulk
    host DBホスト
    port ポート番号
    username DBユーザー名
    password DBパスワード

    database データベース名
    table テーブル名
    include_time_key yes ※要らないかも

    column_names id, datetime, keyword, hogehoge, mogemoge
    key_names id, ${time}, keyword, hogehoge, mogemoge

    flush_interval 10s
  </store>
</match>

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2