Ubuntu下Filebeat定制报告模板(索引模板)的步骤
若需完全自定义模板的字段映射、设置等,可创建JSON格式的模板文件(如/etc/filebeat/custom-template.json),内容需包含索引模式、设置(如分片数)、映射(如字段类型)。示例如下:
{
"index_patterns": ["custom-report-*"], // 索引匹配模式
"settings": {
"number_of_shards": 3, // 主分片数(根据数据量调整)
"number_of_replicas": 1 // 副本数(高可用需求)
},
"mappings": {
"_source": {"enabled": true}, // 是否存储原始数据
"properties": {
"@timestamp": {"type": "date"}, // 时间戳字段类型
"message": {"type": "text"}, // 日志消息字段类型
"log_level": {"type": "keyword"}, // 日志级别(精确匹配)
"source_ip": {"type": "ip"} // IP地址(支持聚合)
}
}
}
编辑/etc/filebeat/filebeat.yml,通过以下参数关联自定义模板或直接定义模板内容:
setup.template.name: "custom-report" # 模板名称(自定义,需与后续pattern匹配)
setup.template.pattern: "custom-report-*" # 索引名称匹配规则(如custom-report-2025.11.09)
setup.template.enabled: true # 启用自定义模板(默认true)
setup.template.overwrite: true # 若模板已存在,是否覆盖(谨慎使用,生产环境建议false)
setup.template.name: "inline-template"
setup.template.source: | # 直接写入模板内容(JSON格式)
{
"index_patterns": ["inline-*"],
"settings": {"number_of_shards": 1},
"mappings": {"properties": {"@timestamp": {"type": "date"}}}
}
output.elasticsearch:
hosts: ["localhost:9200"]
index: "report-%{[beat.version]}-%{+yyyy.MM.dd}" # 索引名称格式(如report-8.12.0-2025.11.09)
若需在Kibana中可视化报告数据,需配置Dashboard索引并导入模板:
setup.dashboards.enabled: true # 启用Dashboard配置
setup.dashboards.index: "custom-report-*" # Dashboard索引匹配规则(需与模板pattern一致)
setup.kibana: # Kibana连接配置
host: "localhost:5601" # Kibana地址
username: "elastic" # 认证用户(若有)
password: "your_password" # 认证密码(若有)
保存filebeat.yml后,重启Filebeat服务使配置生效:
sudo systemctl restart filebeat
GET _index_template/custom-report
GET _cat/indices/custom-report-*?v
GET custom-report-*/_mapping
setup.template.source和setup.template.name,Filebeat会优先使用source中的内容。overwrite设为false,避免误覆盖已有模板;模板中的mappings需根据实际日志字段调整,避免不必要的字段存储。mappings正确定义了字段类型(如keyword用于精确匹配,text用于全文搜索)。