使用td-agent3(Fluentd v1.0)将日志保存到Elasticsearch中。2018-03-05
內容相同,则僅需要一個選擇:
参考Elasticsearch输出插件,在td-agent3(Fluentd v1.0)中配置保存日志到Elasticsearch的设定。
准备
td-agent3(Fluentd v1.0)のインストールは済んでいるものとします
参考: Ubuntu 16.04.3 LTSにtd-agent3(Fluentd 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的示例。
# <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-agent3(Fluentd v1.0)中,我们成功地将日志保存到Elasticsearch中。
其他
出现了错误。
[error]: #0 Permission denied @ rb_sysopen - /var/log/apache2/access.log
当出现类似的错误时,需要执行chmod操作。
$ sudo chmod -R 755 /var/log/apache2/
使用Docker安装Elasticsearch
使用Docker的Elasticsearch,方便快捷地进行尝试。
https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html