带有预旋转脚本的条件 logrotate

dob*_*bbs 3 bash logrotate

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

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

有人能指出正确的方向来弄清楚如何使logrotate prerotate脚本成为exit其父logrotate进程吗?

dob*_*bbs 6

在手册页中找到了答案(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.
Run Code Online (Sandbox Code Playgroud)

我创建了一个firstactionbash 脚本,如下所示来检查节点上是否存在负载平衡 IP:

#!/bin/bash

TESTCASE="$(ifconfig | grep '\([^0-9]\|^\)1\.2\.3\.4\([^0-9]\|$\)')"

if [ -z "$TESTCASE" ]; then
        /bin/false
fi
Run Code Online (Sandbox Code Playgroud)

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

logrotate.d 示例:

/var/log/blah/*/*.log {
    firstaction
    /usr/local/bin/amiactive.sh > /dev/null 2>&1
    endscript
    ...
    }
Run Code Online (Sandbox Code Playgroud)