Debian Codegraph @

src/cronosplit.in

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
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 require 5.8.0;
74
75 use Getopt::Long;
76 use Time::Local;
77 use POSIX qw(strftime);
78
79 use strict;
80
81 my $program = 'cronosplit';
82 my $version = '@VERSION@';
83
84 # Programs
85
86 my $ZCAT = '/bin/zcat';
87 my $BZCAT = '/usr/bin/bzcat';
88
89 # Parameters
90
91 my $MaxHandles = 50;
92 my $DirMode = 0775;
93
94 # Patterns for log file entries (Common Log Format) and timestamps
95
96 my $access_log_entry_pattern = "^(\\S+) (\\S+) ([^[]+) \\[([^]]*)] \"([^\"]*)\" (\\S+) (\\S+)(.*)";
97 my $access_timestamp_pattern = "^([ 0-3]?\\d)/([A-Za-z]+)/(\\d{4}):(\\d\\d):(\\d\\d):(\\d\\d) [+ -]\\d{1,4}";
98
99 my $error_log_entry_pattern = "^\\[([^]]*)] \\[([^]]*)] (\\S+)(.*)";
100 my $error_timestamp_pattern = "^[A-Z][a-z]{2} ([A-Z][a-z]{2}) ([0-3]?\\d) (\\d\\d):(\\d\\d):(\\d\\d) (\\d{4})";
101
102 # An associative array of month names and abbreviations
103
104 my %month = (Jan => 1, January => 1,
105 Feb => 2, February => 2,
106 Mar => 3, March => 3,
107 Apr => 4, April => 4,
108 May => 5,
109 Jun => 6, June => 6,
110 Jul => 7, July => 7,
111 Aug => 8, August => 8,
112 Sep => 9, September => 9, Sept => 9,
113 Oct => 10, October => 10,
114 Nov => 11, November => 11,
115 Dec => 12, December => 12);
116
117
118 # Variables
119
120 my $file;
121 my $outfile;
122 my $template;
123 my $debug;
124 my $verbose;
125 my $PrintInvalids;
126 my $print_help;
127 my $print_version;
128 my $handle;
129
130 my %OpenHandles = ();
131 my @HandlesInUse = ();
132
133 # Process options
134
135 (GetOptions("template=s", \$template,
136 "print-invalid", \$PrintInvalids,
137 "debug", \$debug,
138 "verbose", \$verbose,
139 "help", \$print_help,
140 "version", \$print_version)
141 and ($print_help or $print_version or $template))
142 or $print_help++;
143
144 $verbose++ if $debug; # --debug implies --verbose
145
146
147 # If version number requested, print it and exit
148
149 if ($print_version)
150 {
151 print <<EOF;
152 $program version $version
153
154 Copyright (C) 1997-1998 Ford & Mason Ltd
155 This is free software; see the source for copying conditions.
156 There is NO warranty; not even for MERCHANTABILITY or FITNESS
157 FOR A PARTICULAR PURPOSE.
158
159 Written by Andrew Ford <A.Ford\@ford-mason.co.uk>
160
161 $program is part of the cronolog package.
162 The latest version of which can be found at:
163
164 http://www.ford-mason.co.uk/resources/cronolog/
165 EOF
166 exit(0);
167 }
168
169
170 # If help requested, print it and exit
171
172 if ($print_help)
173 {
174 print <<EOS;
175 Usage: $program --template=TEMPLATE [OPTIONS] file ...
176
177 --template=TEMPLATE output log-file template
178 --print-invalid print invalid log-file entries
179 --help print this help, then exit
180 --version print version number, then exit
181 EOS
182 exit(0);
183 }
184
185
186
187 # Process each input file specified
188
189 push(@ARGV, "-") unless @ARGV;
190
191 FILE:
192 foreach $file (@ARGV) {
193 if ($file eq "-") {
194 print STDERR "reading from STDIN\n" if $verbose;
195 *INFILE = *STDIN;
196 }
197 elsif ($file =~ /\.gz$/) {
198 print STDERR "opening \"$file\"\n" if $verbose;
199
200 if (!open(INFILE, "-|", $ZCAT, $file)) {
201 print STDERR "cannot open \"$file\"\n";
202 next FILE;
203 }
204 }
205 elsif ($file =~ /\.bz2$/) {
206 if (! -x $BZCAT)
207 {
208 print STDERR "Cannot process \"$file\", install bzip2 first.\n";
209 next FILE;
210 }
211 print STDERR "opening \"$file\"\n" if $verbose;
212
213 if (!open(INFILE, "-|", $BZCAT, $file)) {
214 print STDERR "cannot open \"$file\"\n";
215 next FILE;
216 }
217 }
218 else {
219 print STDERR "opening \"$file\"\n" if $verbose;
220
221 if (! open(INFILE, "<$file")) {
222 print STDERR "cannot open \"$file\"\n";
223 next FILE;
224 }
225 }
226
227
228 # For each line in the current input log file parse the line,
229 # determine the appropritate output log and write the line.
230
231 LINE:
232 while (<INFILE>)
233 {
234 my ($timestamp, $time_t);
235 if (/$access_log_entry_pattern/)
236 {
237 $timestamp = $4;
238 if ($timestamp =~ /$access_timestamp_pattern/)
239 {
240 # 1:day 2:month 3:year 4:hour 5:minute 6:second
241 $time_t = eval { timelocal($6, $5, $4, $1, $month{$2} - 1, $3 - 1900) };
242 }
243 }
244 elsif (/$error_log_entry_pattern/)
245 {
246 $timestamp = $1;
247 if ($timestamp =~ /$error_timestamp_pattern/)
248 {
249 # 1:month 2:day 3:hour 4:minute 5:second 6:year
250 $time_t = eval { timelocal($5, $4, $3, $2, $month{$1} - 1, $6 - 1900) };
251 }
252 }
253 if (!defined ($time_t))
254 {
255 if ($PrintInvalids) { print STDERR "$.:$_"; }
256 next LINE;
257 }
258
259 next LINE unless defined($outfile = &get_handle($template, $time_t));
260
261 print($outfile $_);
262
263 }
264 close(INFILE);
265 }
266
267
268 # Get a file handle for a log file, closing the oldest handle if there
269 # are too many handles open
270
271 sub get_handle
272 {
273 my($template, $time_t) = @_;
274
275
276 # Determine the filename from the template and time
277
278 my($file) = $template;
279 my(@time) = localtime($time_t);
280 $file =~ s/(%[a-zA-Z%])/strftime($1, @time)/eg;
281
282 # See if we already have it open and ready to write
283
284 return $handle if defined($handle = $OpenHandles{$file});
285
286 # See if we already have too many files opened
287
288 if (($#HandlesInUse + 1) >= $MaxHandles)
289 {
290 my ($oldkey) = shift @HandlesInUse; # close the oldest
291 $handle = $OpenHandles{$oldkey};
292 delete $OpenHandles{$oldkey};
293 close $handle;
294 }
295
296 # Finally, try to open and remember a new handle for this pathkey
297
298 undef $handle;
299
300 make_dirs($file);
301
302 if (open($handle, ">>$file"))
303 {
304 push(@HandlesInUse, $file);
305 $OpenHandles{$file} = $handle;
306 return $handle;
307 }
308 else
309 {
310 warn "Failed open of $file: $!\n";
311 return undef;
312 }
313 }
314
315
316 # Make any missing directories on the path specified
317 # (this subroutine is not optimal).
318
319 sub make_dirs
320 {
321 my($path) = shift;
322 my($abs);
323
324 # Strip off the filename bit
325
326 $path =~ s!/[^/]*$!! or return;
327
328
329 # Return early if the directory exists
330
331 return if -d $path;
332
333
334 # Trim off any leading '/' (remembering whether the path was
335 # absolute or relative)
336
337 $path =~ s!^(/)!!;
338 $abs = $1;
339
340
341 # Split what's left into directories
342
343 my(@path) = split(/\//, $path);
344
345
346 # Check each directory on the path
347
348 foreach my $i (0 .. $#path)
349 {
350 $path = $abs . join("/", (@path[0 .. $i]));
351 print(STDERR "Testing $path\n") if $debug;
352 print(STDERR "Making $path\n") if !-d $path and $debug;
353 mkdir($path, $DirMode) unless -d $path;
354 }
355 }