??????連接Elasticsearch是要求加載索引模版的。如果你的輸出目標不是Elasticsearch,那么你就必須手動加載模版。
在Elasticsearch中,索引模版通常用來定義設置和映射關系,確定域需要如何被數據分析。
Metricbeat建議的索引模版在已經在安裝Metricbeat的時候配置好了。如果你使用默認的metricbeat.yml配置文件,那么Metricbeat會自動在成功連接到Elasticsearch之后加載模版。如果模版已經存在,那就不會被覆蓋除非你配置了Metricbeat強制覆蓋。
配置模版加載
默認情況,如果Metricbeat激活了Elasticsearch作為輸出,那么Metricbeat會自動加載推薦的模版文件fields.yml。如果你向使用默認的索引模版,不需要其他額外的配置。否則,你可以修改在metricbeat.yml配置文件的默認配置:
- 加載不同的模版
setup.template.name: "your_template_name"
setup.template.fields: "path/to/fields.yml"
如果模版已存在,除非你配置Metricbeat強制覆蓋,否則文件不會做改動。
- 覆蓋已存在的模版
setup.template.overwrite: true
- 禁用模版加載
setup.template.enabled: false
如果你禁用自動模版加載,那么你就需要自己手動加載模版
- 重命名索引
??如果你正在發送事件到一個支持索引生命周期管理的集群,那么請參考 Index lifecycle management (ILM)重命名索引。
默認情況下,當禁用或者不支持索引生命周期管理時,Metricbeat使用時間序列索引。這個索引叫metricbeat-7.x.x-yyyy.MM.dd,這里的yyyy.MM.dd是這個觸發索引的事件發生的時間。要用其他命名的話,參考Elasticsearch輸出的索引配置選項。你所指定的名字應該包含這個索引的根名稱+版本號+日期信息。同時你還需要配置setup.template.name和setup.template.pattern來匹配這個新名字。如下:
output.elasticsearch.index: "customname-%{[agent.version]}-%{+yyyy.MM.dd}"
setup.template.name: "customname"
setup.template.pattern: "customname-*"
??如果索引生命周期管理被啟用(通常是默認的),那么setup.template.name和setup.template.pattern會被忽略。
如果你使用預構建Kibana儀表盤,同時設置了setup.dashboards.index選項,如下:
setup.dashboards.index: "customname-*"
參考Elasticsearch index template獲取所有配置列表。
手動加載模版
要手動加載模版,運行setup命令。要求已有Elasticsearch
的連接。如果還啟用了另一個輸出,你需要臨時將其禁用,并使用-E參數吧Elasticsearch啟用。下面的例子假設Logstash輸出已經啟用。你可以省略-E參數,如果Elasticsearch輸出已經啟用。
如果你連接到啟用安全性的Elasticsearch集群,確保你已經按照第二步配置Metricbeat所表述的方式配置了credentials。
如果你所運行的Metricbeat沒有直連Elasticsearch,參考 Load the template manually (alternate method).
加載模版,使用對應系統的命令:
deb and rpm:
metricbeat setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'
mac:
./metricbeat setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'
brew:
metricbeat setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'
linux:
./metricbeat setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'
docker:
docker run docker.elastic.co/beats/metricbeat:7.8.0 setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'
win:
用管理員打開 一個PowerShell prompt (右鍵單擊PowerShell 選擇以管理員運行).
在PowerShell prompt中, 重定向到你安裝Metricbeat的目錄, 然后運行:
PS > .\metricbeat.exe setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'
強制讓Kibana查看新文檔
如果你已經用Metricbeat索引數據到Elasticsearch,這些索引可能包含舊文檔。當你加載了索引模版之后,你可以在metricbeat-*刪除舊文檔,一強制Kibana查看新文檔。
使用如下命令:
deb and rpm:
curl -XDELETE 'http://localhost:9200/metricbeat-*'
mac:
curl -XDELETE 'http://localhost:9200/metricbeat-*'
linux:
curl -XDELETE 'http://localhost:9200/metricbeat-*'
win:
PS > Invoke-RestMethod -Method Delete "http://localhost:9200/metricbeat-*"
這個命令會刪除所有符合模式匹配metricbeat-*的索引。在運行此命令之前,請確認你確實想要刪除這些符合模式匹配的索引。
手動加載模版(替代方法)
如果你運行的Metricbeat沒有直接連接Elasticsearch,你可以將索引模版到導出到一個文件中,然后將文件移動到具有連接的機器上,然后手動安裝模版。
導出索引模版的命令如下:
deb and rpm:
metricbeat export template > metricbeat.template.json
mac:
./metricbeat export template > metricbeat.template.json
brew:
metricbeat export template > metricbeat.template.json
linux:
./metricbeat export template > metricbeat.template.json
win:
PS > .\metricbeat.exe export template --es.version 7.8.0 | Out-File -Encoding UTF8 metricbeat.template.json
加載模版,運行下面這個命令:
deb and rpm:
curl -XPUT -H 'Content-Type: application/json' http://localhost:9200/_template/metricbeat-7.8.0 -d@metricbeat.template.json
mac:
curl -XPUT -H 'Content-Type: application/json' http://localhost:9200/_template/metricbeat-7.8.0 -d@metricbeat.template.json
linux:
curl -XPUT -H 'Content-Type: application/json' http://localhost:9200/_template/metricbeat-7.8.0 -d@metricbeat.template.json
win:
PS > Invoke-RestMethod -Method Put -ContentType "application/json" -InFile metricbeat.template.json -Uri http://localhost:9200/_template/metricbeat-7.8.0