定制Ubuntu Filebeat的日志采集規則,主要通過編輯配置文件/etc/filebeat/filebeat.yml
實現,以下是具體方法:
sudo apt-get update
和sudo apt-get install filebeat
進行安裝。filebeat.inputs
部分,通過paths
參數指定要采集的日志文件路徑,可使用通配符。如filebeat.inputs: - type: log enabled: true paths: - /var/log/*.log
。fields
參數添加自定義字段,fields_under_root
設置為true
可將字段提升到事件頂層。例如fields: app: myapp environment: production fields_under_root: true
。include_lines
和exclude_lines
參數進行行級過濾,用正則表達式指定包含或排除的行。如include_lines: ['^ERROR', '^WARN']
。multiline
參數。pattern
指定多行起始行的正則,negate
和match
控制合并方式。output
部分,指定日志輸出的目標,如Elasticsearch、Logstash等。以輸出到Elasticsearch為例,配置為output.elasticsearch: hosts: ["localhost:9200"]
。scan_frequency
、close_inactive
等參數,以平衡實時性與資源消耗。