对 Ops Agent 自身日志使用日志轮替

Ops Agent 会写入名为 logging-module.log 的日志文件。如果代理长时间无人值守运行或出现问题,则此“自身日志”文件可能会消耗所有可用的磁盘空间。本文档介绍如何使用日志轮替来防止此问题。

Ops Agent 2.31.0 版引入了内置于代理中的可配置日志轮替功能。如果您运行的是 Ops Agent 2.31.0 或更高版本,则可以使用内置的日志轮替功能;请参阅在 Ops Agent 中配置日志轮替

您也可以手动管理日志轮替。如果您使用的是未内置日志轮替的 Ops Agent 版本,或者希望手动轮替日志,则可能需要手动执行流程。如需了解一种可能的方法,请参阅在 Linux 虚拟机上设置自日志文件轮替

在 Ops Agent 中配置日志轮替

本部分介绍如何修改 Ops Agent 用于自动轮替其日志的默认日志轮替配置。使用此功能需要 Ops Agent 2.31.0 或更高版本。

默认配置

Ops Agent 使用 default_self_log_file_rotation 条目来配置日志轮替。此配置条目采用三个选项;以下代码段显示了选项及其默认值:

  default_self_log_file_rotation:
    enabled: true
    max_file_size_megabytes: 400
    backup_count: 1

default_self_log_file_rotation 配置提供三个选项:

  • enabled:是否启用了日志轮替;默认值为 true
  • max_file_size_megabytes:日志文件通过日志轮替备份之前可达到的大小上限。以兆字节(10242 字节)为单位。默认值为 400,最小的有效值为 1。
  • backup_count:要保留的旧日志文件的数量。默认值为 1,最小的有效值为 1。

日志轮换的用户配置

如需修改默认日志轮替配置,您可以通过重新定义 Ops Agent 用户配置文件中的配置来替换该配置:

  • 在 Linux 上:/etc/google-cloud-ops-agent/config.yaml
  • 在 Windows 上:C:\Program Files\Google\Cloud Operations\Ops Agent\config\config.yaml

如需在 Ops Agent 中配置日志轮替,请在用户配置文件中添加 global 部分,并在 global 部分添加配置元素 default_self_log_file_rotation。此配置文件中可能已经有日志记录或指标流水线;在流水线后添加 global 部分。指定所有选项和默认值的结果如下所示:

logging:  ...
metrics:  ...
global:
  default_self_log_file_rotation:
    enabled: true
    max_file_size_megabytes: 400
    backup_count: 1

配置示例

如需停用 Ops Agent 的日志轮替,请指定值为 falseenabled 选项:

logging:  ...
metrics:  ...
global:
  default_self_log_file_rotation:
    enabled: false

如需在日志文件达到 20 MB 时轮替日志并保留 5 个备份(总共 6 个文件),请运行以下命令:

logging:  ...
metrics:  ...
global:
  default_self_log_file_rotation:
    max_file_size_megabytes: 20
    backup_count: 5

如需在日志文件达到 2000 MB (2 GB) 时轮替日志,并保留 1 个备份(总共 2 个文件),请运行以下命令:

logging:  ...
metrics:  ...
global:
  default_self_log_file_rotation:
    max_file_size_megabytes: 2000

如需在日志文件达到 400 MB 时轮替日志并保留 2 个备份(总共 3 个文件):

logging:  ...
metrics:  ...
global:
  default_self_log_file_rotation:
    backup_count: 2

如果您在优化日志轮替配置时频繁进行更改,请务必重启代理以应用更改。

在 Linux 虚拟机上设置自身日志文件轮替

如需将日志记录分代理日志的大小限制为 /var/log/google-cloud-ops-agent/subagents/logging-module.log,请安装并配置 logrotate 实用程序。

  1. 通过运行以下命令安装 logrotate 实用程序:

    在 Debian 和 Ubuntu 上

    sudo apt install logrotate
    

    在 CentOS、RHEL 和 Fedora 上

    sudo yum install logrotate
    
  2. /etc/logrotate.d/google-cloud-ops-agent.conf 处创建 logrotate 配置文件。

    sudo tee /etc/logrotate.d/google-cloud-ops-agent.conf > /dev/null << EOF
    # logrotate config to rotate Google Cloud Ops Agent self log file.
    # See https://manpages.debian.org/jessie/logrotate/logrotate.8.en.html for
    # the full options.
    /var/log/google-cloud-ops-agent/subagents/logging-module.log
    {
        # Log files are rotated every day.
        daily
        # Log files are rotated this many times before being removed. This
        # effectively limits the disk space used by the Ops Agent self log files.
        rotate 30
        # Log files are rotated when they grow bigger than maxsize even before the
        # additionally specified time interval
        maxsize 256M
        # Skip rotation if the log file is missing.
        missingok
        # Do not rotate the log if it is empty.
        notifempty
        # Old versions of log files are compressed with gzip by default.
        compress
        # Postpone compression of the previous log file to the next rotation
        # cycle.
        delaycompress
    }
    EOF
    
  3. 设置 crontabsystemd timer 以定期触发 logrotate 实用程序。

日志轮替生效后,您会在 /var/log/google-cloud-ops-agent/subagents/ 目录中看到轮替的文件。结果类似于以下输出内容:

/var/log/google-cloud-ops-agent/subagents$ ls -lh
total 24K
-rw-r--r-- 1 root root  717 Sep  3 19:54 logging-module.log
-rw-r--r-- 1 root root 6.8K Sep  3 19:51 logging-module.log.1
-rw-r--r-- 1 root root  874 Sep  3 19:50 logging-module.log.2.gz
-rw-r--r-- 1 root root  873 Sep  3 19:50 logging-module.log.3.gz
-rw-r--r-- 1 root root 3.2K Sep  3 19:34 logging-module.log.4.gz

如需测试日志轮替,请执行以下操作:

  1. 通过将 /etc/logrotate.d/google-cloud-ops-agent.conf 文件中的 maxsize 值设置为 1k,暂时减小触发轮替的文件大小。

  2. 通过重启代理几次来触发代理自我日志文件,使其大于 1K:

    sudo service google-cloud-ops-agent restart
    
  3. 等待 crontabsystemd timer 生效以触发 logrotate 实用程序,或运行以下命令手动触发 logrotate 实用程序:

    sudo logrotate /etc/logrotate.d/google-cloud-ops-agent.conf
    
  4. 验证您是否在 /var/log/google-cloud-ops-agent/subagents/ 目录中看到轮替的日志文件。

  5. 通过恢复原始 maxsize 值来重置日志轮替配置。