希望将实时/var/log/audit/audit.log文件的每周或每月保存为具有audit_2020-05-05.log.gz之类的压缩文件
在RHEL/CentOS 7.x中,是否有一种优雅的方式在现有的审计..conf文件中实现以下操作?
否则,简单地执行根crontab将运行本地bash脚本的最佳方法是.
service auditd stopcp /var/log/audit/audit.log /var/log/audit/audit_.logservice auditd startgzip -9 /var/log/audit/audit_.log如果可能的话,我更愿意在/etc/audit/文件的范围内这样做,这就是我为什么要问的原因。但是我强烈希望audit_.log.gz文件每个星期/每个月都有特定的文件命名约定。
最终目标是以可靠和可靠的方式管理所创建的审计日志档案。将保存的任何一个审计日志文本文件保持在未压缩大小小于1GB的范围内,从而相应地调整日志保存/旋转。同时,也不要丢失任何审计日志,或者根据audit.conf设置将系统带入单用户模式。所以比我现在想的更好的方法我会很高兴听到的。
发布于 2020-10-15 17:15:53
默认情况下,所有版本的中的auditd在达到一定大小时都会自动旋转自己的日志文件,这是由max_log_file设置(默认为6MB)确定的。
禁用/etc/审核/auditd.conf中的轮换,以便:
max_log_file_action = ignore杀死-HUP $(pidof auditd) (任何版本) systemctl重新加载auditd (RHEL7)服务auditd重载(RHEL6和更早版本)
每日轮换的简单解决方案:将auditd.cron复制到cron.daily
~]# cp /usr/share/doc/audit-*/auditd.cron /etc/cron.daily
~]# chmod +x /etc/cron.daily/auditd.cron
~]# cat /etc/cron.daily/auditd.cron
#!/bin/sh
##########
# This script can be installed to get a daily log rotation
# based on a cron job.
##########
/sbin/service auditd rotate
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t auditd "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0Implementing日志压缩
auditd不支持日志压缩;但是,更新上面的脚本来重命名旧的audit.log.n文件并压缩它们是很简单的。为了演示目的,提供了一个工作示例。
按照上述步骤禁用基于大小的自动旋转。
用以下代码替换以前创建的脚本:
#!/bin/bash
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
FORMAT="%F_%T" # Customize timestamp format as desired, per `man date`
# %F_%T will lead to files like: audit.log.2015-02-26_15:43:46
COMPRESS=gzip # Change to bzip2 or xz as desired
KEEP=5 # Number of compressed log files to keep
ROTATE_TIME=5 # Amount of time in seconds to wait for auditd to rotate its logs. Adjust this as necessary
rename_and_compress_old_logs() {
for file in $(find /var/log/audit/ -name 'audit.log.[0-9]'); do
timestamp=$(ls -l --time-style="+${FORMAT}" ${file} | awk '{print $6}')
newfile=${file%.[0-9]}.${timestamp}
# Optional: remove "-v" verbose flag from next 2 lines to hide output
mv -v ${file} ${newfile}
${COMPRESS} -v ${newfile}
done
}
delete_old_compressed_logs() {
# Optional: remove "-v" verbose flag to hide output
rm -v $(find /var/log/audit/ -regextype posix-extended -regex '.*audit\.log\..*(xz|gz|bz2)按需要修改FORMAT、COMPRESS和KEEP的声明确保脚本被标记为可执行文件,并将其设置为在所需时间由cron调用(通过普通cron作业或将其放入cron.daily中,如上面所示) | sort -n | head -n -${KEEP})
}
rename_and_compress_old_logs
service auditd rotate
sleep $ROTATE_TIME
rename_and_compress_old_logs
delete_old_compressed_logs按需要修改D19、D20和D21的声明
确保脚本被标记为可执行文件,并将其设置为在所需时间由cron调用(通过普通cron作业或将其放入cron.daily中,如上面所示)
https://unix.stackexchange.com/questions/586518
复制相似问题