#!/usr/bin/perl -w # Name: mybin/apache/mv-web-logs Author: js-cgi@inwap.com 05-Mar-2002 # Purpose: Archives access_log.?.gz and error_log.?.gz to yearly subdirectories # Naming convention: yyyy/mmdd-{acc,err}.gz (year, month, day, 3-letter name). # Also runs logresolve to do DNS lookups on client IP addresses in access_log. # Last name on command-line is the destination directory. I use $HOME/weblogs. use strict; my $USAGE = "Usage: $0 [-v] access_log.?.gz error_log.?.gz \$HOME/weblogs\n"; die $USAGE unless @ARGV; my $verbose = ($ARGV[0] eq "-v") ? shift : ""; my $DIR = pop @ARGV; # Last item is destination directory die $USAGE unless @ARGV && -d $DIR; # Look for apache/bin/logresolve in the usual places $ENV{PATH}.=":/usr/apache/bin:/usr/local/apache/bin:/usr/sbin"; foreach my $log (@ARGV) { my $mtime = (stat $log)[9]; # File's modification time next if (not defined $mtime and $log =~ /\?|\*/); # Skip unmatched wildcards (warn("stat($log): $!\n"),next) unless $mtime; $_ = $log; s%.*/%%; # Remove directory component my $fil = substr $_,0,3; # Either "acc" or "err" my ($day,$mon,$year) = (gmtime $mtime)[3..5]; my $subdir = sprintf "%s/%4d", $DIR, $year+1900; my $file = sprintf "%s/%02d%02d-%s", $subdir, $mon+1, $day, $fil; my $file_gz = "$file.gz"; if (not -d $subdir) { # Start a new subdirectory on January 1st mkdir $subdir,0777 or warn "mkdir($subdir): $!\n"; # 0777 OK due to umask system "mkdir", "-p", $subdir unless -d $subdir; # Punt if this fails } if ($fil eq "acc") { # Run `logresolve` on access_log only # 'zcat' may or may not understand *.gz; use 'gunzip -c' to be safe $_ = "gunzip -c $log | logresolve >$file && rm $log; gzip -f -9 $file"; } else { $_ = "mv $log $file_gz"; } print $_,"\n" if $verbose; system $_ and warn "'$_' returned $?\n"; utime $^T,$mtime,$file_gz or warn "utime($file_gz): $!\n"; # Preserve date }