在 Ubuntu 上,Filebeat 本身不提供内置告警,常见做法是将日志送入 Elasticsearch/Kibana 或 Logstash,再基于这些组件配置规则与通知。下面给出 3 种常用方案与可直接套用的示例。
| 方案 | 组件依赖 | 适用场景 | 通知方式 |
|---|---|---|---|
| Elasticsearch Watcher | Elasticsearch(建议 7.x/8.x 基础版及以上)、Kibana(可选) | 已在用 ES 做检索分析,需原生定时规则 | Email、Webhook 等 |
| ElastAlert | ElastAlert、Elasticsearch | 需要更灵活的匹配规则(频次、阈值、复合条件) | Email、钉钉、企业微信、Slack 等 |
| Logstash 告警输出 | Logstash | 已在用 Logstash 做处理,想就近发信 | Email(SMTP) 等 |
output.elasticsearch:
hosts: ["localhost:9200"]
index: "filebeat-%{+yyyy.MM.dd}"
PUT _watcher/watch/filebeat-error-alert
{
"trigger": { "schedule": { "interval": "1m" } },
"input": {
"search": {
"request": {
"indices": ["filebeat-*"],
"body": {
"query": { "match": { "message": "ERROR" } },
"size": 0
}
}
}
},
"condition": {
"compare": { "ctx.payload.hits.total.value": { "gt": 0 } }
},
"actions": {
"email_admin": {
"email": {
"to": "admin@example.com",
"subject": "Filebeat Alert: ERROR detected",
"body": "Found {{ctx.payload.hits.total.value}} ERROR logs in the last minute."
}
}
}
}
rule_folder: /etc/elastalert/rules
run_every:
minutes: 1
buffer_time:
minutes: 15
es_host: localhost
es_port: 9200
name: Filebeat ERROR 1m
type: frequency
index: filebeat-*
num_events: 1
timeframe:
minutes: 1
filter:
- query:
query_string:
query: "message:ERROR"
alert:
- email
email:
- "admin@example.com"
如需钉钉,可改用 webhook 类型并配置钉钉机器人 access_token。elastalert --config /etc/elastalert/elastalert_config.yaml
input {
beats { port => 5044 }
}
filter {
# 按需解析与丰富字段
}
output {
if [message] =~ /ERROR/ {
email {
to => "admin@example.com"
subject => "Logstash Alert: ERROR found"
body => "Message: %{[message]}"
via => "smtp"
smtp {
host => "smtp.example.com"
port => 587
user => "you@example.com"
password => "yourpass"
authentication => "plain"
enable_starttls_auto => true
}
}
}
elasticsearch {
hosts => ["localhost:9200"]
index => "filebeat-%{+YYYY.MM.dd}"
}
}
说明:适合已在 Logstash 做处理的链路,就近利用 SMTP 发信。