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