File Coverage

blib/lib/App/RecordStream/Operation/fromapache.pm
Criterion Covered Total %
statement 15 54 27.7
branch 0 22 0.0
condition 0 3 0.0
subroutine 5 8 62.5
pod n/a
total 20 87 22.9


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::fromapache;
2              
3             our $VERSION = "4.0.24";
4              
5 2     2   302 use strict;
  2         5  
  2         60  
6 2     2   13 use warnings;
  2         4  
  2         70  
7              
8 2     2   12 use base qw(App::RecordStream::Operation);
  2         6  
  2         145  
9              
10 2     2   15 use App::RecordStream::Record;
  2         5  
  2         59  
11 2     2   251 use App::RecordStream::OptionalRequire qw(Apache::Log::Parser);
  2         6  
  2         13  
12             App::RecordStream::OptionalRequire::require_done();
13              
14             sub init {
15 0     0     my $this = shift;
16 0           my $args = shift;
17              
18 0           my $fast;
19             my $strict;
20 0           my $verbose;
21 0           my $woothee;
22              
23 0           my $spec = {
24             "fast:s" => \$fast,
25             "strict:s" => \$strict,
26             "verbose" => \$verbose,
27             "woothee" => \$woothee,
28             };
29              
30 0           $this->parse_options($args, $spec);
31              
32 0           my %opts;
33              
34 0 0         if (defined $fast) {
35 0 0         if ($fast eq '') {
36 0           $opts{fast} = 1;
37             }
38             else {
39 0           $opts{fast} = eval $fast;
40 0 0         die "eval of option fast failed. $@" if $@;
41             }
42             }
43              
44 0 0         if (defined $strict) {
45 0 0         if ($strict eq '') {
46 0           $opts{strict} = 1;
47             }
48             else {
49 0           $opts{strict} = eval $strict;
50 0 0         die "eval of option strict failed. $@" if $@;
51             }
52             }
53            
54             # default is --fast
55 0 0 0       unless ($opts{fast} or $opts{strict}) {
56 0           $opts{fast} = 1;
57             }
58              
59 0 0         if ($verbose) {
60 0           $opts{verbose} = 1;
61             }
62              
63 0 0         if ($woothee) {
64 0           App::RecordStream::OptionalRequire::optional_use("Woothee");
65 0           App::RecordStream::OptionalRequire::require_done();
66 0           $this->{'WOOTHEE'} = 1;
67             }
68              
69 0           $this->{'PARSER'} = Apache::Log::Parser->new(%opts);
70             }
71              
72             sub accept_line {
73 0     0     my $this = shift;
74 0           my $line = shift;
75              
76 0           my $parser = $this->{'PARSER'};
77              
78 0 0         if (my $hash = $parser->parse($line)) {
79 0           my $record = App::RecordStream::Record->new($hash);
80 0 0         $record->{woothee} = Woothee->parse($record->{agent}) if $this->{'WOOTHEE'};
81 0           $this->push_record($record);
82             }
83              
84 0           return 1;
85             }
86              
87             sub usage {
88 0     0     my $this = shift;
89              
90 0           my $options = [
91             [ 'fast', q{'fast' parser works relatively fast. It can process only 'common', 'combined' and custom styles with compatibility with 'common', and cannot work with backslash-quoted double-quotes in fields. (This is the default)} ],
92             [ 'strict', q{'strict' parser works relatively slow. It can process any style format logs, with specification about separator, and checker for perfection. It can also process backslash-quoted double-quotes properly.} ],
93             [ 'verbose', q{Verbose output.} ],
94             [ 'woothee', q{Each agent field of records is parse by Woothee to produce woothee field.} ],
95             ];
96              
97 0           my $args_string = $this->options_string($options);
98              
99 0           return <
100             Usage: recs-fromapache
101             __FORMAT_TEXT__
102             Each line of input (or lines of ) is parse by Apache::Log::Parser to produce an output record.
103             __FORMAT_TEXT__
104              
105             Arguments:
106             $args_string
107              
108             Examples:
109             Get records from typical apache log
110             recs-fromapache < /var/log/httpd-access.log
111             A more detailed how to use (See perldoc Apache::Log::Parser)
112             recs-fromapache --strict '[qw(combined common vhost_common)]' < /var/log/httpd-access.log
113             Get records except access of crawler
114             recs-fromapache --woothee < /var/log/httpd-access.log | recs-grep '\$r->{woothee}{category} ne "crawler"'
115             USAGE
116             }
117              
118             1;