dobbs
dobbs

Reputation: 1043

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?

Upvotes: 3

Views: 6030

Answers (2)

dobbs
dobbs

Reputation: 1043

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
    ...
    }

Upvotes: 6

Uri Simchoni
Uri Simchoni

Reputation: 326

Seems like if the pre-rotate script simply fails (returns a non-zero value, for example if you run /bin/false as a script or "exit 1" from a bash script), then the file itself doesn't get rotated. However previous files (foo.log.1, ...) do get rotated. I suppose that can be qualified as a bug of logrotate.

So if you also use dateext (which prevents the foo.log.1 => foo.log.2 rotation) you should be all set.

Upvotes: 2

Related Questions