lib/Log/Munger/App/Command/list_fields.pm

filter: Statementstmt 75.8% 47 / 62 Branchbran 35.0% 14 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 8 / 10 Podpod 80.0% 4 / 5 totaltotal 62.3% 73 / 117 CC 28 SCAR 44.3
CC
28
Cov
60%
execute
114.7
abstract
2.0
description
2.0
CRAP 84.1
↑ Summary lib/Log/Munger/RulesTest.pm »
1 package Log::Munger::App::Command::list_fields;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Log::Munger::App -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Log::Munger::RuleFileParser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
9 1 return ( [ 'f=s', 'Rule file to inspect.' ], );
10 }
11  
12 0 ▶sub abstract { "List the fields a rule file can produce (a schema hint)" }
Subroutine abstract: not called CC=1 SCAR=6.9
Pod documented
13  
14 0 ▶sub description {
Subroutine description: not called CC=1 SCAR=6.9
Pod documented
15 0 "Reads a rule file and lists what it can emit without running anything against it: the
16 named-capture fields of its patterns, the fields its pattern decomposes produce, the prefixes
17 its kv decomposes produce keys under, and the fields it geoips.
18  
19 The kv prefixes are listed separately because those keys are whatever the log line happened to
20 contain and are not knowable until something is matched.
21 ";
22 }
23  
24 1 ▶sub validate { return 1 }
Subroutine validate: called CC=1 SCAR=0.0
Pod undocumented
25  
26 # Collects the named capture names out of a resolved regexp string.
27 #
28 # This is how the field list is worked out without running anything: a pattern's
29 # (?<name>...) groups are the fields it emits, so reading them straight off the
30 # resolved regexp gives the schema. It only finds what is written down, which is
31 # why kv decompose keys are reported separately as a prefix rather than as
32 # individual fields. Those are whatever the log line happened to contain and are
33 # not knowable until something is matched.
34 #
35 # The name pattern is [A-Za-z_]\w*, which is what Perl accepts for a capture name.
36 # That also means (?<= and (?<! are skipped, since neither can look like a name.
37 #
38 # Args:
39 #
40 # - $string :: The resolved regexp string to read, after templating rather
41 # than the vars_templated source.
42 #
43 # - $set :: Hash ref used as a set. Every name found is added as a key with a
44 # value of 1. Modified in place, so it accumulates across calls, which is
45 # how the fields of every pattern in a file end up in one collection.
46 #
47 # Returns nothing. The names come back through $set.
48 #
49 # my %fields;
50 # _captures( $rules->{'vars'}{'SSH_FAILED'}, \%fields );
51 # # $fields{ssh_user} = 1, $fields{ssh_src_ip} = 1, ...
52 29 ▶sub _captures {
Subroutine _captures: called CC=2 SCAR=6.9
53 29 my ( $string, $set ) = @_;
54 29 ▶ while ( $string =~ /\(\?<([A-Za-z_]\w*)>/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\(\?<([A-Za-z_]\w*)>/g) 102 29 131
55 102 $set->{$1} = 1;
56 }
57 29 return;
58 }
59  
60 1 ▶sub execute {
Subroutine execute: called CC=27 SCAR=47.4
Pod documented
61 1 my ( $self, $opts, $args ) = @_;
62  
63 1 ▶ if ( !defined( $opts->{'f'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts->{"f"}) 0 1 1
64 0 die('No rule file specified via -f');
65 }
66  
67 1 my $rules = Log::Munger::RuleFileParser->new->load( 'file' => $opts->{'f'} );
68 1 ▶ my $vars = ( ref( $rules->{'vars'} ) eq 'HASH' ) ? $rules->{'vars'} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $rules->{'vars'} eq 'HASH' ? : 1 0 1
69  
70 1 my %fields; # static capture names
71 my %kv; # prefix => source field (dynamic)
72 1 my %geoip; # field => 1
73  
74 1 ▶ my @rulelist = ( ref( $rules->{'rules'} ) eq 'ARRAY' ) ? @{ $rules->{'rules'} } : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $rules->{'rules'} eq 'ARRAY' ? : 1 0 1
75 1 foreach my $rule (@rulelist) {
76 1 ▶ next if ( ref($rule) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $rule ne "HASH" 0 1 1
77  
78 # pattern captures
79 1 foreach my $pattern ( @{ $rule->{'patterns'} } ) {
80 29 ▶ next if ( ref($pattern) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $pattern ne "" 0 29 29
81 29 ▶ my $string = exists( $vars->{$pattern} ) ? $vars->{$pattern} : $pattern;
Branch: 1 branch
BranchTrueFalseTotal
exists $vars->{$pattern} ? : 29 0 29
82 29 _captures( $string, \%fields );
83 }
84  
85 # decompose (rule-level, else file-level default)
86 1 ▶ my $decompose = defined( $rule->{'decompose'} ) ? $rule->{'decompose'} : $rules->{'decompose'};
Branch: 1 branch
BranchTrueFalseTotal
defined $rule->{'decompose'} ? : 0 1 1
87 1 ▶ if ( ref($decompose) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $decompose eq "ARRAY") 0 1 1
88 0 foreach my $d ( @{$decompose} ) {
89 0 ▶ next if ( ref($d) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $d ne "HASH" 0 0 0
90 0 ▶ my $type = defined( $d->{'type'} ) ? $d->{'type'} : 'kv';
Branch: 1 branch
BranchTrueFalseTotal
defined $d->{'type'} ? : 0 0 0
91 0 ▶ if ( $type eq 'kv' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'kv') { } 0 0 0
elsif ($type eq 'pattern') { } 0 0 0
92 0 ▶ my $prefix = defined( $d->{'prefix'} ) ? $d->{'prefix'} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $d->{'prefix'} ? : 0 0 0
93 0 $kv{$prefix} = $d->{'field'};
94 } elsif ( $type eq 'pattern' ) {
95 0 ▶ my $string = exists( $vars->{ $d->{'pattern'} } ) ? $vars->{ $d->{'pattern'} } : $d->{'pattern'};
Branch: 1 branch
BranchTrueFalseTotal
exists $$vars{$d->{'pattern'}} ? : 0 0 0
96 0 ▶ _captures( $string, \%fields ) if ( defined($string) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $string 0 0 0
97 }
98 } ## end foreach my $d ( @{$decompose} )
99 } ## end if ( ref($decompose) eq 'ARRAY' )
100  
101 # geoip (rule-level, else file-level default)
102 1 ▶ my $g = defined( $rule->{'geoip'} ) ? $rule->{'geoip'} : $rules->{'geoip'};
Branch: 1 branch
BranchTrueFalseTotal
defined $rule->{'geoip'} ? : 0 1 1
103 1 ▶ if ( ref($g) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $g eq "ARRAY") 1 0 1
104 1 $geoip{$_} = 1 for ( @{$g} );
105 }
106 } ## end foreach my $rule (@rulelist)
107  
108 1 print "fields:\n";
109 1 print " $_\n" for ( sort keys %fields );
110  
111 1 ▶ if (%kv) {
Branch: 1 branch
BranchTrueFalseTotal
if (%kv) 0 1 1
112 0 print "\nkv-decompose (dynamic keys):\n";
113 0 foreach my $prefix ( sort keys %kv ) {
114 0 print " ${prefix}* (from " . $kv{$prefix} . ")\n";
115 }
116 }
117  
118 1 ▶ if (%geoip) {
Branch: 1 branch
BranchTrueFalseTotal
if (%geoip) 1 0 1
119 1 print "\ngeoip:\n";
120 1 print " .geoip.$_\n" for ( sort keys %geoip );
121 }
122  
123 1 return;
124 } ## end sub execute
125  
126 16 1;
↑ Summary lib/Log/Munger/RulesTest.pm »