Debian Codegraph @

debian/patches/040_cronosplit.diff

raw symbols SCIP
1 Description: cronosplit bugfixes and improvements.
2 from changelog:
3 * cronosplit bugfixes:
4 * Print content of invalid lines.
5 * Also split error logfiles. (Closes: #241492)
6 * Safe open for input files/pipes.
7 * cronosplit improvements:
8 * use strict;
9 * Use strftime for template conversion.
10 * Add processing of bzip2 compressed logfiles.
11 * Incorporate the above changes + 020_cronosplit_HMS.diff into new
12 040_cronosplit.diff.
13 Author: Christoph Biedl <[email protected]>
14 Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=241492
15 Forwarded: no
16 Last-Update: 2011-05-27
17
18 Index: cronolog-1.6.2/src/cronosplit.in
19 ===================================================================
20 --- cronolog-1.6.2.orig/src/cronosplit.in 1999-12-27 16:52:52.000000000 +0100
21 +++ cronolog-1.6.2/src/cronosplit.in 2011-05-27 03:18:06.620455021 +0200
22 @@ -70,26 +70,38 @@
23 # (splitlog is part of the wwwstat package,
24 # see <http://www.ics.uci.edu/pub/websoft/wwwstat/>)
25
26 +require 5.8.0;
27
28 use Getopt::Long;
29 +use Time::Local;
30 +use POSIX qw(strftime);
31
32 -$program = 'cronosplit';
33 -$version = '@VERSION@';
34 +use strict;
35
36 -# Parameters
37 +my $program = 'cronosplit';
38 +my $version = '@VERSION@';
39 +
40 +# Programs
41
42 -$MaxHandles = 50;
43 -$DirMode = 0775;
44 +my $ZCAT = '/bin/zcat';
45 +my $BZCAT = '/usr/bin/bzcat';
46 +
47 +# Parameters
48
49 +my $MaxHandles = 50;
50 +my $DirMode = 0775;
51
52 # Patterns for log file entries (Common Log Format) and timestamps
53
54 -$log_entry_pattern = "^(\\S+) (\\S+) ([^[]+) \\[([^]]*)] \"([^\"]*)\" (\\S+) (\\S+)(.*)";
55 -$timestamp_pattern = "^([ 0-3]?\\d)/([A-Za-z]+)/(\\d{4}):(\\d\\d):(\\d\\d):(\\d\\d) [+ -]\\d{1,4}";
56 +my $access_log_entry_pattern = "^(\\S+) (\\S+) ([^[]+) \\[([^]]*)] \"([^\"]*)\" (\\S+) (\\S+)(.*)";
57 +my $access_timestamp_pattern = "^([ 0-3]?\\d)/([A-Za-z]+)/(\\d{4}):(\\d\\d):(\\d\\d):(\\d\\d) [+ -]\\d{1,4}";
58 +
59 +my $error_log_entry_pattern = "^\\[([^]]*)] \\[([^]]*)] (\\S+)(.*)";
60 +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})";
61
62 # An associative array of month names and abbreviations
63
64 -%month = (Jan => 1, January => 1,
65 +my %month = (Jan => 1, January => 1,
66 Feb => 2, February => 2,
67 Mar => 3, March => 3,
68 Apr => 4, April => 4,
69 @@ -102,15 +114,21 @@
70 Nov => 11, November => 11,
71 Dec => 12, December => 12);
72
73 -@month = ("", "January", "February", "March", "April", "May", "June",
74 - "July", "August", "September", "October", "November", "December");
75 -@mmm = ("", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
76 - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
77 -
78 -@days = ("Sunday", "Monday", "Tuesday", "Wednesday",
79 - "Thursday", "Friday", "Saturday");
80 -@ddd = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
81
82 +# Variables
83 +
84 +my $file;
85 +my $outfile;
86 +my $template;
87 +my $debug;
88 +my $verbose;
89 +my $PrintInvalids;
90 +my $print_help;
91 +my $print_version;
92 +my $handle;
93 +
94 +my %OpenHandles = ();
95 +my @HandlesInUse = ();
96
97 # Process options
98
99 @@ -170,25 +188,37 @@
100
101 push(@ARGV, "-") unless @ARGV;
102
103 -
104 FILE:
105 foreach $file (@ARGV) {
106 if ($file eq "-") {
107 print STDERR "reading from STDIN\n" if $verbose;
108 *INFILE = *STDIN;
109 - }
110 + }
111 elsif ($file =~ /\.gz$/) {
112 print STDERR "opening \"$file\"\n" if $verbose;
113
114 - if (!open(INFILE, "zcat $file|")) {
115 + if (!open(INFILE, "-|", $ZCAT, $file)) {
116 + print STDERR "cannot open \"$file\"\n";
117 + next FILE;
118 + }
119 + }
120 + elsif ($file =~ /\.bz2$/) {
121 + if (! -x $BZCAT)
122 + {
123 + print STDERR "Cannot process \"$file\", install bzip2 first.\n";
124 + next FILE;
125 + }
126 + print STDERR "opening \"$file\"\n" if $verbose;
127 +
128 + if (!open(INFILE, "-|", $BZCAT, $file)) {
129 print STDERR "cannot open \"$file\"\n";
130 next FILE;
131 }
132 }
133 else {
134 print STDERR "opening \"$file\"\n" if $verbose;
135 -
136 - if (! open(INFILE, $file)) {
137 +
138 + if (! open(INFILE, "<$file")) {
139 print STDERR "cannot open \"$file\"\n";
140 next FILE;
141 }
142 @@ -201,32 +231,34 @@
143 LINE:
144 while (<INFILE>)
145 {
146 - my($host, $ident, $authuser, $timestamp,
147 - $request, $status, $bytes, $trailer) = /$log_entry_pattern/;
148 -
149 - if (!($host && $ident && $authuser && $timestamp && $request && $status))
150 - {
151 - if ($PrintInvalids) { print STDERR "$.:$saveline"; }
152 - next LINE;
153 - }
154 -
155 - if ($timestamp =~ /$timestamp_pattern/)
156 + my ($timestamp, $time_t);
157 + if (/$access_log_entry_pattern/)
158 {
159 - ($day, $mon, $year, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6);
160 - }
161 - else
162 + $timestamp = $4;
163 + if ($timestamp =~ /$access_timestamp_pattern/)
164 + {
165 + # 1:day 2:month 3:year 4:hour 5:minute 6:second
166 + $time_t = eval { timelocal($6, $5, $4, $1, $month{$2} - 1, $3 - 1900) };
167 + }
168 + }
169 + elsif (/$error_log_entry_pattern/)
170 + {
171 + $timestamp = $1;
172 + if ($timestamp =~ /$error_timestamp_pattern/)
173 + {
174 + # 1:month 2:day 3:hour 4:minute 5:second 6:year
175 + $time_t = eval { timelocal($5, $4, $3, $2, $month{$1} - 1, $6 - 1900) };
176 + }
177 + }
178 + if (!defined ($time_t))
179 {
180 - if ($PrintInvalids) { print STDERR "$.:$saveline"; }
181 + if ($PrintInvalids) { print STDERR "$.:$_"; }
182 next LINE;
183 }
184
185 - next LINE unless defined($outfile = &get_handle($template,
186 - $day, $mon, $year,
187 - $hour, $min, $sec));
188 -
189 - print($outfile $host, ' ', $ident, ' ', $authuser,
190 - ' [', $timestamp, '] "', $request, '" ',
191 - $status, ' ', $bytes, $trailer, "\n");
192 + next LINE unless defined($outfile = &get_handle($template, $time_t));
193 +
194 + print($outfile $_);
195
196 }
197 close(INFILE);
198 @@ -238,22 +270,14 @@
199
200 sub get_handle
201 {
202 - my($template, $day, $mon, $year, $hour, $min, $sec) = @_;
203 - $mon = $month{$mon};
204 + my($template, $time_t) = @_;
205
206
207 # Determine the filename from the template and time
208
209 my($file) = $template;
210 - $file =~ s/%Y/sprintf("%04d", $year)/eg;
211 - $file =~ s/%m/sprintf("%02d", $mon)/eg;
212 - $file =~ s/%d/sprintf("%02d", $day)/eg;
213 - $file =~ s/%b/$mmm[$mon]/g;
214 - $file =~ s/%B/$month[$mon]/g;
215 - $file =~ s/%H/sprintf("%02d", $hour)/eg;
216 - $file =~ s/%M/sprintf("%02m", $min)/eg;
217 - $file =~ s/%S/sprintf("%02m", $sec)/eg;
218 -
219 + my(@time) = localtime($time_t);
220 + $file =~ s/(%[a-zA-Z%])/strftime($1, @time)/eg;
221
222 # See if we already have it open and ready to write
223
224 @@ -263,7 +287,7 @@
225
226 if (($#HandlesInUse + 1) >= $MaxHandles)
227 {
228 - local($oldkey) = shift @HandlesInUse; # close the oldest
229 + my ($oldkey) = shift @HandlesInUse; # close the oldest
230 $handle = $OpenHandles{$oldkey};
231 delete $OpenHandles{$oldkey};
232 close $handle;
233 @@ -271,7 +295,7 @@
234
235 # Finally, try to open and remember a new handle for this pathkey
236
237 - $handle = ++$NextHandle;
238 + undef $handle;
239
240 make_dirs($file);
241
242 @@ -321,7 +345,7 @@
243
244 # Check each directory on the path
245
246 - foreach $i (0 .. $#path)
247 + foreach my $i (0 .. $#path)
248 {
249 $path = $abs . join("/", (@path[0 .. $i]));
250 print(STDERR "Testing $path\n") if $debug;