在Ubuntu系统上,推荐通过Elastic官方APT仓库安装Filebeat,确保版本更新及时且安装过程便捷。
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/filebeat.list
sudo apt-get update && sudo apt-get install filebeat
Filebeat的核心配置文件位于/etc/filebeat/filebeat.yml,需修改以下关键部分:
syslog、auth.log):filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/syslog
- /var/log/auth.log
output.elasticsearch:
hosts: ["localhost:9200"] # 若Elasticsearch在远程服务器,替换为对应IP
index: "ubuntu-system-logs-%{+yyyy.MM.dd}" # 动态生成日期索引
output.logstash:
hosts: ["localhost:5044"] # Logstash默认监听端口为5044
nginx、mysql)可简化特定应用日志的收集与解析。例如,启用nginx模块:sudo filebeat modules enable nginx
启用后,Filebeat会自动读取/var/log/nginx/access.log和/var/log/nginx/error.log,并应用预定义的解析规则。filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.json
json.keys_under_root: true # 将JSON字段提升至事件顶层
json.add_error_key: true # 添加错误字段(解析失败时记录)
sudo systemctl start filebeat
sudo systemctl enable filebeat # 系统重启后自动启动
curl -X GET "localhost:9200/_cat/indices?v&pretty"
若看到ubuntu-system-logs-*或filebeat-*索引,说明数据已成功传输。tail -f /var/log/logstash/logstash-plain.log
查找Filebeat相关的日志条目(如Received event from Filebeat),确认数据接收情况。adm组(可访问系统日志):sudo usermod -aG adm filebeat
sudo systemctl restart filebeat
filebeat test config命令验证配置文件语法:sudo filebeat test config -e
若存在错误,命令会输出具体行号及错误信息,便于定位问题。通过以上步骤,可实现Filebeat在Ubuntu系统上的数据传输,满足日志收集、处理与存储的需求。根据实际场景(如日志量、处理复杂度),可灵活调整配置(如添加过滤规则、调整批次大小)。