在 Debian 上用 Filebeat 收集日志,通常分为 安装 → 配置 → 启动 → 验证 几个步骤。下面以 收集系统日志并发送到 Elasticsearch / Logstash 为例说明。
sudo apt update
sudo apt install curl gnupg -y
curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/elastic-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
sudo apt update
sudo apt install filebeat -y
如果是 Elasticsearch 7.x,把
8.x改成7.x
配置文件位置:
/etc/filebeat/filebeat.yml
编辑配置文件:
sudo nano /etc/filebeat/filebeat.yml
示例(收集 /var/log/*.log):
filebeat.inputs:
- type: filestream
id: syslog
paths:
- /var/log/*.log
- /var/log/syslog
output.elasticsearch:
hosts: ["http://localhost:9200"]
username: "elastic"
password: "your_password"
output.logstash:
hosts: ["localhost:5044"]
例如收集系统日志、认证日志:
sudo filebeat modules enable system
查看已启用模块:
sudo filebeat modules list
sudo filebeat test config
sudo filebeat test output
sudo systemctl enable filebeat
sudo systemctl start filebeat
systemctl status filebeat
curl -u elastic:password http://localhost:9200/_cat/indices?v
filebeat-*filebeat.inputs:
- type: filestream
paths:
- /opt/myapp/logs/*.log
multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
multiline.negate: true
multiline.match: after
查看日志:
journalctl -u filebeat -f
或:
cat /var/log/filebeat/filebeat
如果你愿意,可以告诉我:
我可以给你一份 可直接用的配置文件。