温馨提示×

centosfilebeat如何过滤数据

小樊
41
2026-10-14 17:57:21
栏目: 智能运维

在 CentOS 上,Filebeat 过滤数据主要通过 processors(处理器) 和 input 配置 来实现,而不是像 Logstash 那样用 filter 插件。下面按常见场景给你说明。


一、Filebeat 过滤数据的核心方式

Filebeat 过滤主要依靠:

  1. processors(推荐)
  2. include_lines / exclude_lines(简单行过滤)
  3. drop_event / drop_fields(丢弃事件或字段)

二、基础配置位置

配置文件一般在:

/etc/filebeat/filebeat.yml

修改后重启:

systemctl restart filebeat

三、按行内容过滤(最简单)

1️⃣ 只采集某些行(include_lines)

filebeat.inputs:
- type: log
  paths:
    - /var/log/app/*.log
  include_lines: ['ERROR', 'WARN']

✅ 只采集包含 ERROR 或 WARN 的行


2️⃣ 排除某些行(exclude_lines)

filebeat.inputs:
- type: log
  paths:
    - /var/log/app/*.log
  exclude_lines: ['DEBUG', 'INFO']

✅ 忽略 DEBUG / INFO 日志


四、使用 processors 过滤(推荐方式)

1️⃣ 丢弃整个事件(drop_event)

示例:丢弃 status=200 的日志

processors:
  - drop_event:
      when:
        contains:
          message: "status=200"

2️⃣ 按字段条件丢弃

processors:
  - drop_event:
      when:
        equals:
          log.level: "info"

3️⃣ 删除字段(drop_fields)

processors:
  - drop_fields:
      fields: ["agent.version", "ecs.version"]

4️⃣ 添加 / 修改字段

processors:
  - add_fields:
      target: ''
      fields:
        env: production

五、正则过滤示例

processors:
  - drop_event:
      when:
        regexp:
          message: "^DEBUG"

六、多条件过滤(and / or)

processors:
  - drop_event:
      when:
        and:
          - contains:
              message: "test"
          - equals:
              log.level: "debug"

七、常见实战场景

✅ 场景 1:只采集 ERROR 日志到 ES

filebeat.inputs:
- type: log
  paths:
    - /var/log/app.log
  include_lines: ['ERROR']

output.elasticsearch:
  hosts: ["http://localhost:9200"]

✅ 场景 2:过滤掉健康检查日志

processors:
  - drop_event:
      when:
        contains:
          message: "health check"

✅ 场景 3:不同日志发不同输出(用 fields + 条件)

filebeat.inputs:
- type: log
  paths:
    - /var/log/app.log
  fields:
    type: app

processors:
  - drop_event:
      when:
        not:
          equals:
            fields.type: app

八、注意事项(很重要)

❗ Filebeat 不能做复杂解析(如 JSON 拆分、grok)
❗ 复杂处理建议:

Filebeat → Kafka / Logstash → ES

或:

Filebeat → ES(简单过滤)

九、排查配置是否生效

filebeat test config
filebeat test output

查看实际发送内容:

filebeat -e -c /etc/filebeat/filebeat.yml

如果你愿意,可以告诉我:

  • 你用的是 ES / Kafka / Logstash
  • 日志格式(普通文本 / JSON)
  • 想过滤的具体规则

我可以帮你写一份可直接用的 filebeat.yml。

0 踩