首页 > 解决方案 > Filebeat multiline patter

问题描述

I would like to configure a multiline pattern for each docker container that are deployed. I know that I can configure different filebeat inputs but the thing is that I don't know which container I am using because the path of the container log is like /var/lib/docker/containers/{id}/[{id}.log

Any ideas?

标签: dockerlogstashfilebeat

解决方案


您可以在 filebeat 配置中使用 glob-patterns:

像这样的设置

/var/lib/docker/containers/*/*.log

应该匹配您要查找的任何文件吗?

https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-input-log.html#input-paths

请确保一个文件没有被多个路径设置匹配。

根据添加的要求在下面进行编辑。

因此,例如,您将运行以下 2 个容器:

CONTAINER ID        IMAGE                                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
77e87b8e772e        yadayada                                   "/hihihi"                 2 weeks ago        Up 19 seconds       0.0.0.0:9080->9080/tcp   container1
99e87b8e772e        blablabla                                  "/hahaha"                 2 weeks ago        Up 19 seconds       0.0.0.0:9080->9080/tcp   container2

根据提供的信息,假设是:

container1 登录/var/lib/docker/containers/77e87b8e772e/77e87b8e772e.log
container2 登录/var/lib/docker/containers/99e87b8e772e/99e87b8e772e.log

这可能是配置:

filebeat.inputs:
- type: log
  paths: /var/lib/docker/containers/${CONTAINERID1}/${CONTAINERID1}.log
  multiline.pattern: '^=[A-Z]+|^$'
  multiline.negate: true
  multiline.match: after
- type: log
  paths: /var/lib/docker/containers/${CONTAINERID2}/${CONTAINERID2}.log
  multiline.pattern: '^=[1-9]+|^$'
  multiline.negate: true
  multiline.match: after

所以在启动 filebeat 时,在实际运行 filebeat 之前,你需要做一些额外的事情:

export CONTAINERID1=$(docker ps|grep "container1$" | cut -d ' ' -f1)
export CONTAINERID2=$(docker ps|grep "container2$" | cut -d ' ' -f1)
./filebeat

这样,只要容器名称保持不变,ID 就可以不同并且仍然有效。请注意,当您启动一个新的(a 版本)容器时,您必须重新启动 Filebeat 才能选择新路径。

另请注意,如果您在 docker 容器本身中运行 Filebeat,导出变量很可能是不够的,您必须在将文件 sed传递到 filebeat-container 之前使用或其他方式编辑文件


推荐阅读