定制CentOS Filebeat的日志采集規則,主要通過編輯其配置文件/etc/filebeat/filebeat.yml實現,以下是具體方法:
sudo yum install filebeat進行安裝。filebeat.inputs部分,通過paths字段指定要采集的日志文件路徑,可使用通配符,如paths: - /var/log/*.log。fields字段中添加自定義字段,如fields: {application: "myapp", environment: "production"},若要將這些字段放在頂級字段,可設置fields_under_root: true。include_lines和exclude_lines字段來包含或排除特定日志行,如include_lines: ['^ERR', '^WARN'],exclude_lines: ['^dbg']。multiline字段配置,如multiline.pattern: '^\[',multiline.negate: true,multiline.match: 'after'等。output部分,指定日志輸出目標,如輸出到Elasticsearch可配置output.elasticsearch,設置主機地址和端口等;輸出到Logstash可配置output.logstash。sudo systemctl restart filebeat重啟服務使配置生效。sudo journalctl -u filebeat -f,或查看輸出目標(如Elasticsearch)來確認配置是否正確。