| 1 |
#!@PERL@ -- # -*- perl -*- |
| 2 |
# ==================================================================== |
| 3 |
# Copyright (c) 1995-1999 The Apache Group. All rights reserved. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions |
| 7 |
# are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer in |
| 14 |
# the documentation and/or other materials provided with the |
| 15 |
# distribution. |
| 16 |
# |
| 17 |
# 3. All advertising materials mentioning features or use of this |
| 18 |
# software must display the following acknowledgment: |
| 19 |
# "This product includes software developed by the Apache Group |
| 20 |
# for use in the Apache HTTP server project (http://www.apache.org/)." |
| 21 |
# |
| 22 |
# 4. The names "Apache Server" and "Apache Group" must not be used to |
| 23 |
# endorse or promote products derived from this software without |
| 24 |
# prior written permission. For written permission, please contact |
| 25 |
# [email protected]. |
| 26 |
# |
| 27 |
# 5. Products derived from this software may not be called "Apache" |
| 28 |
# nor may "Apache" appear in their names without prior written |
| 29 |
# permission of the Apache Group. |
| 30 |
# |
| 31 |
# 6. Redistributions of any form whatsoever must retain the following |
| 32 |
# acknowledgment: |
| 33 |
# "This product includes software developed by the Apache Group |
| 34 |
# for use in the Apache HTTP server project (http://www.apache.org/)." |
| 35 |
# |
| 36 |
# THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY |
| 37 |
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 38 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 39 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR |
| 40 |
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 42 |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 43 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 45 |
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 46 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 47 |
# OF THE POSSIBILITY OF SUCH DAMAGE. |
| 48 |
# ==================================================================== |
| 49 |
# |
| 50 |
# This software consists of voluntary contributions made by many |
| 51 |
# individuals on behalf of the Apache Group and was originally based |
| 52 |
# on public domain software written at the National Center for |
| 53 |
# Supercomputing Applications, University of Illinois, Urbana-Champaign. |
| 54 |
# For more information on the Apache Group and the Apache HTTP server |
| 55 |
# project, please see <http://www.apache.org/>. |
| 56 |
# |
| 57 |
# |
| 58 |
# cronosplit -- split log files into cronolog-compatible logs |
| 59 |
# |
| 60 |
# Copyright (c) 1996-1999 by Ford & Mason Ltd |
| 61 |
# |
| 62 |
# This software was submitted by Ford & Mason Ltd to the Apache |
| 63 |
# Software Foundation in December 1999. Future revisions and |
| 64 |
# derivatives of this source code must acknowledge Ford & Mason Ltd |
| 65 |
# as the original contributor of this module. All other licensing |
| 66 |
# and usage conditions are those of the Apache Software Foundation. |
| 67 |
# |
| 68 |
# cronosplit is loosly based on the splitlog script by |
| 69 |
# Roy Fielding <[email protected]> |
| 70 |
# (splitlog is part of the wwwstat package, |
| 71 |
# see <http://www.ics.uci.edu/pub/websoft/wwwstat/>) |
| 72 |
|
| 73 |
|
| 74 |
use Getopt::Long; |
| 75 |
|
| 76 |
$program = 'cronosplit'; |
| 77 |
$version = '@VERSION@'; |
| 78 |
|
| 79 |
# Parameters |
| 80 |
|
| 81 |
$MaxHandles = 50; |
| 82 |
$DirMode = 0775; |
| 83 |
|
| 84 |
|
| 85 |
# Patterns for log file entries (Common Log Format) and timestamps |
| 86 |
|
| 87 |
$log_entry_pattern = "^(\\S+) (\\S+) ([^[]+) \\[([^]]*)] \"([^\"]*)\" (\\S+) (\\S+)(.*)"; |
| 88 |
$timestamp_pattern = "^([ 0-3]?\\d)/([A-Za-z]+)/(\\d{4}):(\\d\\d):(\\d\\d):(\\d\\d) [+ -]\\d{1,4}"; |
| 89 |
|
| 90 |
# An associative array of month names and abbreviations |
| 91 |
|
| 92 |
%month = (Jan => 1, January => 1, |
| 93 |
Feb => 2, February => 2, |
| 94 |
Mar => 3, March => 3, |
| 95 |
Apr => 4, April => 4, |
| 96 |
May => 5, |
| 97 |
Jun => 6, June => 6, |
| 98 |
Jul => 7, July => 7, |
| 99 |
Aug => 8, August => 8, |
| 100 |
Sep => 9, September => 9, Sept => 9, |
| 101 |
Oct => 10, October => 10, |
| 102 |
Nov => 11, November => 11, |
| 103 |
Dec => 12, December => 12); |
| 104 |
|
| 105 |
@month = ("", "January", "February", "March", "April", "May", "June", |
| 106 |
"July", "August", "September", "October", "November", "December"); |
| 107 |
@mmm = ("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 108 |
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); |
| 109 |
|
| 110 |
@days = ("Sunday", "Monday", "Tuesday", "Wednesday", |
| 111 |
"Thursday", "Friday", "Saturday"); |
| 112 |
@ddd = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); |
| 113 |
|
| 114 |
|
| 115 |
# Process options |
| 116 |
|
| 117 |
(GetOptions("template=s", \$template, |
| 118 |
"print-invalid", \$PrintInvalids, |
| 119 |
"debug", \$debug, |
| 120 |
"verbose", \$verbose, |
| 121 |
"help", \$print_help, |
| 122 |
"version", \$print_version) |
| 123 |
and ($print_help or $print_version or $template)) |
| 124 |
or $print_help++; |
| 125 |
|
| 126 |
$verbose++ if $debug; # --debug implies --verbose |
| 127 |
|
| 128 |
|
| 129 |
# If version number requested, print it and exit |
| 130 |
|
| 131 |
if ($print_version) |
| 132 |
{ |
| 133 |
print <<EOF; |
| 134 |
$program version $version |
| 135 |
|
| 136 |
Copyright (C) 1997-1998 Ford & Mason Ltd |
| 137 |
This is free software; see the source for copying conditions. |
| 138 |
There is NO warranty; not even for MERCHANTABILITY or FITNESS |
| 139 |
FOR A PARTICULAR PURPOSE. |
| 140 |
|
| 141 |
Written by Andrew Ford <A.Ford\@ford-mason.co.uk> |
| 142 |
|
| 143 |
$program is part of the cronolog package. |
| 144 |
The latest version of which can be found at: |
| 145 |
|
| 146 |
http://www.ford-mason.co.uk/resources/cronolog/ |
| 147 |
EOF |
| 148 |
exit(0); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
# If help requested, print it and exit |
| 153 |
|
| 154 |
if ($print_help) |
| 155 |
{ |
| 156 |
print <<EOS; |
| 157 |
Usage: $program --template=TEMPLATE [OPTIONS] file ... |
| 158 |
|
| 159 |
--template=TEMPLATE output log-file template |
| 160 |
--print-invalid print invalid log-file entries |
| 161 |
--help print this help, then exit |
| 162 |
--version print version number, then exit |
| 163 |
EOS |
| 164 |
exit(0); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
# Process each input file specified |
| 170 |
|
| 171 |
push(@ARGV, "-") unless @ARGV; |
| 172 |
|
| 173 |
|
| 174 |
FILE: |
| 175 |
foreach $file (@ARGV) { |
| 176 |
if ($file eq "-") { |
| 177 |
print STDERR "reading from STDIN\n" if $verbose; |
| 178 |
*INFILE = *STDIN; |
| 179 |
} |
| 180 |
elsif ($file =~ /\.gz$/) { |
| 181 |
print STDERR "opening \"$file\"\n" if $verbose; |
| 182 |
|
| 183 |
if (!open(INFILE, "zcat $file|")) { |
| 184 |
print STDERR "cannot open \"$file\"\n"; |
| 185 |
next FILE; |
| 186 |
} |
| 187 |
} |
| 188 |
else { |
| 189 |
print STDERR "opening \"$file\"\n" if $verbose; |
| 190 |
|
| 191 |
if (! open(INFILE, $file)) { |
| 192 |
print STDERR "cannot open \"$file\"\n"; |
| 193 |
next FILE; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
# For each line in the current input log file parse the line, |
| 199 |
# determine the appropritate output log and write the line. |
| 200 |
|
| 201 |
LINE: |
| 202 |
while (<INFILE>) |
| 203 |
{ |
| 204 |
my($host, $ident, $authuser, $timestamp, |
| 205 |
$request, $status, $bytes, $trailer) = /$log_entry_pattern/; |
| 206 |
|
| 207 |
if (!($host && $ident && $authuser && $timestamp && $request && $status)) |
| 208 |
{ |
| 209 |
if ($PrintInvalids) { print STDERR "$.:$saveline"; } |
| 210 |
next LINE; |
| 211 |
} |
| 212 |
|
| 213 |
if ($timestamp =~ /$timestamp_pattern/) |
| 214 |
{ |
| 215 |
($day, $mon, $year, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6); |
| 216 |
} |
| 217 |
else |
| 218 |
{ |
| 219 |
if ($PrintInvalids) { print STDERR "$.:$saveline"; } |
| 220 |
next LINE; |
| 221 |
} |
| 222 |
|
| 223 |
next LINE unless defined($outfile = &get_handle($template, |
| 224 |
$day, $mon, $year, |
| 225 |
$hour, $min, $sec)); |
| 226 |
|
| 227 |
print($outfile $host, ' ', $ident, ' ', $authuser, |
| 228 |
' [', $timestamp, '] "', $request, '" ', |
| 229 |
$status, ' ', $bytes, $trailer, "\n"); |
| 230 |
|
| 231 |
} |
| 232 |
close(INFILE); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
# Get a file handle for a log file, closing the oldest handle if there |
| 237 |
# are too many handles open |
| 238 |
|
| 239 |
sub get_handle |
| 240 |
{ |
| 241 |
my($template, $day, $mon, $year, $hour, $min, $sec) = @_; |
| 242 |
$mon = $month{$mon}; |
| 243 |
|
| 244 |
|
| 245 |
# Determine the filename from the template and time |
| 246 |
|
| 247 |
my($file) = $template; |
| 248 |
$file =~ s/%Y/sprintf("%04d", $year)/eg; |
| 249 |
$file =~ s/%m/sprintf("%02d", $mon)/eg; |
| 250 |
$file =~ s/%d/sprintf("%02d", $day)/eg; |
| 251 |
$file =~ s/%b/$mmm[$mon]/g; |
| 252 |
$file =~ s/%B/$month[$mon]/g; |
| 253 |
$file =~ s/%H/sprintf("%02d", $hour)/eg; |
| 254 |
$file =~ s/%M/sprintf("%02m", $min)/eg; |
| 255 |
$file =~ s/%S/sprintf("%02m", $sec)/eg; |
| 256 |
|
| 257 |
|
| 258 |
# See if we already have it open and ready to write |
| 259 |
|
| 260 |
return $handle if defined($handle = $OpenHandles{$file}); |
| 261 |
|
| 262 |
# See if we already have too many files opened |
| 263 |
|
| 264 |
if (($#HandlesInUse + 1) >= $MaxHandles) |
| 265 |
{ |
| 266 |
local($oldkey) = shift @HandlesInUse; # close the oldest |
| 267 |
$handle = $OpenHandles{$oldkey}; |
| 268 |
delete $OpenHandles{$oldkey}; |
| 269 |
close $handle; |
| 270 |
} |
| 271 |
|
| 272 |
# Finally, try to open and remember a new handle for this pathkey |
| 273 |
|
| 274 |
$handle = ++$NextHandle; |
| 275 |
|
| 276 |
make_dirs($file); |
| 277 |
|
| 278 |
if (open($handle, ">>$file")) |
| 279 |
{ |
| 280 |
push(@HandlesInUse, $file); |
| 281 |
$OpenHandles{$file} = $handle; |
| 282 |
return $handle; |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
warn "Failed open of $file: $!\n"; |
| 287 |
return undef; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
# Make any missing directories on the path specified |
| 293 |
# (this subroutine is not optimal). |
| 294 |
|
| 295 |
sub make_dirs |
| 296 |
{ |
| 297 |
my($path) = shift; |
| 298 |
my($abs); |
| 299 |
|
| 300 |
# Strip off the filename bit |
| 301 |
|
| 302 |
$path =~ s!/[^/]*$!! or return; |
| 303 |
|
| 304 |
|
| 305 |
# Return early if the directory exists |
| 306 |
|
| 307 |
return if -d $path; |
| 308 |
|
| 309 |
|
| 310 |
# Trim off any leading '/' (remembering whether the path was |
| 311 |
# absolute or relative) |
| 312 |
|
| 313 |
$path =~ s!^(/)!!; |
| 314 |
$abs = $1; |
| 315 |
|
| 316 |
|
| 317 |
# Split what's left into directories |
| 318 |
|
| 319 |
my(@path) = split(/\//, $path); |
| 320 |
|
| 321 |
|
| 322 |
# Check each directory on the path |
| 323 |
|
| 324 |
foreach $i (0 .. $#path) |
| 325 |
{ |
| 326 |
$path = $abs . join("/", (@path[0 .. $i])); |
| 327 |
print(STDERR "Testing $path\n") if $debug; |
| 328 |
print(STDERR "Making $path\n") if !-d $path and $debug; |
| 329 |
mkdir($path, $DirMode) unless -d $path; |
| 330 |
} |
| 331 |
} |