Skip to main content

One post tagged with "Filebeat"

View All Tags

· 4 min read
Dylan Li

在使用 Elastic Stack v7.4 实现统一的日志系统时,我们希望在 Filebeat 增加像 hostip 这样的参数来将宿主机的 IP 带到日志采集流中。比如这样配置:

- type: log
enabled: true
paths:
- /path/to/your/logs/your-serviceid.log
name: "your-serviceid-log"
multiline.pattern: '^[[:space:]]|^Caused by:|^<|^{|^}'
multiline.negate: false
multiline.match: after
ignore_older: 24h
fields_under_root: true
fields:
serviceid: your-serviceid
hostip: your-hostip

your-hostip 替换为当前宿主机的 IP,然后启动 filebeat 服务即可实现最初的目的。但是这样静态配置的方式还不够优雅,如果每台宿主机的配置文件中存在多组上述配置,在每一组配置中都去手动指定 IP 将会成一个繁琐而无味的工作。

换种思路,如果我们在 /etc/profile 文件中添加 HOST_IP 环境变量,并将值设置为宿主机的 IP,然后尝试重启获取一下,效果如何,我们将配置变更一下:

- type: log
enabled: true
paths:
- /path/to/your/logs/your-serviceid.log
name: "your-serviceid-log"
multiline.pattern: '^[[:space:]]|^Caused by:|^<|^{|^}'
multiline.negate: false
multiline.match: after
ignore_older: 24h
fields_under_root: true
fields:
serviceid: your-serviceid
hostip: "${HOST_IP}"

令人失落的是,我们发现使用这种方案会导致 filebeat 进程无法重启,原因出在它是通过 systemd 启动的,默认情况下系统的环境变量只有通过 pam 方式登录的用户才能读取到,但是 systemd 是不会进行登录的,所以就不能直接读取到系统的环境变量。该如何解决呢?