Debian Codegraph @

src/cronosplit.in

raw ◀ ▶
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 $utime;
126 my $PrintInvalids;
127 my $print_help;
128 my $print_version;
129 my $handle;
130
131 my %OpenHandles = ();
132 my @HandlesInUse = ();
•133 my %utimes = ();
134
135 # Process options
136
137 (GetOptions("template=s", \$template,
138 "print-invalid", \$PrintInvalids,
•139 "utime", \$utime,
140 "debug", \$debug,
141 "verbose", \$verbose,
142 "help", \$print_help,
143 "version", \$print_version)
144 and ($print_help or $print_version or $template))
145 or $print_help++;
146
147 $verbose++ if $debug; # --debug implies --verbose
148
149
150 # If version number requested, print it and exit
151
152 if ($print_version)
153 {
154 print <<EOF;
155 $program version $version
156
157 Copyright (C) 1997-1998 Ford & Mason Ltd
158 This is free software; see the source for copying conditions.
159 There is NO warranty; not even for MERCHANTABILITY or FITNESS
160 FOR A PARTICULAR PURPOSE.
161
162 Written by Andrew Ford <A.Ford\@ford-mason.co.uk>
163
164 $program is part of the cronolog package.
165 The latest version of which can be found at:
166
167 http://www.ford-mason.co.uk/resources/cronolog/
168 EOF
169 exit(0);
170 }
171
172
173 # If help requested, print it and exit
174
175 if ($print_help)
176 {
177 print <<EOS;
178 Usage: $program --template=TEMPLATE [OPTIONS] file ...
179
180 --template=TEMPLATE output log-file template
181 --print-invalid print invalid log-file entries
•182 --utime update mtime of output files to last entry
183 --help print this help, then exit
184 --version print version number, then exit
185 EOS
186 exit(0);
187 }
188
189
190
191 # Process each input file specified
192
193 push(@ARGV, "-") unless @ARGV;
194
195 FILE:
196 foreach $file (@ARGV) {
197 if ($file eq "-") {
198 print STDERR "reading from STDIN\n" if $verbose;
199 *INFILE = *STDIN;
•200 }
201 elsif ($file =~ /\.gz$/) {
202 print STDERR "opening \"$file\"\n" if $verbose;
203
•204 if (!open(INFILE, "-|", $ZCAT, $file)) {
205 print STDERR "cannot open \"$file\"\n";
206 next FILE;
207 }
208 }
209 elsif ($file =~ /\.bz2$/) {
210 if (! -x $BZCAT)
211 {
212 print STDERR "Cannot process \"$file\", install bzip2 first.\n";
213 next FILE;
214 }
215 print STDERR "opening \"$file\"\n" if $verbose;
216
217 if (!open(INFILE, "-|", $BZCAT, $file)) {
218 print STDERR "cannot open \"$file\"\n";
219 next FILE;
220 }
221 }
222 else {
223 print STDERR "opening \"$file\"\n" if $verbose;
•224
225 if (! open(INFILE, "<$file")) {
226 print STDERR "cannot open \"$file\"\n";
227 next FILE;
228 }
229 }
230
231
232 # For each line in the current input log file parse the line,
233 # determine the appropritate output log and write the line.
234
235 LINE:
236 while (<INFILE>)
237 {
•238 my ($timestamp, $time_t);
239 if (/$access_log_entry_pattern/)
240 {
•241 $timestamp = $4;
242 if ($timestamp =~ /$access_timestamp_pattern/)
243 {
244 # 1:day 2:month 3:year 4:hour 5:minute 6:second
245 $time_t = eval { timelocal($6, $5, $4, $1, $month{$2} - 1, $3 - 1900) };
246 }
247 }
248 elsif (/$error_log_entry_pattern/)
249 {
250 $timestamp = $1;
251 if ($timestamp =~ /$error_timestamp_pattern/)
252 {
253 # 1:month 2:day 3:hour 4:minute 5:second 6:year
254 $time_t = eval { timelocal($5, $4, $3, $2, $month{$1} - 1, $6 - 1900) };
255 }
256 }
257 if (!defined ($time_t))
258 {
•259 if ($PrintInvalids) { print STDERR "$.:$_"; }
260 next LINE;
261 }
262
•263 next LINE unless defined($outfile = &get_handle($template, $time_t));
264
265 print($outfile $_);
266
267 }
268 close(INFILE);
269 }
270
•271 # close any open files
272 foreach my $oldkey (@HandlesInUse)
273 {
274 $handle = $OpenHandles{$oldkey};
275 close $handle;
276 if ($utime)
277 {
278 my $mtime = $utimes{$oldkey};
279 utime $mtime, $mtime, $oldkey;
280 }
281 }
282
283 # Get a file handle for a log file, closing the oldest handle if there
284 # are too many handles open
285
286 sub get_handle
287 {
•288 my($template, $time_t) = @_;
289
290
291 # Determine the filename from the template and time
292
293 my($file) = $template;
•294 my(@time) = localtime($time_t);
295 $file =~ s/(%[a-zA-Z%])/strftime($1, @time)/eg;
296
•297 if ($utime)
298 {
299 if (!defined ($utimes{$file}) || $time_t > $utimes{$file})
300 {
301 $utimes{$file} = $time_t;
302 }
303 }
304
305 # See if we already have it open and ready to write
306
307 return $handle if defined($handle = $OpenHandles{$file});
308
309 # See if we already have too many files opened
310
311 if (($#HandlesInUse + 1) >= $MaxHandles)
312 {
•313 my ($oldkey) = shift @HandlesInUse; # close the oldest
314 $handle = $OpenHandles{$oldkey};
315 close $handle;
•316 if ($utime)
317 {
318 my $mtime = $utimes{$oldkey};
319 utime $mtime, $mtime, $oldkey;
320 }
321 delete $OpenHandles{$oldkey};
322 }
323
324 # Finally, try to open and remember a new handle for this pathkey
325
•326 undef $handle;
327
328 make_dirs($file);
329
330 if (open($handle, ">>$file"))
331 {
332 push(@HandlesInUse, $file);
333 $OpenHandles{$file} = $handle;
334 return $handle;
335 }
336 else
337 {
338 warn "Failed open of $file: $!\n";
339 return undef;
340 }
341 }
342
343
344 # Make any missing directories on the path specified
345 # (this subroutine is not optimal).
346
347 sub make_dirs
348 {
349 my($path) = shift;
350 my($abs);
351
352 # Strip off the filename bit
353
354 $path =~ s!/[^/]*$!! or return;
355
356
357 # Return early if the directory exists
358
359 return if -d $path;
360
361
362 # Trim off any leading '/' (remembering whether the path was
363 # absolute or relative)
364
365 $path =~ s!^(/)!!;
366 $abs = $1;
367
368
369 # Split what's left into directories
370
371 my(@path) = split(/\//, $path);
372
373
374 # Check each directory on the path
375
•376 foreach my $i (0 .. $#path)
377 {
378 $path = $abs . join("/", (@path[0 .. $i]));
379 print(STDERR "Testing $path\n") if $debug;
380 print(STDERR "Making $path\n") if !-d $path and $debug;
381 mkdir($path, $DirMode) unless -d $path;
382 }
383 }