Filebeat在Ubuntu上如何设置报警规则

作者:袖梨 2026-08-07

在 Ubuntu 上设置 Filebeat 报警规则的可行方案

Filebeat在Ubuntu上如何设置报警规则

核心要点

  1. Filebeat 本身不提供告警功能,通常将日志发往 Elasticsearch 或 Logstash,再由 Watcher(Elasticsearch 7.x)/ Kibana 告警(8.x+)、Logstash 告警输出、或 ElastAlert 等组件实现规则与通知。Watcher 在 7.x 中已弃用,8.x 移除,建议在新环境优先使用 Kibana 告警 或 ElastAlert。

方案一 使用 Kibana 告警规则 适用于 Elasticsearch 8.x 或订阅版

  1. 前置准备
    1. 部署并连通 Elasticsearch 8.x 与 Kibana,确保 Filebeat 已正确输出到 ES 索引(如 filebeat-*)。
  2. 配置 Filebeat(/etc/filebeat/filebeat.yml 示例)
    1. 输出到 ES:
      1. output.elasticsearch:
        1. hosts: [“localhost:9200”]
    2. 若使用模块(如 system、nginx),启用对应模块并加载仪表板:
      1. filebeat.modules:
        1. module: system
      2. setup.dashboards.enabled: true
  3. 在 Kibana 创建告警
    1. 打开 Kibana → Stack Management → Rules and Connectors,新建规则:
      1. 数据视图:选择 filebeat-* 或相应索引模式。
      2. 触发条件示例:使用 KQL 查询如 message: “ERROR”,设置阈值与时间窗口(如过去 1 分钟 命中数 > 0)。
      3. 动作:选择 Email/Webhook/Slack 等连接器并配置收件人、标题与正文模板。
    2. 保存并启用规则,观察是否按预期触发与通知。

方案二 使用 Elasticsearch Watcher 适用于 Elasticsearch 7.x

  1. 前置准备
    1. 启用 X-Pack 并确保 Watcher 可用(Watcher 在 7.x 已弃用,8.x 移除,不建议新部署)。
  2. 创建 Watcher(Kibana Dev Tools 或 API)
    1. 每分钟检查 filebeat-* 中是否出现 ERROR,触发则发邮件:
      1. PUT _watcher/watch/filebeat_error_alert
        1. trigger: { schedule: { interval: “1m” } }
        2. input:
          1. search:
            1. request:
              1. indices: [“filebeat-*”]
              2. body:
                1. query:
                  1. bool:
                    1. must:
                      1. range: { “@timestamp”: { gte: “now-1m”, lte: “now” } }
                      2. match: { message: “ERROR” }
        3. condition: { compare: { “ctx.payload.hits.total”: { gt: 0 } } }
        4. actions:
          1. email:
            1. to: “[email protected]
            2. subject: “Filebeat Alert: ERROR detected”
            3. body: “Found {{ctx.payload.hits.total}} ERROR events in the last minute.”
  3. 说明
    1. 需提前在 Elasticsearch 中配置邮件发送(如 xpack.notification.email),并确保 SMTP 可达;Watcher 仅建议用于 7.x 存量环境。

方案三 使用 ElastAlert 适用于自建规则引擎

  1. 安装与配置
    1. pip 安装 ElastAlert,创建配置文件(elastalert_config.yaml):
      1. rule_folder: /path/to/rules
      2. run_every: { minutes: 1 }
      3. buffer_time: { minutes: 15 }
      4. es_host: localhost
      5. es_port: 9200
  2. 定义规则(/path/to/rules/error_alert.yaml)
    1. type: frequency
    2. index: filebeat-*
    3. num_events: 1
    4. timeframe: { minutes: 1 }
    5. filter:
      1. query:
        1. query_string:
          1. query: “ERROR”
    6. alert: email
    7. email:
      1. [email protected]
  3. 运行与自启
    1. elastalert --config /path/to/elastalert_config.yaml
    2. 建议使用 systemd 或 supervisord 托管进程,确保稳定运行。

方案四 使用 Logstash 在管道内触发告警

  1. 思路
    1. Filebeat 将日志发往 Logstash(beats 输入),在 Logstash 过滤后按条件通过 email 输出插件发送告警。
  2. 最小配置示例(/etc/logstash/conf.d/alert.conf)
    1. input { beats { port => 5044 } }
    2. filter {if [message] =~ /ERROR/ {mutate { add_tag => [“alert_error”] }}}
    3. output {if “alert_error” in [tags] {email {to => “[email protected]”subject => “Logstash Alert: ERROR found”body => “Message: %{[message]}”via => “smtp”smtp {host => “smtp.example.com”port => 587user => “[email protected]”password => “your-password”authentication => “plain”}}}elasticsearch { hosts => [“localhost:9200”] index => “filebeat-%{+YYYY.MM.dd}” }}
  3. 说明
    1. 适合已有 Logstash 链路、希望就近在管道内做简单阈值判断与通知的场景。

相关文章

精彩推荐