secgateway/libs/files/deal_logfiles.sh

112 lines
2.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 本脚本功能:
# 1. 从logrotate配置文件中取出日志文件的路径所有日志文件均以logrotate配置文件中第一行指示的路径为前缀
# 2. 从log_sched配置文件中取出日志文件大小上限
# 3. 计算步骤1指示的日志文件的总大小检测日志总大小是否超出上限若超出则删除所有日志文件
function deal_logs ()
{
# logrotate配置文件路径
logrotate_log_file="/etc/logrotate.d/log-syslog"
#log_sched配置文件路径
log_sched_file="/etc/log-sched.conf"
# 日志文件前缀
prefix=
# 日志大小上限
size_max=0
echo "**********************************************************"
echo " logrotate_cfg: $logrotate_log_file"
echo " log_sched_cfg: $log_sched_file"
echo "**********************************************************"
echo ""
# logrotate配置文件判空
if [ ! -e $logrotate_log_file ] ; then
return
fi
# 从logrotate配置文件中第一行有效行中读取出日志文件前缀
while read line
do
if [ -z $line ] ; then
continue
fi
prefix=$line;
echo "**********************************************************"
echo " get log-file-prefix: $prefix"
echo "**********************************************************"
echo ""
break
done < $logrotate_log_file
if [ -z $prefix ] ; then
echo "get log file failed"
return
fi
# log_sched配置文件判空
if [ ! -e $log_sched_file ] ; then
return
fi
#从log_sched配置文件中读取出日志大小上限值
while read line
do
tmpkey="file.max_size="
if [[ $line = *$tmpkey* ]] ; then
if [[ $line = $tmpkey ]] ; then
echo "size_max is empty, do nothing and return"
return
fi
size_max=${line#$tmpkey}
echo "**********************************************************"
echo " get size_max: $size_max"
echo "**********************************************************"
echo ""
break
fi
done < $log_sched_file
if [ $size_max -eq 0 ] ; then
echo "size_max is 0, do nothing and return"
return
fi
#定期检查日志总大小是否超限
path=$prefix*
echo " get path: $path"
folder_size=$(wc -c $path|grep total |awk '{print $1}')
if [ -z $folder_size ] ; then
folder_size=0
fi
echo "cursize: $folder_size"
echo "maxsize: $size_max"
if [ $folder_size -gt $size_max ] ; then
echo "flush folder $path"
rm -rf $path
sync
fi
}
deal_logs $@