| 1 | package Lemonolap::Log4lemon ; | ||
| 2 | use strict; | ||
| 3 | our $VERSION = '0.03'; | ||
| 4 | sub can_field { | ||
| 5 | my $self = shift; | ||
| 6 | return ( 'date','time','node','source','url','response','size','referer','agent','vhost','uid'); | ||
| 7 | |||
| 8 | } | ||
| 9 | sub get_line { | ||
| 10 | |||
| 11 | my $line = shift; | ||
| 12 | chomp ($line); | ||
| 13 | my @tab = split /HTTP/ , $line; | ||
| 14 | (my $filler,my $machine,my $source) = $tab[0]=~/.+(\d\d:\d\d\s?)(.+) logger: ([^ ]+)/; | ||
| 15 | (my $date,my $hour) =$tab[0]=~ /\[([^:]+)(.+)\]/ ; | ||
| 16 | $date =~ s/Nov/11/; | ||
| 17 | $date =~ s/Dec/12/; | ||
| 18 | $date =~ s/Jan/01/; | ||
| 19 | $date =~ s/Feb/02/; | ||
| 20 | $date =~ s/Mar/03/; | ||
| 21 | $date =~ s/Apr/04/; | ||
| 22 | $date =~ s/May/05/; | ||
| 23 | $date =~ s/Jun/06/; | ||
| 24 | $date =~ s/Jui/07/; | ||
| 25 | $date =~ s/Aug/08/; | ||
| 26 | $date =~ s/Sep/09/; | ||
| 27 | $date =~ s/Oct/10/; | ||
| 28 | $date=~ s/\///g; | ||
| 29 | my $zdate= substr($date,4,4).substr($date,2,2).substr($date,0,2); | ||
| 30 | |||
| 31 | |||
| 32 | $hour =~ s/^://; | ||
| 33 | $hour =~ s/ .+$//; | ||
| 34 | $hour=~ s/://g; | ||
| 35 | (my $url) =$tab[0]=~ /.+\"\w+\s(.+?) $/ ; | ||
| 36 | (my $uid) = $tab[0]=~ /uid=([^ ]+)/ ; | ||
| 37 | my @tab2 = split /"/, $tab[1]; | ||
| 38 | (my $response,my $weight) = $tab2[1]=~ /(\d\d\d) (\d+)/; | ||
| 39 | ($response) =$tab2[1]=~ /(\d\d\d)/ unless $response; | ||
| 40 | my $referer= $tab2[2]; | ||
| 41 | undef $referer if $referer=~ /-$/; | ||
| 42 | my $client= $tab2[4]; | ||
| 43 | my $vhost = $tab2[7]; | ||
| 44 | $vhost=~ s/ //g; | ||
| 45 | return ($zdate,$hour,$machine,$source,$url,$response,$weight,$referer,$client,$vhost,$uid); | ||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | } | ||
| 52 | sub get_field_by_name{ | ||
| 53 | my $self = shift; | ||
| 54 | my %args= @_; | ||
| 55 | my $separator =$args{separator}||'|'; | ||
| 56 | if ($args{header}) { | ||
| 57 | my @t =$self->can_field(); | ||
| 58 | if ( $args{'fields'} ) { | ||
| 59 | my @tab =@{$args{fields} } ; | ||
| 60 | @t =@tab; | ||
| 61 | } | ||
| 62 | my $l = join $separator, @t; | ||
| 63 | return $l ; | ||
| 64 | |||
| 65 | } | ||
| 66 | my $FILE =$self->{handler}; | ||
| 67 | my $line = <$FILE> ; | ||
| 68 | unless ($line ) { | ||
| 69 | |||
| 70 | close $FILE; | ||
| 71 | return undef; | ||
| 72 | } | ||
| 73 | my @fline = get_line($line); | ||
| 74 | if ( $args{'fields'} ) { | ||
| 75 | my @a =$self->can_field(); | ||
| 76 | my %tmp; | ||
| 77 | foreach (@a) { | ||
| 78 | my $value = shift @fline ; | ||
| 79 | $tmp{$_} = $value; | ||
| 80 | } | ||
| 81 | my @tab =@{$args{fields} } ; | ||
| 82 | my @tt; | ||
| 83 | foreach (@tab) { | ||
| 84 | my $name =$_; | ||
| 85 | if (lc ($name) eq 'date:aaaa') { | ||
| 86 | $tmp{$name} = substr($tmp{date},0,4); | ||
| 87 | } | ||
| 88 | if (lc ($name) eq 'date:mm') { | ||
| 89 | $tmp{$name} = substr($tmp{date},4,2); | ||
| 90 | } | ||
| 91 | if (lc ($name) eq 'date:dd') { | ||
| 92 | $tmp{$name} = substr($tmp{date},6,2); | ||
| 93 | } | ||
| 94 | if (lc ($name) eq 'time:hh') { | ||
| 95 | $tmp{$name} = substr($tmp{time},0,2); | ||
| 96 | } | ||
| 97 | if (lc ($name) eq 'time:mm') { | ||
| 98 | $tmp{$name} = substr($tmp{time},2,2); | ||
| 99 | } | ||
| 100 | if (lc ($name) eq 'time:se') { | ||
| 101 | $tmp{$name} = substr($tmp{time},4,2); | ||
| 102 | } | ||
| 103 | |||
| 104 | push @tt , $tmp{$name} ; | ||
| 105 | |||
| 106 | } | ||
| 107 | return join $separator,@tt; | ||
| 108 | |||
| 109 | } else | ||
| 110 | { return join $separator ,@fline ;} | ||
| 111 | |||
| 112 | |||
| 113 | } | ||
| 114 | |||
| 115 | sub get_field_by_label{ | ||
| 116 | my $self = shift; | ||
| 117 | my %args= @_; | ||
| 118 | my $separator =$args{separator}||'|'; | ||
| 119 | if ($args{header}) { | ||
| 120 | my @t =$self->can_field(); | ||
| 121 | if ( $args{'label'} ) { | ||
| 122 | my @tab =@{$args{label} } ; | ||
| 123 | @t =@tab; | ||
| 124 | } | ||
| 125 | |||
| 126 | my $l = join $separator, @t; | ||
| 127 | return $l ; | ||
| 128 | |||
| 129 | } | ||
| 130 | my $FILE =$self->{handler}; | ||
| 131 | my $line = <$FILE> ; | ||
| 132 | unless ($line ) { | ||
| 133 | |||
| 134 | close $FILE; | ||
| 135 | return undef; | ||
| 136 | } | ||
| 137 | my @fline = get_line($line); | ||
| 138 | if ( $args{'label'} ) { | ||
| 139 | my @a =$self->can_field(); | ||
| 140 | my %tmp; | ||
| 141 | foreach (@a) { | ||
| 142 | my $value = shift @fline ; | ||
| 143 | $tmp{$_} = $value; | ||
| 144 | } | ||
| 145 | my @tab =@{$args{fields} } ; | ||
| 146 | my @tt; | ||
| 147 | foreach (@tab) { | ||
| 148 | # mapping | ||
| 149 | my $name = $self->{mapping}{$_} ; | ||
| 150 | if (lc ($name) eq 'date:aaaa') { | ||
| 151 | $tmp{$name} = substr($tmp{date},0,4); | ||
| 152 | } | ||
| 153 | if (lc ($name) eq 'date:mm') { | ||
| 154 | $tmp{$name} = substr($tmp{date},4,2); | ||
| 155 | } | ||
| 156 | if (lc ($name) eq 'date:dd') { | ||
| 157 | $tmp{$name} = substr($tmp{date},6,2); | ||
| 158 | } | ||
| 159 | if (lc ($name) eq 'time:hh') { | ||
| 160 | $tmp{$name} = substr($tmp{time},0,2); | ||
| 161 | } | ||
| 162 | if (lc ($name) eq 'time:mm') { | ||
| 163 | $tmp{$name} = substr($tmp{time},2,2); | ||
| 164 | } | ||
| 165 | if (lc ($name) eq 'time:se') { | ||
| 166 | $tmp{$name} = substr($tmp{time},4,2); | ||
| 167 | } | ||
| 168 | |||
| 169 | push @tt , $tmp{$name} ; | ||
| 170 | |||
| 171 | } | ||
| 172 | return join $separator,@tt; | ||
| 173 | |||
| 174 | } else | ||
| 175 | { return join $separator ,@fline ;} | ||
| 176 | |||
| 177 | |||
| 178 | } | ||
| 179 | |||
| 180 | sub apply { | ||
| 181 | my $self =shift; | ||
| 182 | my %args = @_; | ||
| 183 | my $file_in = $args{infile}; | ||
| 184 | my $file_out = $args{outfile}; | ||
| 185 | $self->{file_in} =$file_in unless $self->{file_in}; | ||
| 186 | $self->{file_out} =$file_out unless $self->{file_out}; | ||
| 187 | |||
| 188 | $self->{header} =$args{header}; | ||
| 189 | my $FILE ; | ||
| 190 | my $FILEOUT; | ||
| 191 | open ( $FILE,"< $file_in") || die "$file_in $!\n"; | ||
| 192 | open ( $FILEOUT,"> $file_out") || die "$file_out $!\n"; | ||
| 193 | $self->{handler} =$FILE; | ||
| 194 | if ($self->{header} ) { | ||
| 195 | my $separator =$args{separator}||'|'; | ||
| 196 | my @t =$self->can_field(); | ||
| 197 | my $l = join $separator, @t; | ||
| 198 | print $FILEOUT "$l\n"; | ||
| 199 | } | ||
| 200 | while (my $l =$self->get_field_by_name() ) { | ||
| 201 | print $FILEOUT "$l\n" ; | ||
| 202 | } | ||
| 203 | close $FILE; | ||
| 204 | close $FILEOUT; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub new { | ||
| 208 | my $class =shift; | ||
| 209 | my %args = @_; | ||
| 210 | my $self; | ||
| 211 | $self=\%args;; | ||
| 212 | bless $self,$class; | ||
| 213 | return $self; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub set_output { | ||
| 217 | my $self =shift; | ||
| 218 | $self->{file_out} = shift; | ||
| 219 | return 1; | ||
| 220 | } | ||
| 221 | sub set_label { | ||
| 222 | my $self =shift; | ||
| 223 | my %args = @_; | ||
| 224 | foreach (keys %args) { | ||
| 225 | $self->{mapping}{$_} =$args{$_} ; | ||
| 226 | $self->{mapping}{$args{$_}} =$_ ; | ||
| 227 | } | ||
| 228 | return 1; | ||
| 229 | |||
| 230 | } | ||
| 231 | 1; | ||
| 232 | __END__ |