1. 安装Filebeat
根据操作系统选择对应安装方式:
sudo yum install filebeat -y;sudo apt update && sudo apt install filebeat -y。/etc/filebeat/filebeat.yml。2. 配置监控特定文件
编辑filebeat.yml文件,重点修改filebeat.inputs部分,通过paths参数指定要监控的文件或目录路径:
/var/log/syslog:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/syslog
*,例如监控/var/log下所有.log文件:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
exclude_files参数排除不需要的文件(如压缩文件),例如排除所有.gz文件:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
exclude_files: ['\.gz$']
/opt/myapp/logs/app.log:filebeat.inputs:
- type: log
enabled: true
paths:
- /opt/myapp/logs/app.log
3. 可选:优化采集配置
根据需求调整以下参数,提升采集效率和针对性:
ignore_older参数忽略超过指定时间的旧日志(单位:小时),避免重复采集,例如忽略72小时以上的旧文件:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/syslog
ignore_older: 72h
scan_frequency参数设置Filebeat检查文件变化的间隔时间(默认10秒),例如每5秒检查一次:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/syslog
scan_frequency: 5s
tags或fields参数为日志事件添加标识,便于后续过滤和分析,例如添加app: myapp标签和env: production字段:filebeat.inputs:
- type: log
enabled: true
paths:
- /opt/myapp/logs/app.log
tags: ["myapp"]
fields:
env: production
fields_under_root: true
4. 配置输出目标
根据需求设置日志发送的目标(如Elasticsearch、Logstash),以Elasticsearch为例:
output.elasticsearch:
hosts: ["localhost:9200"] # 替换为Elasticsearch实际地址
index: "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}" # 动态生成日期索引
若输出到Logstash,需修改为:
output.logstash:
hosts: ["logstash:5044"] # 替换为Logstash实际地址
5. 启动并验证Filebeat
sudo systemctl start filebeat启动Filebeat;sudo systemctl enable filebeat,确保系统重启后自动启动;sudo systemctl status filebeat,确认服务状态为active (running);sudo journalctl -u filebeat -f,实时查看Filebeat运行日志,排查错误;curl -X GET "localhost:9200/_cat/indices?v"查看是否存在filebeat-*索引,确认数据是否采集成功。注意事项
chown或chmod调整权限);inotify限制(编辑/etc/sysctl.conf,添加fs.inotify.max_user_watches=524288,然后运行sudo sysctl -p生效);filebeat -e -c /etc/filebeat/filebeat.yml前台运行测试配置文件语法是否正确。