在CentOS上配置Filebeat的告警通知,通常需要結合Elastic Stack中的其他組件,如Elasticsearch和Kibana,以及使用ElastAlert或其他告警工具。以下是一個基本的步驟指南,展示如何配置Filebeat將日志發送到Elasticsearch,并使用ElastAlert來設置告警通知。
下載并安裝Filebeat:
sudo wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.10.0-amd64.deb
sudo dpkg -i filebeat-7.10.0-amd64.deb
啟動并啟用Filebeat服務:
sudo systemctl start filebeat
sudo systemctl enable filebeat
編輯Filebeat配置文件:
sudo vi /etc/filebeat/filebeat.yml
配置輸出到Elasticsearch:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["localhost:9200"]
index: "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}"
安裝ElastAlert:
sudo pip install elastalert
創建ElastAlert配置文件:
sudo mkdir /etc/elastalert
sudo cp /usr/share/elastalert/config.yaml.example /etc/elastalert/config.yaml
sudo vi /etc/elastalert/config.yaml
配置ElastAlert規則:
編輯/etc/elastalert/rules.json
文件,添加你的告警規則。例如:
{
"type": "frequency",
"name": "High Error Rate",
"description": "High error rate detected",
"category": "Logs",
"filter":
[
{
"term":
{
"log.level": "ERROR"
}
}
],
"window_size": "1h",
"threshold": 100,
"num_events": 1
}
配置ElastAlert與Elasticsearch連接:
編輯/etc/elastalert/config.yaml
文件,確保ElastAlert可以連接到Elasticsearch:
rule_folder: /etc/elastalert/rules
run_every:
minutes: 1
buffer_time:
minutes: 15
es_host: localhost
es_port: 9200
創建ElastAlert日志目錄:
sudo mkdir /var/log/elastalert
啟動ElastAlert:
elastalert --config /etc/elastalert/config.yaml --rule /etc/elastalert/rules.json --verbose
ElastAlert支持多種通知方式,如電子郵件、Slack、PagerDuty等。以下是一個配置電子郵件通知的示例:
安裝elastalert-email-notifier
:
pip install elastalert-email-notifier
在config.yaml
中配置電子郵件通知:
email:
- address: "your-email@example.com"
name: "ElastAlert"
server: "smtp.example.com"
port: 587
username: "your-email@example.com"
password: "your-password"
tls: true
在規則文件中添加通知:
編輯/etc/elastalert/rules.json
文件,添加通知配置:
{
"type": "frequency",
"name": "High Error Rate",
"description": "High error rate detected",
"category": "Logs",
"filter":
[
{
"term":
{
"log.level": "ERROR"
}
}
],
"window_size": "1h",
"threshold": 100,
"num_events": 1,
"email":
[
{
"to": "your-email@example.com",
"subject": "High Error Rate Alert"
}
]
}
通過以上步驟,你可以在CentOS上配置Filebeat將日志發送到Elasticsearch,并使用ElastAlert設置告警通知。根據你的具體需求,可以進一步自定義和擴展這些配置。