After the site upgrade to Drupal 6.2, I discovered that sometime in April, the format of the names of my Apache access logs had been changed. They used to look like this:
/var/log/httpd/access_log
/var/log/httpd/access_log.1
/var/log/httpd/access_log.2
/var/log/httpd/access_log.3
/var/log/httpd/access_log.4
But now they looked like this:
/var/log/httpd/access_log
/var/log/httpd/access_log-20080427
/var/log/httpd/access_log-20080504
/var/log/httpd/access_log-20080511
/var/log/httpd/access_log-20080518
Well ... obviously that's a cool format (after all, you know what periods each log covers), but it's not the format I'd built some tools around ... ahhh, the life of the lazy system administrator (yum update ... yeah, that looks good ...). The main tools were the google sitemap generator, and the website scrubber I last talked about awhile ago.
Being quite out of practice with shell scripting/sed, and too lazy to get back into practice, I wrote another little perl program to run as a cron job every night. The idea was to do it quick, and not worry about all of the things that could go wrong ... like another update to logrotate/apache (not sure which one changed the access_log names). Oh well, there's nothing like living on the edge!
While I'm quite aware of how amateurish, simple, and dirty these examples are, I do hope someone finds it useful. If you visit and find something you like, please leave a comment!
#!/usr/bin/perl
#
# scrubsitemap.pl
#
# Copyright 2007-2008 Joshua Radke (josh at radkeland dot org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (version 2) as
# published by the Free Software Foundation.
#
# This is a very simple script (might have been 'easier' in a shell
# script, but I'm more comfy with perl) to 'fix' the dated style of
# Apache access_log to look like the older style of access_log.1, etc.
#
# I'll use variables to it'll be easy to rewrite in the future. Note that
# at least on the current system, it will need to be run as root. It will
# simply die if any of its operations fail ... with little grace at all.
use strict;
use threads;
use IO::All;
my $logpath = "/var/log/httpd/";
my $fnbase = "access_log";
my @oldnames;
my @newnames;
my @dirlist = `ls -l $logpath$fnbase*`;
foreach (@dirlist) {
chomp;
$_ =~ /$logpath$fnbase(.*)/;
my $thissuffix = $1;
# Let's file them away.
if ($thissuffix =~ /(\.\d+)/) {
push (@oldnames, $1);
} elsif ($thissuffix =~ /(\-\d+)/) {
push (@newnames, $1);
}
}
# Okies, now we unlink the oldnames, and make links to the new ones.
foreach (@oldnames) {
unlink "$logpath$fnbase$_" or die "Couldn't unlink $logpath$fnbase$_";
}
# Sort the @newnames list.
@newnames = sort @newnames;
# And make new links
my $i;
for ($i = 0; $i <= $#newnames; $i++) {
symlink("$logpath$fnbase$newnames[$i]",
"$logpath$fnbase.". ($i+1)) or
die "Couldn't symlink $logpath$fnbase$newnames[$i],$logpath$fnbase."
. ($i+1);
}
exit(0);