bashlogrotate

conditional logrotate with prerotate script


I have two syslog-ng servers in a cluster (hot/cold), both mapping the same NFS Share. I would like to run logrotate on the syslog servers to rotate the logs stored on the NFS Share. The problem is that currerntly if both nodes have a /etc/logrotate.d/syslog-ng configuration, it would cause double rotation.

I am thinking there must be a way to use a prerotate stanza in logrotate.d to determine whether or not the rotate should happen on a server. In other words, If the passive node tries to run logrotate, the prerotate script would check first the node is primary. If it is not primary, I want the prerotate script to exit the logrotate process before it runs.

Can someone point me in the right direction to figure out how to make a logrotate prerotate script exit its parent logrotate process?


Solution

  • Found the answer in the man pages (RTFM, oops!)

       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.
    

    I created a firstaction bash script as follows to check if a loadbalanced IP exists on the node:

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

    If it returns false, logrotate does not continue.

    logrotate.d sample:

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