blib/lib/Bro/Log/Parse.pm

filter: Statementstmt 83.3% 100 / 120 Branchbran 74.0% 40 / 54 Conditioncond 50.0% 12 / 24 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 100% 13 / 13 Podpod 50.0% 2 / 4 totaltotal 74.6% 171 / 229 CC 39 SCAR 25.2
CC
39
Cov
77%
readheader
18.7
getLine
15.5
new
14.5
CRAP 12.4
↑ Summary
1 package Bro::Log::Parse;
2 # ABSTRACT: Perl interface for parsing Bro logfiles
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # use Exporter;
9 8 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Scalar::Util qw/openhandle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 our $VERSION = '0.08';
14  
15 #@EXPORT_OK = qw//;
16  
17 8 my $json = eval {
18 8 require JSON;
19 0 JSON->import();
20 0 1;
21 }; # true if we support reading from json
22  
23 8 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
24 8 my @accessors = qw/fh file line headers headerlines fields/;
25  
26 8 for my $accessor ( @accessors ) {
27 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 8 ▶ *$accessor = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 8 my $self = shift;
30 8 return $self->{$accessor};
31 }
32 48 }
33  
34 }
35  
36 11 ▶sub new {
Subroutine new: called CC=14 SCAR=26.7
Pod documented
37 11 my $class = shift;
38 11 my $arg = shift;
39  
40 11 my $self = {};
41 11 $self->{line} = undef;
42  
43 11 ▶ if ( !defined($arg) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $arg) { } 1 10 11
elsif (ref $arg eq 'HASH') { } 5 5 10
elsif (defined &openhandle($arg)) { } 1 4 5
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 4 $self->{file} = $arg;
51 }
52  
53 11 bless $self, $class;
54  
55 11 ▶ if ( defined($self->{file}) && !(defined($self->{fh})) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"file"} and not defined $self->{"fh"}) 6 5 11
Condition66%2/3
defined $self->{"file"} and not defined $self->{"fh"}
!ll&&!rl&&r
506
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 6 ▶ unless ( -f $self->{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"file"}) 0 6 6
57 0 croak("Could not open ".$self->{file});
58 }
59  
60 open( my $fh, "<", $self->{file} )
61 6 ▶ or croak("Cannot open ".$self->{file});
Branch: 1 branch
BranchTrueFalseTotal
&Bro::Log::Parse::open(my $fh, "<", $self->{"file"}) or croak('Cannot open ' . $self->{"file"}) 6 0 6
62 6 $self->{fh} = $fh;
63 }
64  
65 11 ▶ 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 10 11
Condition83%5/6
defined $self->{"fh"} or defined $self->{"diamond"} and $self->{"diamond"}
l!l&&r!l&&!r
821
defined $self->{"diamond"} and $self->{"diamond"}
!ll&&!rl&&r
102
MC/DC66%2/3
defined $self->{"fh"} or defined $self->{"diamond"} and $self->{"diamond"}
defined $self->{"fh"} defined $self->{"diamond"} $self->{"diamond"}
Truth table75%3/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 10 $self->{json_file} = 0;
70 10 $self->{names} = [ $self->readheader() ];
71 10 $self->{fields} = $self->{names};
72 10 ▶ $self->{empty_as_undef} //= 0;
Condition100%2/2
$self->{"empty_as_undef"} //= 0
l!l
19
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 10 ▶ $self->{headers} //= {};
Condition50%1/2
$self->{"headers"} //= {}
l!l
100
MC/DC0%0/1
$self->{"headers"} //= {}
$self->{"headers"}
Truth table50%1/2
A
A= $self->{"headers"}
Aresult
11
00
75 10 ▶ $self->{headerlines} //= [];
Condition50%1/2
$self->{"headerlines"} //= []
l!l
100
MC/DC0%0/1
$self->{"headerlines"} //= []
$self->{"headerlines"}
Truth table50%1/2
A
A= $self->{"headerlines"}
Aresult
11
00
76  
77 10 return $self;
78 }
79  
80 10 ▶sub readheader {
Subroutine readheader: called CC=11 SCAR=29.3
Pod undocumented
81 10 my $self = shift;
82  
83 10 my @headerlines;
84 my @names;
85 10 my $firstline = 1;
86 # first: read header line. This is a little brittle, but... welll... well, it is.
87 10 ▶ while ( my $line = $self->extractNextLine() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->extractNextLine) 80 0 80
88 80 ▶ if ( $firstline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstline) 10 70 80
89 10 $firstline = 0;
90 10 ▶ if ( length($line) > 1 && substr($line, 0, 1) eq "{" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line > 1 and substr($line, 0, 1) eq "{") 0 10 10
Condition33%1/3
length $line > 1 and substr($line, 0, 1) eq "{"
!ll&&!rl&&r
0100
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 80 chomp($line);
103 80 push(@headerlines, $line);
104  
105 80 my @fields = split /\t/,$line;
106  
107 80 ▶ unless ( $line =~ /^#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^#/) 0 80 80
108 0 croak("Did not find required fields and types header lines: $line");
109 }
110  
111 80 my $type = shift(@fields);
112 80 ▶ if ( "#fields" eq $type ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('#fields' eq $type) { } 10 70 80
elsif ('#types' eq $type) { } 10 60 70
113 # yay.
114 # we have our field names...
115 10 @names = @fields;
116 } elsif ( "#types" eq $type) {
117 10 last;
118 }
119 }
120  
121 10 $self->{headerlines} = \@headerlines;
122 10 $self->{headers} = { map {/#(\w+)\s+(.*)/;$1=>$2} @headerlines };
123  
124 10 return @names;
125 }
126  
127  
128 22 ▶sub getLine {
Subroutine getLine: called CC=13 SCAR=27.4
Pod documented
129 22 my $self = shift;
130  
131 22 my @names = @{$self->{names}};
132  
133 22 ▶ while ( my $line = $self->extractNextLine ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->extractNextLine) 31 4 35
134 31 my $removed = chomp($line);
135 31 $self->{line} = $line;
136  
137 31 ▶ if ( $self->{json_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"json_file"}) 0 31 31
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 31 my @fields = split "\t", $line;
147  
148 31 ▶ if ( $line =~ /^#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#/) 12 19 31
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 19 my %f;
160  
161 19 ▶ unless (scalar @names == scalar @fields) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @names == scalar @fields) 1 18 19
162 1 ▶ next if ( $removed == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $removed == 0 1 0 1
163 0 croak("Number of expected fields does not match number of fields in file");
164 }
165  
166 18 for my $name ( @names ) {
167 360 my $field = shift(@fields);
168 360 ▶ if ( ( $field eq "-" ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field eq '-') { } 90 270 360
elsif ($field eq '(empty)') { } 14 256 270
169 90 $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 256 $f{$name} = $field;
174 }
175 }
176  
177 18 return \%f;
178 }
179 }
180  
181 115 ▶sub extractNextLine {
Subroutine extractNextLine: called CC=3 SCAR=12.3
Pod undocumented
182 115 my $self = shift;
183  
184 115 ▶ if( defined($self->{saved_line}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"saved_line"}) 0 115 115
185 0 my $sl = $self->{saved_line};
186 0 undef $self->{saved_line};
187 0 return $sl;
188 }
189  
190 115 my $in = $self->{fh};
191  
192 115 ▶ return defined($in) ? <$in> : <>;
Branch: 1 branch
BranchTrueFalseTotal
defined $in ? : 94 21 115
193 }
194  
195 8 1;
↑ Summary