| 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 |
|
| 33 |
|
| 34 |
+use strict; |
| 35 |
|
| 36 |
|
| 37 |
+my $program = 'cronosplit'; |
| 38 |
+my $version = '@VERSION@'; |
| 39 |
+ |
| 40 |
+# Programs |
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 55 |
|
| 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 |
|
| 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 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
+ my ($timestamp, $time_t); |
| 157 |
+ if (/$access_log_entry_pattern/) |
| 158 |
{ |
| 159 |
|
| 160 |
|
| 161 |
|
| 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 |
|
| 181 |
+ if ($PrintInvalids) { print STDERR "$.:$_"; } |
| 182 |
next LINE; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 203 |
|
| 204 |
+ my($template, $time_t) = @_; |
| 205 |
|
| 206 |
|
| 207 |
# Determine the filename from the template and time |
| 208 |
|
| 209 |
my($file) = $template; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 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 |
|
| 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 |
|
| 238 |
+ undef $handle; |
| 239 |
|
| 240 |
make_dirs($file); |
| 241 |
|
| 242 |
@@ -321,7 +345,7 @@ |
| 243 |
|
| 244 |
# Check each directory on the path |
| 245 |
|
| 246 |
|
| 247 |
+ foreach my $i (0 .. $#path) |
| 248 |
{ |
| 249 |
$path = $abs . join("/", (@path[0 .. $i])); |
| 250 |
print(STDERR "Testing $path\n") if $debug; |