Metricbeat 参考指南(步骤3:在Elasticsearch中加载索引模板)

步骤3:在Elasticsearch中加载索引模板

在Elasticsearch中,索引模板用于定义设置和映射,以肯定如何分析字段。html

Metricbeat推荐的索引模板文件是由Metricbeat包安装的,若是你接受metricbeat.yml配置文件中的默认配置,那么Metricbeat在成功链接到Elasticsearch以后会自动加载模板。若是模板已经存在,则不会覆盖它,除非你配置Metricbeat这样作。docker

经过在Metricbeat配置文件中配置模板加载选项,你能够禁用自动模板加载,或者加载你本身的模板。json

你还能够设置选项来更改索引和索引模板的名称。segmentfault

加载索引模板须要链接到Elasticsearch,若是输出不是Elasticsearch,则必须手动加载模板。

配置模板加载

若是激活了Elasticsearch输出,默认状况下,Metricbeat会自动加载推荐的模板文件fields.yml,你能够将metricbeat.yml配置文件中的默认值更改成:安全

  • 加载不一样的模板
setup.template.name: "your_template_name"
setup.template.fields: "path/to/fields.yml"

若是模板已经存在,则不会覆盖它,除非你配置Metricbeat这样作。app

  • 覆盖现有模板
setup.template.overwrite: true
  • 禁用自动模板加载
setup.template.enabled: false

若是禁用自动模板加载,则须要手动加载模板。curl

  • 更改索引名称

默认状况下,Metricbeat将事件写入名为metricbeat-6.4.2-yyyy.MM.dd的索引,yyyy.MM.dd是事件被索引的日期,要使用不一样的名称,能够在Elasticsearch输出中设置index选项。你指定的值应该包括索引的根名称加上版本和日期信息,你还须要配置setup.template.namesetup.template.pattern选项以匹配新名称,例如:elasticsearch

output.elasticsearch.index: "customname-%{[beat.version]}-%{+yyyy.MM.dd}"
setup.template.name: "customname"
setup.template.pattern: "customname-*"
setup.dashboards.index: "customname-*"

setup.dashboards.index => 若是你计划设置Kibana仪表盘,还能够设置此选项以覆盖仪表盘和索引模式中定义的索引名称。ide

有关配置选项的完整列表,请参阅加载Elasticsearch索引模板。ui

手动加载模板

要手动加载模板,运行setup命令,须要链接到Elasticsearch,若是启用了另外一个输出,你须要使用-E选项暂时禁用该输出并启用Elasticsearch。这里的示例假设启用了Logstash输出,若是已经启用了Elasticsearch输出,能够省略-E标志。

若是你链接到一个安全的Elasticsearch集群,请确保你已经按照步骤2:配置Metricbeat所述配置了凭证。

若是运行Metricbeat的主机没有直接链接到Elasticsearch,请参见手动加载模板(备用方法)。

要加载模板,请为系统使用适当的命令。

deb和rpm:

metricbeat setup --template -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]

mac:

./metricbeat setup --template -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'

docker:

docker run docker.elastic.co/beats/metricbeat:6.4.2 setup --template -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'

win:

做为管理员打开PowerShell提示符(右键单击PowerShell图标并选择Run as Administrator)。

从PowerShell提示符,切换到你安装Metricbeat的目录,而后运行:

PS > .\metricbeat.exe setup --template -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'

强迫Kibana查看最新的文档

若是你已经使用Metricbeat将数据索引到Elasticsearch,那么索引可能包含旧文档,加载索引模板后,能够从metricbeat-*删除旧文档,以强制Kibana查看最新的文档,使用此命令:

deb、rpm和mac:

curl -XDELETE 'http://localhost:9200/metricbeat-*'

win:

PS > Invoke-RestMethod -Method Delete "http://localhost:9200/metricbeat-*"

这个命令删除全部匹配metricbeat-*模式的索引,在运行此命令以前,请确保要删除与模式匹配的全部索引。

手动加载模板(备用方法)

若是运行Metricbeat的主机没有直接链接到Elasticsearch,你能够将索引模板导出到文件,将其移动到有链接的机器上,而后手动安装模板。

  1. 导出索引模板:
    deb和rpm:

    metricbeat export template > metricbeat.template.json

    mac:

    ./metricbeat export template > metricbeat.template.json

    win:

    PS > .\metricbeat.exe export template --es.version 6.4.2 | Out-File -Encoding UTF8 metricbeat.template.json
  2. 安装模版:
    deb、rpm和mac:

    curl -XPUT -H 'Content-Type: application/json' http://localhost:9200/_template/metricbeat-6.4.2 -d@metricbeat.template.json

    win:

    PS > Invoke-RestMethod -Method Put -ContentType "application/json" -InFile metricbeat.template.json -Uri http://localhost:9200/_template/metricbeat-6.4.2
相关文章
相关标签/搜索