LoginSignup
1
2

More than 5 years have passed since last update.

td-agent3(Fluentd v1.0)でElasticsearchに「独自ログ」を保存する。2018-03-05

Posted at

内容

td-agent3(Fluentd v1.0)でElasticsearchにログを保存する では td-agent3Fluentd v1.0)で Apache のログを、 Elasticsearch に保存しました
今回は、「独自ログ」を保存する設定について記します

準備

td-agent3(Fluentd v1.0)でElasticsearchにログを保存する を一通り設定した状態にする

設定の例

独自ログについて

(例ということで独自ログはかなりてきとーなものです)

  • /tmp/my.log に出力される
  • ログのフォーマットは datetime=%Y-%m-%d %H:%M:%S id=数値のID
    • 例: datetime=2018-03-05 07:48:33 id=697
    • $ echo "datetime=$(date "+%Y-%m-%d %H:%M:%S") id=$RANDOM" >> /tmp/my.log で出力します

設定ファイル

参考: https://docs.fluentd.org/v1.0/articles/parser_regexp

/etc/td-agent/td-agent.conf
# <source> の内容が本題
<source>
  @type tail
  path /tmp/my.log
  tag my.log
  pos_file /var/log/td-agent/my-log.pos

  # example:
  # datetime=2018-03-05 07:48:33 id=697
  <parse>
    @type regexp
    expression ^datetime=(?<datetime>[-: 0-9]+)\s+id=(?<id>\d+)
    types id:integer
  </parse>
</source>

# <match> についての詳細は割愛
<match my.log>
  @type elasticsearch
  host localhost
  port 9200
  index_name my_log
  type_name my_log
</match>

動作確認

以上の設定例で td-agent を起動してログ(/var/log/td-agent/td-agent.log)を確認します。
特に問題がなさそうであれば

$ while true; do echo "datetime=$(date "+%Y-%m-%d %H:%M:%S") id=$RANDOM" >> /tmp/my.log && sleep 10; done

などで「独自ログ」を出力して、Elasticsearchにログがたまっていくか確認します。

# 確認
$ tail /tmp/my.log
date=2018-03-05 08:09:46 id=3694
date=2018-03-05 08:09:56 id=24838

Elasticsearchの状況を確認

インデックスなど確認

$ curl localhost:9200/_aliases?pretty
{
  ".monitoring-es-6-2018.03.05" : {
    "aliases" : { }
  },
  "my_log" : {
    "aliases" : { }
  }
}
$ curl localhost:9200/my_log/_mapping?pretty=true
{
  "my_log" : {
    "mappings" : {
      "my_log" : {
        "properties" : {
          "datetime" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "id" : {
            "type" : "long"
          }
        }
      }
    }
  }
}

データの確認

$ curl localhost:9200/my_log/my_log/_search?pretty=true
{
  "took" : 75,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_log",
        "_type" : "my_log",
        "_id" : "yoQ69WEBsd0BUvEznMwz",
        "_score" : 1.0,
        "_source" : {
          "datetime" : "2018-03-05 08:14:34",
          "id" : 22645
        }
      },
      {
        "_index" : "my_log",
        "_type" : "my_log",
        "_id" : "y4Q69WEBsd0BUvEznMwz",
        "_score" : 1.0,
        "_source" : {
          "datetime" : "2018-03-05 08:14:44",
          "id" : 6898
        }
      },
      {
        "_index" : "my_log",
        "_type" : "my_log",
        "_id" : "yYQ69WEBsd0BUvEznMwz",
        "_score" : 1.0,
        "_source" : {
          "datetime" : "2018-03-05 08:14:24",
          "id" : 22018
        }
      },
      {
        "_index" : "my_log",
        "_type" : "my_log",
        "_id" : "yIQ69WEBsd0BUvEznMwz",
        "_score" : 1.0,
        "_source" : {
          "datetime" : "2018-03-05 08:14:14",
          "id" : 20901
        }
      },
      {
        "_index" : "my_log",
        "_type" : "my_log",
        "_id" : "zYQ69WEBsd0BUvEznMwz",
        "_score" : 1.0,
        "_source" : {
          "datetime" : "2018-03-05 08:15:04",
          "id" : 7842
        }
      },
      {
        "_index" : "my_log",
        "_type" : "my_log",
        "_id" : "zIQ69WEBsd0BUvEznMwz",
        "_score" : 1.0,
        "_source" : {
          "datetime" : "2018-03-05 08:14:54",
          "id" : 20147
        }
      }
    ]
  }
}

まとめ

  • 「独自ログ」を parse できました
  • td-agent3Fluentd v1.0)で Elasticsearch に「独自ログ」を保存できました
1
2
0

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
1
2