温馨提示×

centos filebeat如何过滤数据

小樊
40
2026-10-05 21:33:24
栏目: 智能运维

在 CentOS 上使用 Filebeat 过滤数据,通常是通过 Filebeat 自身的 processors 或结合 Logstash / Elasticsearch Ingest Pipeline 来实现。下面按常见场景给你说明。


一、Filebeat 自身过滤(推荐先做)

Filebeat 支持在 filebeat.yml 中使用 processors 对数据进行过滤、裁剪、丢弃。

1️⃣ 丢弃不需要的日志(drop_event)

例如:过滤掉包含 DEBUG 的日志

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

2️⃣ 只保留某些字段(drop_fields)

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

3️⃣ 条件过滤(if / contains / regexp)

processors:
  - drop_event:
      when:
        regexp:
          message: "^test.*"

4️⃣ 多条件过滤

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

二、按日志路径或类型过滤

1️⃣ 多 input 分别控制

filebeat.inputs:
  - type: log
    paths:
      - /var/log/app/*.log
    tags: ["app"]

  - type: log
    paths:
      - /var/log/nginx/*.log
    tags: ["nginx"]

在 processors 中按 tag 过滤:

processors:
  - drop_event:
      when:
        contains:
          tags: "nginx"

三、结构化日志(JSON)过滤

如果是 JSON 日志:

filebeat.inputs:
  - type: log
    paths:
      - /var/log/app.json
    json.keys_under_root: true
    json.add_error_key: true

然后按字段过滤:

processors:
  - drop_event:
      when:
        equals:
          status: 200

四、使用 Logstash 做高级过滤(Filebeat → Logstash)

如果过滤逻辑复杂,推荐:

Filebeat → Logstash → ES

Logstash 示例

filter {
  if [message] =~ "DEBUG" {
    drop {}
  }
}

五、CentOS 上常用操作

修改配置

vi /etc/filebeat/filebeat.yml

校验配置

filebeat test config

重启 Filebeat

systemctl restart filebeat
systemctl status filebeat

六、常见误区

❌ 在 Elasticsearch 里过滤(太晚)
✅ 在 Filebeat / Logstash 里过滤(更早、省资源)


如果你能给我:

  • 日志样例
  • 想过滤的规则(如“去掉 DEBUG / 只留 error / 去掉某个字段”)

我可以直接帮你写 可用的 filebeat.yml 配置。

0 踩