Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

logrotate

by enaco (Novice)
on Nov 12, 2001 at 19:23 UTC ( [id://124840]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility scripts
Author/Contact Info Tommy Rohde aka. enaco
Description: This is a script for rotating logs.
It reads a configuration file that tells it what files to rotate, how long the rotate history is and if it exists, what program to send a HUP when rotatings its log.
This script was made primarly for learning perl but still a usefull pice of work.

The script is not quite done yet, i still got some bugs to fix, i release it early and hope for some input.

#!/usr/bin/perl -w
#
# Logrotate 0.1
#
# Uses an external configuration file with the following syntax:
#
# [logfile]             [versions]      [pidfile]
# /somedir/logfile      5               /var/pid/someapp.pid
#
#
use strict;
use Logfile::Rotate;

# Change between these lines to change the default config file.
#-------------------------------------------
my $config = '/etc/logrotate.conf';
#-------------------------------------------
our $confignr;
our @config;
our $appid;

if (defined $ARGV[0]) { $config = $ARGV[0] }
else {
        print "Using default configfile: $config\n";
}


if (! -e $config) { syntax () }
else {
        open(CONFIG, $config) or syntax ();
        @config = <CONFIG>;
        @config = grep !/^#/, @config;
        chomp @config;
        $confignr = @config;
}

for (my $nr = 0; $nr < $confignr; $nr++) {
        $_ = $config[$nr];
        /^(\b)\s+(\b)\s+(\b)/;
        my $file = $1;
        my $versions = $2;
        my $pidfile = $3;
        print $file;
        print $versions;
        print $pidfile;
        my $log = new Logfile::Rotate(
                File   => $file,
                Count  => $versions,
                Gzip  => 'lib',
                Post   => sub{
                open(IN, $pidfile);
                $appid = <IN>;
                chomp $appid;
                kill("HUP", $appid); },
                Dir    => '/var/log/old',
                Flock  => 'yes',
                Persist => 'yes',);
        if (defined $log) { print "Looks ok..\n";
        } else {
                die "An error accured processing config data\n";
        }
        $log->rotate();
        undef $log;
}

sub syntax {
print <<eof;

        Make sure the configfile exists and is
        readable by the script.

        Correct use is:
        $0 configfile

        if no argument is given the script
        will use the default:
        /etc/logrotate.conf

eof
exit;
}
Replies are listed 'Best First'.
Re: logrotate
by CharlesClarkson (Curate) on Nov 12, 2001 at 20:54 UTC

    You don't need $confignr. Changing the for a bit will load $_ with each array element. (Note: for and foreach are the same.)

    foreach (@config) { next unless /^(\b)\s+(\b)\s+(\b)/; my $file = $1; my $versions = $2; my $pidfile = $3;

    the next unless /^(\b)\s+(\b)\s+(\b)/; line adds some error checking.

    BTW, does that regex work?

    The subroutine could be indented to highlight it better

    Gzip => 'lib', Post => sub{ open(IN, $pidfile); $appid = <IN>; chomp $appid; kill("HUP", $appid); }, Dir => '/var/log/old', Flock => 'yes',
    might look like:
    File => $file, Count => $versions, Gzip => 'lib', Post => sub{ open(IN, $pidfile) or die "$pidfile: $!"; $appid = <IN>; chomp $appid; kill("HUP", $appid); }, Dir => '/var/log/old', Flock => 'yes',

    You really should check the open for success. I'm uncertain why $appid is defined with our. Can you explain? Try testing $appid before killing it.

    There should be no need to undef $log; as $log is lexically scoped to the foreach block.

    instead of:
    if (defined $log) { print "Looks ok..\n"; } else { die "An error accured processing config data\n"; } $log->rotate(); undef $log; }
    how about:
    defined $log or die "An error accured processing config data\n"; print "Looks ok..\n"; $log->rotate(); }



    HTH,
    Charles K. Clarkson



    The vast majority of the Earth is underground. - R. Zubrin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://124840]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found