温馨提示×

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

小樊
35
2025-12-22 21:26:44
栏目: 智能运维

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

核心要点

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

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

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

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

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

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

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

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

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

0