Ok. What you'll need to do is this:
At the beginning of the script, you'll need to add in a section that loads the passwd file, more specifically, the user's name and home directory.
Might look something like this:
Code:
my($hostname, $pathname);
my($userlist) = {};
while(($hostname,$pathname) = (getpwent())[6,7]) {
# skip anything that doesn't have it's home in /home
#
next if($pathname !~ /^\/home/);
$userlist->{$hostname} = $pathname;
}
# don't forget to include the default logfile!
#
$userlist->{access} = '/var/log/access'; # or whatever yours is
For some reason, what
getpwent() returns on my system seems to be different than what perl's book says it should be, but for me, position 6 was the comment field (the host name), and position 7 was the home dir. You might want to verify this on your system.
This code will make the path checking much easier.
Now, right after
Code:
# If the log file for this virtual host isn't opened
# yet, do it now.
#
but before
Code:
if (! $is_open{$vhost}) {
We're going to add in some path building, as long as the host exists in the userlist hash. Here is my take on it:
Code:
# first check to see if the host exists in our userlist
#
if(defined($userlist->{$vhost})) {
# build the path to the file
#
my($logpath) = $userlist->{$vhost} . '/';
# make sure path exists
#
if(-d "$logpath") {
# now attach the filename
#
$logpath .= $vhost . ".log";
# we have permission?
#
if(-w "$logpath") {
# been opened already?
#
if (!$is_open{$vhost}) {
open $vhost, ">>$logpath" or die ("Can't open $logpath\n");
$is_open{$vhost} = 1;
}
#
# Strip off the first token (which may be null in the
# case of the default server), and write the edited
# record to the current log file.
#
$log_line =~ s/^\S*\s+//;
printf $vhost "%s", $log_line;
}
}
}
This should take care of it.. now, I have
not tested this code, because my setup is different han yours..
--edit--
Sorry, I noticed a few bugs in the code