Blame support/split-logfile.in

Packit 90a5c9
#!@perlbin@
Packit 90a5c9
#
Packit 90a5c9
# Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
# contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
# this work for additional information regarding copyright ownership.
Packit 90a5c9
# The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
# (the "License"); you may not use this file except in compliance with
Packit 90a5c9
# the License.  You may obtain a copy of the License at
Packit 90a5c9
#
Packit 90a5c9
#     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
#
Packit 90a5c9
# Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
# distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
# See the License for the specific language governing permissions and
Packit 90a5c9
# limitations under the License.
Packit 90a5c9
#
Packit 90a5c9
#
Packit 90a5c9
# This script will take a combined Web server access
Packit 90a5c9
# log file and break its contents into separate files.
Packit 90a5c9
# It assumes that the first field of each line is the
Packit 90a5c9
# virtual host identity (put there by "%v"), and that
Packit 90a5c9
# the logfiles should be named that+".log" in the current
Packit 90a5c9
# directory.
Packit 90a5c9
#
Packit 90a5c9
# The combined log file is read from stdin. Records read
Packit 90a5c9
# will be appended to any existing log files.
Packit 90a5c9
#
Packit 90a5c9
use strict;
Packit 90a5c9
use warnings;
Packit 90a5c9
Packit 90a5c9
my %log_file = ();
Packit 90a5c9
Packit 90a5c9
while (my $log_line = <STDIN>) {
Packit 90a5c9
    #
Packit 90a5c9
    # Get the first token from the log record; it's the
Packit 90a5c9
    # identity of the virtual host to which the record
Packit 90a5c9
    # applies.
Packit 90a5c9
    #
Packit 90a5c9
    my ($vhost) = split (/\s/, $log_line);
Packit 90a5c9
    #
Packit 90a5c9
    # Normalize the virtual host name to all lowercase.
Packit 90a5c9
    # If it's blank, the request was handled by the default
Packit 90a5c9
    # server, so supply a default name.  This shouldn't
Packit 90a5c9
    # happen, but caution rocks.
Packit 90a5c9
    #
Packit 90a5c9
    $vhost = lc ($vhost) || "access";
Packit 90a5c9
    #
Packit 90a5c9
    # if the vhost contains a "/" or "\", it is illegal so just use 
Packit 90a5c9
    # the default log to avoid any security issues due if it is interprted
Packit 90a5c9
    # as a directory separator.
Packit 90a5c9
    if ($vhost =~ m#[/\\]#) { $vhost = "access" }
Packit 90a5c9
    #
Packit 90a5c9
    # If the log file for this virtual host isn't opened
Packit 90a5c9
    # yet, do it now.
Packit 90a5c9
    #
Packit 90a5c9
    if (! $log_file{$vhost}) {
Packit 90a5c9
        open $log_file{$vhost}, ">>${vhost}.log"
Packit 90a5c9
            or die ("Can't open ${vhost}.log");
Packit 90a5c9
    }
Packit 90a5c9
    #
Packit 90a5c9
    # Strip off the first token (which may be null in the
Packit 90a5c9
    # case of the default server), and write the edited
Packit 90a5c9
    # record to the current log file.
Packit 90a5c9
    #
Packit 90a5c9
    $log_line =~ s/^\S*\s+//;
Packit 90a5c9
    print {$log_file{$vhost}} $log_line;
Packit 90a5c9
}
Packit 90a5c9
exit 0;