在CentOS系统中,Filebeat的日志轮转可以通过配置logrotate工具来实现。以下是详细的步骤和配置示例:
首先,确保你已经安装了Filebeat。如果还没有安装,可以使用以下命令进行安装:
sudo yum install filebeat
编辑Filebeat的配置文件,通常位于/etc/filebeat/filebeat.yml。确保配置文件中有以下内容:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["localhost:9200"]
创建一个新的logrotate配置文件来管理Filebeat的日志轮转。你可以使用以下命令创建一个名为filebeat的文件:
sudo vi /etc/logrotate.d/filebeat
在文件中添加以下内容:
/var/log/filebeat/*.log {
daily
missingok
rotate 7
compress
notifempty
create 640 root root
}
解释:
daily: 每天轮转一次日志。missingok: 如果日志文件丢失,不会报错。rotate 7: 保留7天的日志文件。compress: 压缩旧的日志文件。notifempty: 如果日志文件为空,则不进行轮转。create 640 root root: 轮转后创建新的日志文件,权限为640,属主为root,属组为root。你可以手动测试logrotate配置是否正确:
sudo logrotate -f /etc/logrotate.d/filebeat
这个命令会强制执行一次日志轮转,并应用你刚刚创建的配置。
logrotate通常由系统的cron任务自动执行。你可以检查/etc/cron.daily/logrotate文件,确保它包含了调用logrotate的命令:
/etc/cron.daily/logrotate
如果没有,可以手动添加以下内容:
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
然后确保该脚本有执行权限:
sudo chmod +x /etc/cron.daily/logrotate
你可以通过查看/var/log/filebeat/目录下的日志文件来监控日志轮转是否正常工作。例如:
ls -l /var/log/filebeat/
你应该能看到按日期命名的日志文件,并且旧文件已经被压缩。
通过以上步骤,你就可以在CentOS系统中成功配置Filebeat的日志轮转。