【问题标题】:conditional logrotate with prerotate script使用 prerotate 脚本进行条件 logrotate
【发布时间】:2018-01-05 00:59:50
【问题描述】:

我在一个集群中有两台 syslog-ng 服务器(热/冷),它们都映射同一个 NFS 共享。我想在 syslog 服务器上运行 logrotate 以轮换存储在 NFS 共享上的日志。问题是当前如果两个节点都具有/etc/logrotate.d/syslog-ng 配置,则会导致双重旋转。

我认为必须有一种方法可以在logrotate.d 中使用prerotate 节来确定是否应该在服务器上进行轮换。换句话说,如果被动节点尝试运行logrotateprerotate 脚本将首先检查该节点是否为主节点。如果它不是主要的,我希望 prerotate 脚本在运行之前将 exit logrotate 进程。

有人能指出我正确的方向来弄清楚如何制作一个logrotate prerotate 脚本exit 它的父logrotate 进程吗?

【问题讨论】:

    标签: bash logrotate


    【解决方案1】:

    在手册页中找到了答案(RTFM,哎呀!)

       firstaction/endscript
              The  lines between firstaction and endscript (both of which must
              appear on lines by themselves) are executed (using /bin/sh) once
              before  all  log  files  that  match  the wildcarded pattern are
              rotated, before prerotate script is run and only if at least one
              log  will actually be rotated.  These directives may only appear
              inside a log file definition. Whole pattern  is  passed  to  the
              script  as  first  argument.  If the script exits with error, no
              further processing is done. See also lastaction.
    

    我创建了一个firstaction bash 脚本如下来检查节点上是否存在负载均衡IP:

    #!/bin/bash
    
    TESTCASE="$(ifconfig | grep '\([^0-9]\|^\)1\.2\.3\.4\([^0-9]\|$\)')"
    
    if [ -z "$TESTCASE" ]; then
            /bin/false
    fi
    

    如果返回false,logrotate 不会继续。

    logrotate.d 示例:

    /var/log/blah/*/*.log {
        firstaction
        /usr/local/bin/amiactive.sh > /dev/null 2>&1
        endscript
        ...
        }
    

    【讨论】:

      【解决方案2】:

      似乎如果预旋转脚本只是失败了(返回一个非零值,例如,如果您将 /bin/false 作为脚本运行或从 bash 脚本“退出 1”),那么文件本身不会t 得到旋转。但是以前的文件(foo.log.1,...)确实会旋转。我想这可以称为 logrotate 的错误。

      因此,如果您还使用 dateext(它可以防止 foo.log.1 => foo.log.2 旋转),您应该已经准备就绪。

      【讨论】:

      • 我会试一试。感谢您的建议
      猜你喜欢
      • 2017-04-26
      • 2019-09-13
      • 1970-01-01
      • 2012-10-19
      • 2014-11-16
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多