Filebeat:自动创建动态的ES索引名称
在我的环境中,我希望按照不同的主机名或者IP的不同日期(年月日)去动态生成每天的ES索引名称,而不是某台机器的所有ES都一股脑的存储在一个索引中。
因此,filebeat所运行的服务器(目标机器)上的Filebeat服务的配置文件需要作出如下更改:
文件:【/etc/filebeat/filebeat.yml】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
[root@cloudera1 filebeat]# pwd /etc/filebeat [root@cloudera1 filebeat]# [root@cloudera1 filebeat]# ls -ltr total 332 -rw-r--r-- 1 root root 78871 Aug 20 03:30 filebeat.reference.yml -rw-r--r-- 1 root root 242580 Aug 20 03:30 fields.yml -rw------- 1 root root 8184 Oct 16 13:45 filebeat.yml drwxr-xr-x 2 root root 4096 Oct 16 14:23 modules.d [root@cloudera1 filebeat]# [root@cloudera1 filebeat]# cat filebeat.yml | grep -A 22 "\-\- Elasticsearch output \-\-" #-------------------------- Elasticsearch output ------------------------------ setup.ilm.enabled: false setup.template.enabled: false setup.template.name: "filebeat-cloudera1" setup.template.pattern: "filebeat-cloudera1-" output.elasticsearch: # Array of hosts to connect to. #hosts: ["localhost:9200"] hosts: ["192.168.72.112:9200"] # adamhuan define # index index: "filebeat-192.168.72.131-cloudera1-%{+yyyy.MM.dd}" # Optional protocol and basic auth credentials. #protocol: "https" #username: "elastic" #password: "changeme" #----------------------------- Logstash output -------------------------------- [root@cloudera1 filebeat]# |
上面的代码,我没有给出完整的filebeat.yml的内容,而是仅针对当前任务所需要修改的部分。
其中:
- 【setup.ilm.enabled】是为了禁用【索引生命周期管理】,因为不这么设置,则自定义的索引名称可能无法被运用。
- 【setup.template.enabled / setup.template.name / setup.template.pattern】 ,需要自定义索引名称,这三个是必须设置的。
- 【index】,自定义的索引名称
- 【${+yyyy.MM.dd}】,格式化时间信息
配置好了以后,重启Filebeat服务:
1 |
service filebeat restart |
然后,在Kibana中,你就可以看到ES增加了一个新的索引:

可以看到,按照我们定义的格式,已经成功创建了一个索引(Index)。
至此,文首的问题已经解决。
Done。