使用操作系统:Ubuntu 22.04 LTS,redis已经提前安装并能正确使用,此处不再介绍。
安装filebeat
Ubuntu使用apt-get方式安装filebeat,参考:官方apt安装方式
1、下载并安装公共签名密钥:
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
2、在Debian上安装apt-transport https包:
sudo apt-get install apt-transport-https
3、将存储库定义保存到/etc/apt/sources.list.d/electric-8.x.list:
echo "deb https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-8.x.list
4、运行apt-get update,存储库就可以使用了。可以通过运行以下命令来安装Filebeat:
sudo apt-get update && sudo apt-get install filebeat
5、要将Filebeat配置为自动启动,请运行:
sudo systemctl enable filebeat
如果您的系统不使用systemd,请运行:
sudo update-rc.d filebeat defaults 95 10
配置filebeat
1、配置filebeat输入,此处以收集错误日志为例,由于最新版本的 Log Input 已经弃用,所以需要使用filestream input来配置。
filebeat.inputs:
- type: filestream
id: erp-error-log-id
enabled: true
paths:
- /var/erp/logs/error.log
fields:
source: erp-error-log
fields_under_root: true
2、配置filebeat输出到redis,可参考下面链接
https://www.elastic.co/guide/en/beats/filebeat/8.4/redis-output.html
3、完整配置
filebeat.inputs:
- type: filestream
id: erp-error-log-id
enabled: true
paths:
- /var/erp/logs/error.log
fields:
source: erp-error-log
fields_under_root: true
output.redis:
hosts: ["127.0.0.1:6379"]
key: application-error-log
password: xxxxxxxx
db: 1
如果需要收集多个日志,可以添加多个type。
启动和停止filebeat
启动filebeat,参考Start FileBeat
sudo service filebeat start
停止filebeat
sudo service filebeat stop