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