温馨提示×

debian filebeat怎么过滤数据

小樊
49
2026-10-18 03:53:50
栏目: 智能运维

在 Debian 上,Filebeat 主要用于采集日志并发送到后端(Elasticsearch / Logstash / Kafka 等),本身不是强大的数据处理工具,但可以通过 processors(处理器) 来过滤、丢弃、修改数据。

下面按常见需求给你讲几种在 Debian 上 Filebeat 过滤数据的方法。


一、Filebeat 过滤的核心方式(processors)

Filebeat 的过滤主要在 filebeat.yml 里通过 processors 配置。

示例位置:

filebeat.inputs:
  - type: log
    paths:
      - /var/log/app/*.log
    processors:
      - drop_event.when:
          contains:
            message: "health check"

二、常见过滤场景

1️⃣ 丢弃包含某些关键字的日志(最常用)

processors:
  - drop_event.when:
      contains:
        message: "DEBUG"

或正则:

processors:
  - drop_event.when:
      regexp:
        message: "^GET /health"

2️⃣ 只保留某些日志(白名单)

processors:
  - drop_event.when.not:
      contains:
        message: "ERROR"

意思是:不是 ERROR 的都丢掉。


3️⃣ 按字段过滤(如日志级别)

如果日志是 JSON:

{"level":"info","msg":"test"}
processors:
  - drop_event.when:
      equals:
        level: "debug"

4️⃣ 丢弃某个路径的日志

processors:
  - drop_event.when:
      equals:
        log.file.path: "/var/log/app/debug.log"

5️⃣ 添加 / 删除字段

删除字段:

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

添加字段:

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

6️⃣ 条件组合(AND / OR)

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

三、测试配置是否正确(Debian)

sudo filebeat test config
sudo filebeat test output

四、调试 Filebeat(非常重要)

开启调试:

sudo filebeat -e -d "*"

或临时输出到 console:

output.console:
  pretty: true

五、重要提醒(很多人踩坑)

✅ Filebeat 不能做复杂聚合
❌ 不适合做 SQL 级别分析
✅ 复杂过滤推荐:

  • Logstash(grok + if)
  • Elasticsearch Ingest Pipeline

典型架构:

Filebeat → Logstash → Elasticsearch

六、简单示例(完整)

filebeat.inputs:
  - type: log
    paths:
      - /var/log/app/*.log
    processors:
      - drop_event.when:
          contains:
            message: "health"

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

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

  • 日志格式(文本 / JSON)
  • 想过滤什么内容
  • 输出到 ES 还是 Logstash

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

0 踩