如何在日志上自动旋转

如何在日志上自动旋转

我们可以看到每天都会创建 atop 日志,并且占用大量空间

ls -l /var/log/atop/
total 1634632
-rw-r--r-- 1 root root 127992086 Aug 30 01:49 atop_20180829
-rw-r--r-- 1 root root 262277153 Aug 31 00:00 atop_20180830
-rw-r--r-- 1 root root 321592670 Sep  1 00:00 atop_20180831
-rw-r--r-- 1 root root 330041977 Sep  2 00:00 atop_20180901
-rw-r--r-- 1 root root 269040388 Sep  3 00:00 atop_20180902
-rw-r--r-- 1 root root 274807097 Sep  4 00:00 atop_20180903
-rw-r--r-- 1 root root  85426960 Sep  4 06:03 atop_20180904
-rw-r--r-- 1 root root         0 Sep  4 06:03 daily.log

如何将 atop 日志限制为仅 5 个日志(最后 5 天)

答案1

在 RH/CentOS 中atop不受logrotate.

其中/usr/share/atop/atop.daily有一个处理atop日志文件轮换的示例脚本。

该脚本作为查找行删除超过 28 天的日志,如下所示:

# delete logfiles older than four weeks
# start a child shell that activates another child shell in
# the background to avoid a zombie
#
( (sleep 3; find $LOGPATH -name 'atop_*' -mtime +28 -exec rm {} \;)& )

您可以将该脚本复制到/etc/cron.daily并将天数更改为 5。

( (sleep 3; find $LOGPATH -name 'atop_*' -mtime +5 -exec rm {} \;)& )

处理日常文件也可能有点不方便。使用上面的脚本,如果您不打算进行纯粹的每日轮换,您还可以编辑/etc/sysconfig/atop和更改持续时间,例如 10 分钟,如下所示:

INTERVAL=600

作为备选,如果您确实想每天轮换它,您可以创建一个logrotate文件,如下/etc/logrotate.d/atop所示:

/var/log/atop/atop_20[0-9][0-9][0-9][0-9][0-9][0-9] {
    missingok
    daily
    nodateext
    rotate 5
    ifempty
    nocreate
    postrotate
      /usr/bin/find /var/log/atop/ -maxdepth 1 -mount -name atop_20\[0-9\]\[0-9\]\[0-9\]\[0-9\]\[0-9\]\[0-9\]\* -mtime +40 -exec /bin/rm {} \;
    endscript
    }

如果做logrotate版本,需要保留日常文件,并且不要更改INTERVAL参数。

答案2

在阅读并花费了一些时间后找到了这个解决方案。

#!/bin/sh

LOGOPTS=""                              # default options
LOGINTERVAL=600                         # default interval in seconds
LOGGENERATIONS=28                       # default number of days
LOGPATH=/var/log/atop                   # default log location

# allow administrator to overrule the variables
# defined above
#
DEFAULTSFILE=/etc/sysconfig/atop                # possibility to overrule vars

if [ -e "$DEFAULTSFILE" ]
then
        . "$DEFAULTSFILE"

        # validate overruled variables
        # (LOGOPTS and LOGINTERVAL are implicitly by atop)
        #
        case "$LOGGENERATIONS" in
            ''|*[!0-9]*)
                echo non-numerical value for LOGGENERATIONS >&2
                exit 1;;
        esac
fi

CURDAY=`date +%Y%m%d`
BINPATH=/usr/bin
PIDFILE=/var/run/atop.pid

# verify if atop still runs for daily logging
#
if [ -e "$PIDFILE" ] && ps -p `cat "$PIDFILE"` | grep 'atop$' > /dev/null
then
        kill -USR2 `cat "$PIDFILE"`       # final sample and terminate

        CNT=0

        while ps -p `cat "$PIDFILE"` > /dev/null
        do
                CNT=$((CNT + 1))

                if [ $CNT -gt 5 ]
                then
                        break;
                fi

                sleep 1
        done

        rm "$PIDFILE"
fi

# delete logfiles older than N days (configurable)
# start a child shell that activates another child shell in
# the background to avoid a zombie
#
( (sleep 3; find "$LOGPATH" -name 'atop_*' -mtime +"$LOGGENERATIONS" -exec rm {} \;)& )

# activate atop with an interval of S seconds (configurable),
# replacing the current shell
#
exec gzip $LOGPATH/atop_$[CURDAY -1]
echo $$ > $PIDFILE
exec systemctl restart atop > "$LOGPATH/daily.log" 2>&1

将其添加到 /etc/crond.daily/atop.daily

希望这有帮助

相关内容