在CentOS下,要自定義Filebeat的輸出格式,你需要修改Filebeat的配置文件。以下是具體步驟:
/etc/filebeat/filebeat.yml。使用文本編輯器打開它,例如使用vi編輯器:sudo vi /etc/filebeat/filebeat.yml
output部分。如果沒有這個部分,你可以在filebeat.yml文件的末尾添加一個。接下來,你可以選擇一個輸出模塊,例如logstash、elasticsearch等。這里以logstash為例:output.logstash:
hosts: ["localhost:5044"]
filebeat.yml文件中添加以下內容:setup.template.name: "filebeat-custom"
setup.template.pattern: "filebeat-*"
setup.template.enabled: false
這將禁用Filebeat自動創建模板,并允許你手動創建自定義模板。
保存并關閉filebeat.yml文件。
創建一個名為filebeat-custom-template.json的新文件,用于定義自定義輸出格式。使用文本編輯器打開它,例如使用vi編輯器:
sudo vi /etc/filebeat/filebeat-custom-template.json
filebeat-custom-template.json文件中,定義你的自定義輸出格式。以下是一個簡單的示例,將日志輸出為JSON格式:{
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"template": {
"mappings": {
"_source": {
"enabled": true
}
}
},
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
這個示例僅定義了索引設置,你可以根據需要添加其他設置。
保存并關閉filebeat-custom-template.json文件。
重啟Filebeat服務以應用更改:
sudo systemctl restart filebeat
現在,Filebeat將使用你定義的自定義輸出格式。請注意,這個示例僅適用于將日志發送到Logstash的情況。如果你使用其他輸出模塊,你需要根據模塊的文檔來定義自定義輸出格式。