blib/lib/Bro/Log/Parse.pm

filter: Statementstmt 82.5% 99 / 120 Branchbran 70.3% 38 / 54 Conditioncond 50.0% 12 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 13 / 13 Podpod 50.0% 2 / 4 totaltotal 72.4% 166 / 229 SCAR 41.3
CC
39
Cov
75%
readheader
18.7
getLine
17.6
new
14.5
CRAP 62.1
↑ Summary
1 package Bro::Log::Parse;
2 # ABSTRACT: Perl interface for parsing Bro logfiles
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # use Exporter;
9 7 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Scalar::Util qw/openhandle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 our $VERSION = '0.07';
14  
15 #@EXPORT_OK = qw//;
16  
17 7 my $json = eval {
18 7 require JSON;
19 0 JSON->import();
20 0 1;
21 }; # true if we support reading from json
22  
23 BEGIN {
24 7 ▶ my @accessors = qw/fh file line headers headerlines fields/;
Subroutine BEGIN: called CC=2 SCAR=6.9
25  
26 7 for my $accessor ( @accessors ) {
27 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 *$accessor = sub {
29 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
30 8 return $self->{$accessor};
31 }
32 42 }
33  
34 }
35  
36 sub new {
37 10 ▶ my $class = shift;
Subroutine new: called CC=14 SCAR=26.7
Pod documented
38 10 my $arg = shift;
39  
40 10 my $self = {};
41 10 $self->{line} = undef;
42  
43 10 ▶ if ( !defined($arg) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $arg) { } 1 9 10
elsif (ref $arg eq 'HASH') { } 5 4 9
elsif (defined &openhandle($arg)) { } 1 3 4
44 1 $self->{diamond} = 1;
45 } elsif ( ref($arg) eq 'HASH' ) {
46 5 $self = $arg;
47 } elsif ( defined(openhandle($arg)) ) {
48 1 $self->{fh} = $arg;
49 } else {
50 3 $self->{file} = $arg;
51 }
52  
53 10 bless $self, $class;
54  
55 10 ▶ if ( defined($self->{file}) && !(defined($self->{fh})) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"file"} and not defined $self->{"fh"}) 5 5 10
Condition66%2/3
defined $self->{"file"} and not defined $self->{"fh"}
!ll&&!rl&&r
505
MC/DC50%1/2
defined $self->{"file"} and not defined $self->{"fh"}
defined $self->{"file"} not defined $self->{"fh"}
Truth table66%2/3
A and not(B)
A= defined $self->{"file"}B= not defined $self->{"fh"}
ABresult
0X0
100
111
56 5 ▶ unless ( -f $self->{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"file"}) 0 5 5
57 0 croak("Could not open ".$self->{file});
58 }
59  
60 open( my $fh, "<", $self->{file} )
61 5 ▶ or croak("Cannot open ".$self->{file});
Branch: 1 branch
BranchTrueFalseTotal
&Bro::Log::Parse::open(my $fh, "<", $self->{"file"}) or croak('Cannot open ' . $self->{"file"}) 0 5 5
62 5 $self->{fh} = $fh;
63 }
64  
65 10 ▶ if ( !defined($self->{fh}) && ( !defined($self->{diamond}) || !$self->{diamond} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"} or defined $self->{"diamond"} and $self->{"diamond"}) 1 9 10
Condition83%5/6
defined $self->{"fh"} or defined $self->{"diamond"} and $self->{"diamond"}
l!l&&r!l&&!r
721
defined $self->{"diamond"} and $self->{"diamond"}
!ll&&!rl&&r
102
MC/DC0%0/3
defined $self->{"fh"} or defined $self->{"diamond"} and $self->{"diamond"}
defined $self->{"fh"} defined $self->{"diamond"} $self->{"diamond"}
Truth table50%2/4
A or B and C
A= defined $self->{"fh"}B= defined $self->{"diamond"}C= $self->{"diamond"}
ABCresult
1XX1
0111
00X0
0100
66 1 croak("No filename given in constructor. Aborting");
67 }
68  
69 9 $self->{json_file} = 0;
70 9 $self->{names} = [ $self->readheader() ];
71 9 $self->{fields} = $self->{names};
72 9 ▶ $self->{empty_as_undef} //= 0;
Condition100%2/2
$self->{"empty_as_undef"} //= 0
l!l
18
MC/DC100%1/1
$self->{"empty_as_undef"} //= 0
$self->{"empty_as_undef"}
Truth table100%2/2
A
A= $self->{"empty_as_undef"}
Aresult
11
00
73  
74 9 ▶ $self->{headers} //= {};
Condition50%1/2
$self->{"headers"} //= {}
l!l
90
MC/DC0%0/1
$self->{"headers"} //= {}
$self->{"headers"}
Truth table50%1/2
A
A= $self->{"headers"}
Aresult
11
00
75 9 ▶ $self->{headerlines} //= [];
Condition50%1/2
$self->{"headerlines"} //= []
l!l
90
MC/DC0%0/1
$self->{"headerlines"} //= []
$self->{"headerlines"}
Truth table50%1/2
A
A= $self->{"headerlines"}
Aresult
11
00
76  
77 9 return $self;
78 }
79  
80 sub readheader {
81 9 ▶ my $self = shift;
Subroutine readheader: called CC=11 SCAR=29.3
Pod undocumented
82  
83 9 my @headerlines;
84 my @names;
85 9 my $firstline = 1;
86 # first: read header line. This is a little brittle, but... welll... well, it is.
87 9 ▶ while ( my $line = $self->extractNextLine() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->extractNextLine) 72 0 72
88 72 ▶ if ( $firstline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstline) 9 63 72
89 9 $firstline = 0;
90 9 ▶ if ( length($line) > 1 && substr($line, 0, 1) eq "{" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line > 1 and substr($line, 0, 1) eq "{") 0 9 9
Condition33%1/3
length $line > 1 and substr($line, 0, 1) eq "{"
!ll&&!rl&&r
090
MC/DC0%0/2
length $line > 1 and substr($line, 0, 1) eq "{"
length $line > 1 substr($line, 0, 1) eq "{"
Truth table33%1/3
A and B
A= length $line > 1B= substr($line, 0, 1) eq "{"
ABresult
0X0
100
111
91 # Json file. stuff line in saved_line and try to extract header fields...
92 0 ▶ croak("Parsing json formatted log files needs JSON module") unless ( $json );
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
93 0 my $val = decode_json($line);
94 0 $self->{saved_line} = $line;
95 0 ▶ if ( !defined($val) || ref($val) ne "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val or ref $val ne "HASH") 0 0 0
Condition0%0/3
not defined $val or ref $val ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $val or ref $val ne "HASH"
not defined $val ref $val ne "HASH"
Truth table0%0/3
not(A) or B
A= not defined $valB= ref $val ne "HASH"
ABresult
1X1
011
000
96 0 croak("Error parsing first line of json formatted log - $line");
97 }
98 0 $self->{json_file} = 1;
99 0 return sort keys %$val;
100 }
101 }
102 72 chomp($line);
103 72 push(@headerlines, $line);
104  
105 72 my @fields = split /\t/,$line;
106  
107 72 ▶ unless ( $line =~ /^#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^#/) 0 72 72
108 0 croak("Did not find required fields and types header lines: $line");
109 }
110  
111 72 my $type = shift(@fields);
112 72 ▶ if ( "#fields" eq $type ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('#fields' eq $type) { } 9 63 72
elsif ('#types' eq $type) { } 9 54 63
113 # yay.
114 # we have our field names...
115 9 @names = @fields;
116 } elsif ( "#types" eq $type) {
117 9 last;
118 }
119 }
120  
121 9 $self->{headerlines} = \@headerlines;
122 9 $self->{headers} = { map {/#(\w+)\s+(.*)/;$1=>$2} @headerlines };
123  
124 9 return @names;
125 }
126  
127  
128 sub getLine {
129 19 ▶ my $self = shift;
Subroutine getLine: called CC=13 SCAR=28.7
Pod documented
130  
131 19 my @names = @{$self->{names}};
132  
133 19 ▶ while ( my $line = $self->extractNextLine ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->extractNextLine) 28 3 31
134 28 my $removed = chomp($line);
135 28 $self->{line} = $line;
136  
137 28 ▶ if ( $self->{json_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"json_file"}) 0 28 28
138 0 my $val = decode_json($line);
139 0 ▶ if ( !defined($val) || ref($val) ne "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val or ref $val ne "HASH") 0 0 0
Condition0%0/3
not defined $val or ref $val ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $val or ref $val ne "HASH"
not defined $val ref $val ne "HASH"
Truth table0%0/3
not(A) or B
A= not defined $valB= ref $val ne "HASH"
ABresult
1X1
011
000
140 0 croak("Error parsing line of json formatted log - $line");
141 }
142 0 $self->{names} = [ sort keys %$val ];
143 0 return $val;
144 }
145  
146 28 my @fields = split "\t", $line;
147  
148 28 ▶ if ( $line =~ /^#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#/) 12 16 28
149 12 ▶ if ( "#fields" eq shift(@fields) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("#fields" eq shift @fields) 1 11 12
150 1 @names = @fields;
151 1 $self->{names} = \@fields;
152 # This is not really nice, but for the moment we do not really need any
153 # of the other header lines for parsing files - and we do not keep track
154 # of them. Sorry...
155 1 $self->{headers} = [ join("\t", ("#fields", @fields)) ];
156 }
157 12 next;
158 }
159 16 my %f;
160  
161 16 ▶ unless (scalar @names == scalar @fields) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @names == scalar @fields) 0 16 16
162 0 ▶ next if ( $removed == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $removed == 0 0 0 0
163 0 croak("Number of expected fields does not match number of fields in file");
164 }
165  
166 16 for my $name ( @names ) {
167 320 my $field = shift(@fields);
168 320 ▶ if ( ( $field eq "-" ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field eq '-') { } 80 240 320
elsif ($field eq '(empty)') { } 14 226 240
169 80 $f{$name} = undef;
170 } elsif ( $field eq "(empty)" ) {
171 14 ▶ $f{$name} = $self->{empty_as_undef} ? undef : [];
Branch: 1 branch
BranchTrueFalseTotal
$self->{'empty_as_undef'} ? : 1 13 14
172 } else {
173 226 $f{$name} = $field;
174 }
175 }
176  
177 16 return \%f;
178 }
179 }
180  
181 sub extractNextLine {
182 103 ▶ my $self = shift;
Subroutine extractNextLine: called CC=3 SCAR=12.3
Pod undocumented
183  
184 103 ▶ if( defined($self->{saved_line}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"saved_line"}) 0 103 103
185 0 my $sl = $self->{saved_line};
186 0 undef $self->{saved_line};
187 0 return $sl;
188 }
189  
190 103 my $in = $self->{fh};
191  
192 103 ▶ return defined($in) ? <$in> : <>;
Branch: 1 branch
BranchTrueFalseTotal
defined $in ? : 82 21 103
193 }
194  
195 7 1;
↑ Summary