在CentOS系统中,Filebeat本身不负责日志轮转。日志轮转通常由logrotate工具来处理。但是,你可以配置Filebeat将日志发送到logrotate管理的日志文件中。以下是如何设置Filebeat和logrotate的步骤:
安装Filebeat: 如果你还没有安装Filebeat,请使用以下命令安装:
sudo yum install filebeat
配置Filebeat:
编辑Filebeat配置文件/etc/filebeat/filebeat.yml
,确保它指向你想要监控的日志文件。例如,如果你想要监控Apache的访问日志,你可以这样设置:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/httpd/access_log
安装和配置logrotate:
logrotate通常已经预装在大多数Linux发行版中。你可以通过检查/etc/logrotate.d/
目录来确认它是否已经安装。
创建logrotate配置文件:
如果你需要为Filebeat监控的日志创建一个特定的logrotate配置,你可以在/etc/logrotate.d/
目录下创建一个新的配置文件。例如,创建一个名为filebeat
的文件:
sudo vi /etc/logrotate.d/filebeat
在这个文件中,添加以下内容:
/var/log/httpd/access_log {
daily
missingok
rotate 7
compress
notifempty
create 640 root adm
postrotate
if [ -f /var/run/filebeat/filebeat.pid ]; then
/usr/share/filebeat/filebeat -e -c /etc/filebeat/filebeat.yml -d "*"
fi
endscript
}
这个配置将会每天轮转日志文件,保留最近7天的日志,并对旧日志进行压缩。postrotate
脚本会在日志轮转后重启Filebeat服务,以确保它开始读取新的日志文件。
测试logrotate配置: 你可以使用以下命令来测试logrotate配置是否正确:
sudo logrotate -f /etc/logrotate.d/filebeat
这个命令会强制执行logrotate配置,并且你应该能看到Filebeat重新启动的日志输出。
确保logrotate定时任务运行:
logrotate通常通过cron的daily定时任务来自动运行。你可以检查/etc/cron.daily/logrotate
文件来确认这一点。
通过以上步骤,你可以确保Filebeat监控的日志文件得到适当的轮转和管理。记得定期检查Filebeat和logrotate的配置,以确保它们符合你的需求。