Buildroot:syslog-ng 登录到“/var/log/messages.1”文件而不是“/var/log/messages”

Buildroot:syslog-ng 登录到“/var/log/messages.1”文件而不是“/var/log/messages”

我正在使用 Buildroot 构建嵌入式 Linux 板(用户手册在这里)。

我已经syslog-ng在板上运行了。它的配置文件在 buildroot 中指定:https://github.com/buildroot/buildroot/blob/master/package/syslog-ng/syslog-ng.conf

@version: 3.37

source s_sys {
    file("/proc/kmsg" program_override("kernel"));
    unix-stream ("/dev/log");
    internal();
};

destination d_all {
    file("/var/log/messages");
};

log {
    source(s_sys);
    destination(d_all);
};

请注意,它将目标指定为"/var/log/messages",但板上的活动日志记录将进入名为 的文件/var/log/messages.1,而该/var/log/messages文件甚至不存在。这是为什么?有没有办法登录到/var/log/messages文件中?

我们过去使用的 Syslog 会登录到/var/log/messages,我们正在努力保持这种行为的一致性。

补充笔记

  1. ls -1 /var/log在运行的板上syslog包含以下messages文件:
    messages
    messages.1
    messages.2
    messages.2.gz
    messages.3
    messages.4
    messages.5
    messages.6
    messages.7
    
  2. ls -1 /var/log在运行的板上syslog-ng包含这些messages文件(messages缺少通知):
    messages.1
    messages.2
    messages.3
    messages.4
    messages.5
    messages.6
    messages.7
    
  3. syslog-ng板上,tail -f /var/log/messages.1显示它不断接收记录的消息,这是意外的,因为当使用syslog“活动”文件时/var/log/messages

答案1

解决了!您必须syslog-ng在每次日志轮转后强制重新打开其目标日志文件

所以,我想通了。感谢@Murray Jensen关于它的提示在这里

每当logrotate旋转我的/var/log/messages文件时,它都会将其重命名为/var/log/messages.1.然而,syslog-ng正在写入它打开的原始文件描述符(fd)所指向的文件。将文件从/var/log/messages重命名为 执行以下/var/log/messages.1操作不是更改文件描述符,因此 syslog-ng 现在写入的文件描述符指向现在名为 的文件/var/log/messages.1修复方法是简单地强制syslog-ng重新打开其日志文件并在每次日志轮转后获取新的文件描述符,从而使其为新创建的目标日志文件获取新的文件描述符,该目标日志文件现在存在于/var/log/messages

有 3 种方法可以做到这一点,我在这里写过:https://github.com/syslog-ng/syslog-ng/issues/1774#issuecomment-1270517815

请参阅此处,了解我在哪里了解到的syslog-ng-ctl reopen,这是推荐的方式:https://github.com/syslog-ng/syslog-ng/issues/1774#issuecomment-346624252

这 3 种方法是:

# Option 0 (no longer recommended): call the heavier `reload` command after log
# rotation
syslog-ng-ctl reload

# Option 1 (RECOMMENDED): call the new `reopen` command after log rotation
syslog-ng-ctl reopen

# Option 2 (same thing as Option 1 above): send the `SIGUSR1` kill signal to the
# running `syslog-ng` process
pid="$(cat /var/run/syslog-ng.pid)" kill -SIGUSR1 $pid

因此,要logrotate在每次日志轮转后自动强制调用上述 3 种方法之一,您必须将正确的命令作为脚本添加到您的(或类似的 - 可以命名为任何名称)配置文件postrotate中。固定的 logrotate 配置文件现在可能如下所示:/etc/logrotate.d/syslog-nglogrotate

从我的笔记来看:

样本/etc/logrotate.d/syslog-ng 对数旋转配置文件:

/var/log/auth.log 
/var/log/user.log
/var/log/messages  
{
    rotate 7
    size 20M
    delaycompress
    missingok
    # Required for syslog-ng after each rotation, to cause it to reopen log
    # files so it can begin logging to the new log file under a new file
    # descriptor, rather than to the old log file which has now been rotated
    # and renamed. 
    postrotate
        # After rotating the log files, cause syslog-ng to reopen the
        # destination log files so it will log into the newly-created log files
        # rather than into the now-rotated and renamed ones.
        #
        # This ensures, for example, that syslog-ng will move its file
        # descriptor to begin logging into the main "/var/log/messages" log
        # file again, instead of into the now-rotated "/var/log/messages.1"
        # file, which the old file descriptor (fd) is now pointing to since
        # that fd's filename was just renamed from "/var/log/messages"
        # to "/var/log/messages.1" during the log rotation.

        # Option 1:
        syslog-ng-ctl reopen
        # OR, Option 2
        # pid="$(cat /var/run/syslog-ng.pid)" kill -SIGUSR1 $pid
    endscript
}

注意:我还在这里提出了文档更改请求:https://github.com/syslog-ng/syslog-ng/issues/4166。现在建议syslog-ng-ctl reopen在每次日志轮换后使用,而不是syslog-ng-ctl reload.

旧的答案尝试(我首先尝试的)

在板上运行时syslog-ng,我完全重新刷新 rootfs(根文件系统)映像, 和重新启动了主板,现在我/var/log/messages再次看到该文件:

的部分输出ls -1 /var/log

messages
messages.1
messages.2
messages.3
messages.4
messages.5
messages.6
messages.7

我无法解释。tail -f /var/log/messages确实显示了进入该文件的活动日志,如预期的那样,并tail -f /var/log/messages.1显示该文件是静态的,没有新消息进入,也如预期的那样。

syslog-ng我可以通过查看以下输出来证明该板确实正在运行ps aux | grep syslog

# ps aux | grep syslog
  803 root      0:00 {syslog-ng} supervising syslog-ng
  804 root      0:02 /usr/sbin/syslog-ng
12571 root      0:00 grep syslog

...与在板上运行时相同命令的输出相反syslog

# ps aux | grep syslog
  789 root      0:19 /sbin/syslogd -n -n -s 0
 2993 root      0:00 grep syslog

再说一遍,我不确定发生了什么,也不知道为什么。

两个板上都ps aux | grep logrotate显示正在logrotate运行。前任:

# ps aux | grep logrotate
 1299 root      0:00 runsv logrotate-periodically
14208 root      0:00 grep logrotate

两个板都有相同的/etc/logrotate.conf文件,并且只有syslog-ng板有该/etc/syslog-ng.conf文件,其中包含问题中所示的内容。

如果我在未来几天发现任何新情况,我会回来更新这个答案。

相关内容