Ubuntu系統下Filebeat實現告警功能的完整流程
Filebeat本身不具備原生告警功能,需結合Elastic Stack(Elasticsearch、Kibana)或其他第三方工具(如ElastAlert)實現。以下是兩種主流方案的詳細步驟:
sudo apt-get update && sudo apt-get install filebeat
/etc/filebeat/filebeat.yml
,啟用Elasticsearch輸出并指定主機地址:output.elasticsearch:
hosts: ["localhost:9200"] # 若Elasticsearch不在本地,替換為對應IP/域名
sudo systemctl enable filebeat && sudo systemctl start filebeat
sudo apt-get install elasticsearch
sudo systemctl start elasticsearch
sudo apt-get install kibana
sudo systemctl start kibana
filebeat-*
索引是否存在)。http://localhost:5601
),導航至Stack Management > Alerts and Actions > Manage alerts
。error
級別的日志):{
"query": {
"bool": {
"must": [{"match": {"message": "error"}}]
}
}
}
ctx.payload.hits.total > 0
)。filebeat-*
”)。/var/log/syslog
)寫入一條包含error
的日志:echo "This is a test error log" | sudo tee -a /var/log/syslog
若不想依賴Kibana,可使用ElastAlert(輕量級告警工具)實現,步驟如下:
sudo apt-get install python-pip
sudo pip install elastalert
/etc/elastalert/config.yaml
中添加以下內容(替換為你的SMTP信息):es_host: localhost
es_port: 9200
name: "Filebeat Error Alert"
type: frequency
index: filebeat-*
num_events: 1 # 觸發條件:1分鐘內出現1次錯誤日志
timeframe:
minutes: 1
filter:
- query:
query_string:
query: "message:error" # 過濾包含“error”的日志
alert:
- email
email:
- "your-email@example.com"
smtp_host: smtp.example.com
smtp_port: 587
smtp_user: your-smtp-user
smtp_password: your-smtp-password
smtp_from: elastalert@example.com
smtp_tls: true
/etc/elastalert/rules/
目錄下創建YAML規則文件(如error_rule.yaml
):name: "Error Log Detection"
type: frequency
index: filebeat-*
num_events: 1
timeframe:
minutes: 1
filter:
- query:
query_string:
query: "message:error"
alert:
- email
email:
- "your-email@example.com"
elastalert --config /etc/elastalert/config.yaml --rule /etc/elastalert/rules/error_rule.yaml
nohup
或systemd
(需創建服務文件)讓ElastAlert持續運行:nohup elastalert --config /etc/elastalert/config.yaml --rule /etc/elastalert/rules/error_rule.yaml > /var/log/elastalert.log 2>&1 &
error
日志(同方案一的測試步驟),檢查郵箱是否收到告警郵件。num_events
、timeframe
),避免誤報或漏報。通過以上步驟,即可在Ubuntu系統上實現Filebeat的告警功能,及時響應日志中的異常情況。