Logrotate 未按预期工作

Logrotate 未按预期工作

这是我的日志旋转配置:

/var/log/glusterfs/*.log /var/log/glusterfs/bricks/*.log /var/log/glusterfs/bricks/*.log.* {
  sharedscripts
  daily
  rotate 3
  copytruncate
  size 100M
  missingok
  compress
  delaycompress
  ifempty
  postrotate
  /usr/bin/killall -HUP glusterfs > /dev/null 2>&1 || true
  /usr/bin/killall -HUP glusterd > /dev/null 2>&1 || true
  endscript
}

这是目录:

username@server:/var/log/glusterfs/bricks$ ll
total 405980
-rw------- 1 root root         0 Dec 23 00:05 be-data.log
-rw------- 1 root root         1 Dec 29 09:38 be.log.1
-rw------- 1 root root         0 Dec 25 11:24 nl.log
-rw------- 1 root root         0 Dec 29 09:49 nl.log.1.1
-rw------- 1 root root         0 Dec 29 09:50 nl.log.1.1.1
-rw------- 1 root root         0 Dec 29 09:55 nl.log.1.1.1.1
-rw------- 1 root root         0 Dec 29 09:55 nl.log.1.1.1.1.1
-rw------- 1 root root         0 Dec 29 09:55 nl.log.1.1.1.1.1.1
-rw------- 1 root root         0 Dec 29 09:55 nl.log.1.1.1.1.1.1.1
-rw------- 1 root root         0 Dec 29 09:55 nl.log.1.1.1.1.1.1.1.1
-rw------- 1 root root         0 Dec 29 10:08 nl.log.1.1.1.1.1.1.1.1.1
-rw------- 1 root root         0 Dec 29 10:08 nl.log.1.1.1.1.1.1.1.1.1.1
-rw------- 1 root root         0 Dec 29 10:08 nl.log.1.1.1.1.1.1.1.1.1.1.1
-rw------- 1 root root 368402432 Dec 29 10:08 nl.log.1.1.1.1.1.1.1.1.1.1.1.1
-rw------- 1 root root    610304 Dec 23 00:05 bo.log.1
-rw------- 1 root root    860160 Dec 23 00:05 bricks.log.1
-rw------- 1 root root    589824 Dec 23 00:05 other.log.1

我能看到什么:

  • 日志未压缩
  • 日志大于 100MB
  • 计数不起作用
  • 存在空日志文件,我不希望轮换空日志文件

我的 logrotate 配置应该如何解决上述所有问题?

答案1

在配置文件中,您为目录中的日志文件指定了两种模式/var/log/glusterfs/bricks

  1. *.log
  2. *.log.*

这些模式中的第二个将匹配任何轮换日志文件。这就是为什么您会得到带有看似无穷无尽的.1后缀的文件。

日志文件未压缩,因为您已delaycompress在配置中进行了压缩。它们将在下一次旋转时被压缩。请注意,第一个问题(由于上面的第二个模式而导致的已旋转日志的旋转)实际上禁用了任何压缩,因为所有旋转都是“第一次”旋转。

如果日志文件大于 100M,则根据您的配置进行轮换。有一个文件比这个大。由于上面第二个日志文件模式存在上述问题,它在每次调用时都会轮换并且从未压缩。

有空日志文件。这只是上面错误的日志文件匹配模式的另一个影响。当日志文件被轮换时,它会被复制到name-of-file.log.1原始文件,name-of-file.log并被截断(“清空”)。同样,由于*.log.*配置中的模式,文件将在下一次旋转时name-of-file.log.1被复制,并且原始文件将被截断。name-of-file.log.1.1name-of-file.log.1

这一切都正常,但由于日志文件模式不仅获取实际的日志文件,还获取旋转的日志文件,因此您最终会陷入混乱。

答案2

本周查看 logrotate 的联机帮助页是免费的!

   delaycompress
          Postpone compression of the previous log file to the next  rota‐
          tion  cycle.  This only has effect when used in combination with
          compress.  It can be used when some program cannot  be  told  to
          close  its logfile and thus might continue writing to the previ‐
          ous log file for some time.

   size size
          Log  files are rotated only if they grow bigger then size bytes.
          If size is followed by k, the size is assumed  to  be  in  kilo‐
          bytes.   If the M is used, the size is in megabytes, and if G is
          used, the size is in gigabytes. So size  100,  size  100k,  size
          100M and size 100G are all valid.

   ifempty
          Rotate  the  log  file  even  if  it  is  empty,  overriding the
          notifempty option (ifempty is the default).

相关内容