ubuntu中filebeat的监控报警设置
导读:Ubuntu 上 Filebeat 监控报警设置 总体思路与前提 Filebeat 本身不提供告警,需与 Elasticsearch/Kibana 的 X-Pack Alerting(原 Watcher) 或第三方工具集成实现监控与通知。...
Ubuntu 上 Filebeat 监控报警设置
总体思路与前提
- Filebeat 本身不提供告警,需与 Elasticsearch/Kibana 的 X-Pack Alerting(原 Watcher) 或第三方工具集成实现监控与通知。
- 建议环境:Ubuntu 20.04/22.04/24.04,Filebeat 输出到 Elasticsearch 或 Logstash;若走 Kibana 告警,需 7.x 及以上版本并启用 X-Pack。
- 基本流程:Filebeat 采集日志 → 写入 filebeat-* 索引 → 在 Kibana 或 Elasticsearch Watcher API 定义阈值与动作(邮件、Slack 等)→ 触发后按渠道通知。
方案一 基于 Kibana X-Pack Alerting 的图形化配置
- 前置检查
- Filebeat 配置输出到 Elasticsearch(示例):
- output.elasticsearch.hosts: [“es-node1:9200”,“es-node2:9200”]
- 确认索引模式为 filebeat-*,Kibana 可正常检索到数据。
- Filebeat 配置输出到 Elasticsearch(示例):
- 在 Kibana 创建告警
- 路径:Stack Management → Alerts and Actions → Manage alerts → Create alert。
- 选择类型:日志类常用 Log threshold;指标类可用 Metric threshold。
- 配置条件(示例):
- Index pattern:选择 filebeat-*
- Time window:1m
- Condition:当 Number of hits > 100(可按需改为“Error logs > N”等)
- 配置通知渠道:
- 点击 Add action,选择 Email/Slack/Webhook 等;以邮件为例,填写 To/Subject/Body。
- 若尚未配置邮件,先在 Stack Management → Connectors 中创建 Email connector(需提供 SMTP 信息)。
- 保存并激活规则,观察 1 个评估周期内是否触发。
方案二 基于 Elasticsearch Watcher 的 API 配置
- 适用场景:无图形界面或需批量/自动化管理告警规则。
- 创建示例规则(每分钟检查最近 1 分钟内包含 “error” 的日志,超过 5 条则发邮件)
- 通过 API 创建:
- curl -X PUT “localhost:9200/_watcher/watch/error_alert” -H ‘Content-Type: application/json’ -d’ { “trigger”: { “schedule”: { “interval”: “1m” } } , “input”: { “search”: { “request”: { “indices”: [“filebeat-*”], “body”: { “query”: { “bool”: { “must”: [ { “match”: { “message”: “error” } } ], “filter”: [ { “range”: { “@timestamp”: { “gte”: “now-1m”, “lte”: “now” } } } ] } } } } } } , “condition”: { “compare”: { “ctx.payload.hits.total”: { “gt”: 5 } } } , “actions”: { “email_admin”: { “email”: { “to”: “admin@example.com”, “subject”: “Elasticsearch Alert: High Error Logs”, “body”: “The following error logs were detected:\n{ { #ctx.payload.hits.hits} } { { _source.message} } \n{ { /ctx.payload.hits.hits} } ” } } } } ’
- 通过 API 创建:
- 邮件发送要求
- 需在 Elasticsearch 配置 SMTP(示例,elasticsearch.yml):
- xpack.notification.email.account.default.smtp.host: “smtp.example.com”
- xpack.notification.email.account.default.smtp.port: 587
- xpack.notification.email.account.default.smtp.user: “your_email@example.com”
- xpack.notification.email.account.default.smtp.password: “your_password”
- xpack.notification.email.account.default.smtp.authentication: “plain”
- xpack.notification.email.account.default.starttls.enable: true
- xpack.notification.email.account.default.from: “alert@example.com”
- 修改后重启 Elasticsearch 使配置生效。
- 需在 Elasticsearch 配置 SMTP(示例,elasticsearch.yml):
方案三 不依赖 X-Pack 的替代工具 ElastAlert
- 适用场景:开源免费、规则灵活(频率、spike、flatline 等)。
- 快速步骤
- 安装 ElastAlert(示例):
- sudo apt-get install python3-pip
- sudo pip3 install elastalert
- 配置 /etc/elastalert/config.yaml(示例):
- rules_folder: /etc/elastalert/rules
- es_host: localhost
- es_port: 9200
- smtp_host: smtp.example.com
- smtp_port: 587
- smtp_user: your_email@example.com
- smtp_password: your_password
- from_addr: elastalert@example.com
- 新建规则 /etc/elastalert/rules/log_error_rule.yaml(示例):
- name: “High Error Log Count”
- type: frequency
- index: filebeat-*
- num_events: 10
- timeframe: { minutes: 1 }
- filter: [{ “query_string”: { “query”: “message: error” } } ]
- alert: email
- email: [“your_email@example.com”]
- 启动(测试):elastalert --config /etc/elastalert/config.yaml --rule /etc/elastalert/rules/log_error_rule.yaml
- 建议配置为 systemd 服务长期运行。
- 安装 ElastAlert(示例):
验证与排错要点
- 数据链路验证
- 确认 Filebeat 已写入 filebeat-*:在 Kibana Discover 或 ES 中查询最近 1–5 分钟数据。
- 检查 Filebeat 与 ES/Kibana 连通性与日志:
- sudo systemctl status filebeat
- journalctl -u filebeat -f
- 告警评估与触发
- Watcher/Kibana 规则评估间隔建议从 1m 开始;先用更易触发的阈值(如 > 1)验证,再调回生产阈值。
- 邮件未收到时,优先检查 SMTP 配置、收件人白名单、是否被反垃圾拦截;Kibana 中查看 告警执行历史与 动作日志。
- 性能与稳定性
- 合理设置查询时间窗口与聚合,避免大查询影响集群;必要时为告警查询增加时间过滤与索引优化。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: ubuntu中filebeat的监控报警设置
本文地址: https://pptw.com/jishu/767072.html
