下面给你一个在 Debian 系统上配置 Filebeat 的完整示例,从安装到常见使用场景(如收集日志并发送到 Elasticsearch 或 Logstash)。
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.13.4-amd64.deb
sudo dpkg -i filebeat-8.13.4-amd64.deb
版本号可根据需要调整,建议与 Elasticsearch 版本一致。
/etc/filebeat/filebeat.yml
通常你只需要修改这个文件。
/var/log/*.logfilebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["localhost:9200"]
启动并测试:
sudo filebeat test config
sudo filebeat test output
sudo systemctl restart filebeat
output.elasticsearch:
hosts: ["localhost:9200"]
如果 ES 有安全认证:
output.elasticsearch:
hosts: ["https://es-node:9200"]
username: "elastic"
password: "your_password"
ssl.verification_mode: none
output.logstash:
hosts: ["127.0.0.1:5044"]
Logstash 示例配置:
input {
beats {
port => 5044
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
}
}
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/nginx/*.log
- type: filestream
enabled: true
paths:
- /var/log/mysql/*.log
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/app/*.log
fields:
app: myapp
fields_under_root: true
Filebeat 自带很多常见日志模块(nginx、mysql、systemd 等)。
filebeat modules list
sudo filebeat modules enable nginx
/etc/filebeat/modules.d/nginx.yml
sudo systemctl enable filebeat
sudo systemctl start filebeat
sudo systemctl status filebeat
sudo journalctl -u filebeat -f
或:
/var/log/filebeat/filebeat
filebeat test outputcurl localhost:9200/_cat/indices
sudo chmod 644 /var/log/*.log
Filebeat 不会修改日志时间,Elasticsearch 使用 @timestamp
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["localhost:9200"]
如果你愿意,可以告诉我:
我可以给你一份完全贴合你环境的 Filebeat 配置。