LoginSignup
1
0

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-03-05

内容

Elasticsearch Output Plugin を参考に、td-agent3Fluentd v1.0)で Elasticsearch にログを保存する設定をします。

準備

ログ送信前のElasticsearchの状況

起動したての何もしていないElasticsearchです

$ curl localhost:9200/_aliases?pretty
{
  ".monitoring-es-6-2018.03.05" : {
    "aliases" : { }
  }
}

fluent-plugin-elasticsearch のインストール

  • td-agent をインストールすると、一緒にインストールされるプラグインですが、最新のプラグインを利用するため念のためインストールしておきます
  • インストールで使うコマンドは /opt/td-agent/embedded/bin/fluent-gem にあります。

インストール

$ sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-elasticsearch
Fetching: fluent-plugin-elasticsearch-2.7.0.gem (100%)
Successfully installed fluent-plugin-elasticsearch-2.7.0
Parsing documentation for fluent-plugin-elasticsearch-2.7.0
Installing ri documentation for fluent-plugin-elasticsearch-2.7.0
Done installing documentation for fluent-plugin-elasticsearch after 0 seconds
1 gem installed

確認

$ /opt/td-agent/embedded/bin/fluent-gem list fluent-plugin-elasticsearch

*** LOCAL GEMS ***

fluent-plugin-elasticsearch (2.7.0, 2.4.0)

設定の例

ApacheのアクセスログをElasticsearchに送る例です。

/etc/td-agent/td-agent.conf
# <source> についての詳細は割愛
<source>
  @type tail
  path /var/log/apache2/access.log
  tag apache.access
  pos_file /var/log/td-agent/apache-access-log.pos
  format apache2
</source>

# <match> の内容が本題
<match apache.access>
  @type elasticsearch
  host localhost
  port 9200
  index_name apache_access
  type_name apache_access
</match>

ドキュメントにあるシンプルな設定で試します

動作確認

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

$ while true; do curl localhost > /dev/null && sleep 30; done

などでアクセスログを出力して、Elasticsearchにログがたまっていくか確認します。

Elasticsearchの状況を確認

インデックスなど確認

$ curl localhost:9200/_aliases?pretty
{
  "apache_access" : {
    "aliases" : { }
  },
  ".monitoring-es-6-2018.03.05" : {
    "aliases" : { }
  }
}
$ curl localhost:9200/apache_access/_mapping?pretty=true
{
  "apache_access" : {
    "mappings" : {
      "apache_access" : {
        "properties" : {
          "agent" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "code" : {
            "type" : "long"
          },
          "host" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "method" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "path" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "size" : {
            "type" : "long"
          }
        }
      }
    }
  }
}

データの確認

$ curl localhost:9200/apache_access/apache_access/_search?pretty=true
{
  "took" : 58,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "apache_access",
        "_type" : "apache_access",
        "_id" : "4_Ly9GEBywL1eMvL3Szz",
        "_score" : 1.0,
        "_source" : {
          "host" : "127.0.0.1",
          "user" : null,
          "method" : "GET",
          "path" : "/",
          "code" : 200,
          "size" : 11576,
          "referer" : null,
          "agent" : "curl/7.47.0"
        }
      },
      {
        "_index" : "apache_access",
        "_type" : "apache_access",
        "_id" : "kfLw9GEBywL1eMvLySw8",
        "_score" : 1.0,
        "_source" : {
          "host" : "127.0.0.1",
          "user" : null,
          "method" : "GET",
          "path" : "/",
          "code" : 200,
          "size" : 11576,
          "referer" : null,
          "agent" : "curl/7.47.0"
        }
      },
      {
        "_index" : "apache_access",
        "_type" : "apache_access",
        "_id" : "5PLy9GEBywL1eMvL3Szz",
        "_score" : 1.0,
        "_source" : {
          "host" : "127.0.0.1",
          "user" : null,
          "method" : "GET",
          "path" : "/",
          "code" : 200,
          "size" : 11576,
          "referer" : null,
          "agent" : "curl/7.47.0"
        }
      },
      {
        "_index" : "apache_access",
        "_type" : "apache_access",
        "_id" : "kvLw9GEBywL1eMvLySw8",
        "_score" : 1.0,
        "_source" : {
          "host" : "127.0.0.1",
          "user" : null,
          "method" : "GET",
          "path" : "/",
          "code" : 200,
          "size" : 11576,
          "referer" : null,
          "agent" : "curl/7.47.0"
        }
      },
      {
        "_index" : "apache_access",
        "_type" : "apache_access",
        "_id" : "k_Lw9GEBywL1eMvLySw8",
        "_score" : 1.0,
        "_source" : {
          "host" : "127.0.0.1",
          "user" : null,
          "method" : "GET",
          "path" : "/",
          "code" : 200,
          "size" : 11576,
          "referer" : null,
          "agent" : "curl/7.47.0"
        }
      },
      {
        "_index" : "apache_access",
        "_type" : "apache_access",
        "_id" : "4vLy9GEBywL1eMvL3Szz",
        "_score" : 1.0,
        "_source" : {
          "host" : "127.0.0.1",
          "user" : null,
          "method" : "GET",
          "path" : "/",
          "code" : 200,
          "size" : 11576,
          "referer" : null,
          "agent" : "curl/7.47.0"
        }
      }
    ]
  }
}

以降、アクセスログが出力されればどんどんたまっていきます

まとめ

td-agent3Fluentd v1.0)で Elasticsearch にログを保存できました。

その他

エラーが出た

[error]: #0 Permission denied @ rb_sysopen - /var/log/apache2/access.log

のようなエラーが出るときはchmodします。

$ sudo chmod -R 755 /var/log/apache2/

Install Elasticsearch with Docker

DockerのElasticsearchを使うとお手軽に試せます
https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

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