Adding Version Control Sanity to Solaris logadm

Solaris includes a log rolling utility called logadm, which is very useful for managing logs without having to write your own custom scripts or install some other utility across all of your servers.

However, there is one big drawback to logadm. Whenever it runs, it stores a timestamp for each log it rolls in the logadm configuration file itself. I'll leave it to the reader to question the sanity of this.

For example:

/var/adm/messages -C 4 -a 'kill -HUP `cat /var/run/syslog.pid`'

Is changed to:

/var/adm/messages -C 4 -P 'Sat Apr 09 21:27:21 2011' -a 'kill -HUP `cat /var/run/syslog.pid`'

My immediate dilemma is that I like to put everthing important into version control. But since the logadm configuration file is constantly modified, it isn't practical to store it in version control without it getting overwritten every time it is deployed to the system from version control.

So, I wrote this simple perl script that takes both files in as arguments and spits out a version of the file that can be safely deployed. There is nothing complex about this script, but I share it here for the benefit of others that run across this dilemma.

#!/usr/bin/perl

my ($configfile, $systemfile) = @ARGV;

# check input
unless ( $configfile and $systemfile and -f $configfile ) {
    print "usage: $0 configfile systemfile\n";
    exit;
}

# nothing to fix
if ( ! -e $systemfile ) {
    exit;
}

# open each file for reading
open my $systemfile_fh, '<', $systemfile or die "can't open $systemfile: $!";
open my $configfile_fh, '<', $configfile or die "can't open $configfile: $!";

# read in each line of the file that does not have the -P timestamp
while ( my $configfile_line = <$configfile_fh> ) {
    chomp $configfile_line;

    # then read in each line of the file that has the -P timestamp
    while ( my $systemfile_line = <$systemfile_fh> ) {
        chomp $systemfile_line;

        # if the line has the -P timestamp
        if ( $systemfile_line =~ /(.*)(\-P\s+'.*?'\s*)(.*)/ ) {

            # remove the -P timestamp and see if it is identical in both files
            my $systemfile_line_sans = $1.$3;
            my $insert = $2;
            if ( $configfile_line eq $systemfile_line_sans ) {

                # if the lines match then we will want to use the replace the
                # line with the version that has the -P timestamp
                $configfile_line = $systemfile_line;
            }
        }
    }

    # print out each line, whether we have swapped it, or not
    print $configfile_line."\n";
}
close $configfile_fh;
close $systemfile_fh;

Leave a comment

About Jesse Thompson

user-pic I blog about Perl.