Filebeat获取type为log的IP方式(解决Filebeat没有办法获取本机的IP)
这种方式只能在启动Filebeat时才会获得一次IP写入到Filebeat中,没有办法实现实时获取的。
这使用了两个脚本:我设置了Filebeat的启动脚本start.sh,和获取IP的get_ip.sh脚本。
start.sh内容
#!/bin/bash
IP=$(./get_ip.sh)
sed "s#HostnameIP#$IP#g" filebeat.kafka.yml > filebeat.yml
nohup ./filebeat -e -c filebeat.yml >> ./logs/filebeat-`date +%Y-%m-%d`.log 2>&1 &
get_ip.sh内容
#!/bin/bash
IP=$(hostname -I | awk '{print $1}')
echo $IP
filebeat.kafka.yml为模板配置文件,真正启动的Filebeat文件,在filebeat.kafka.yml配置
- type: log
enabled: true
paths:
- /tmp/log/*.log
fields_under_root: true # 用于决定是否将添加的字段(标记为 fields)直接放在顶层事件中,而不是在单独的 fields 对象中嵌套。
fields:
wisentIp: "HostnameIP" # HostnameIP将会被脚本查到的IP进行替换