在CentOS系统上,Filebeat的日志轮转可以通过配置logrotate工具来实现。以下是配置Filebeat日志轮转的步骤:
1. 确保logrotate已安装
首先,确保你的CentOS系统上已经安装了logrotate
。如果没有安装,可以使用以下命令进行安装:
sudo yum install logrotate -y
2. 创建logrotate配置文件
Filebeat的日志文件通常位于/var/log/filebeat/filebeat
。你需要为这个文件创建一个logrotate配置文件。
创建一个新的配置文件:
sudo vi /etc/logrotate.d/filebeat
3. 配置logrotate
在打开的文件中,添加以下内容:
/var/log/filebeat/filebeat { daily missingok rotate 7 compress notifempty create 0640 root root sharedscripts postrotate if [ -f /var/run/filebeat/filebeat.pid ]; then /usr/share/filebeat/filebeat -e -c /etc/filebeat/filebeat.yml -d "*" fi endscript}
解释一下这些配置项:
daily
: 每天轮转日志。missingok
: 如果日志文件丢失,不会报错。rotate 7
: 保留7个轮转日志文件。compress
: 压缩旧的日志文件。notifempty
: 如果日志文件为空,则不进行轮转。create 0640 root root
: 创建新的日志文件,权限为0640,属主和属组为root。sharedscripts
: 如果有多个日志文件匹配,只执行一次postrotate脚本。postrotate
: 轮转后执行的脚本。这里重新启动Filebeat以确保它读取新的日志文件。
4. 测试logrotate配置
你可以手动测试logrotate配置是否正确:
sudo logrotate -f /etc/logrotate.d/filebeat
如果一切正常,你应该会看到类似以下的输出:
reading config file /etc/logrotate.d/filebeat...rotating pattern: /var/log/filebeat/filebeat daily (7 rotations)empty log files are not rotated, old versions are removedconsidering log /var/log/filebeat/filebeat log does not exist -- skipping
5. 确保logrotate定时任务运行
logrotate
通常由系统的cron任务自动管理。你可以检查/etc/cron.daily/logrotate
文件,确保它包含了以下内容:
#!/bin/sh/usr/sbin/logrotate /etc/logrotate.confEXITVALUE=https://www.yisu.com/ask/$?"ALERT exited abnormally with [$EXITVALUE]"fiexit 0
这个脚本会每天运行一次,检查并执行所有需要轮转的日志文件。
通过以上步骤,你应该能够在CentOS上成功配置Filebeat的日志轮转。