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

filter: Statementstmt 79.6% 43 / 54 Branchbran 50.0% 8 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 9 / 11 Podpod 80.0% 4 / 5 totaltotal 71.4% 65 / 91 CC 10 SCAR 21.2
CC
10
Cov
71%
execute
15.5
abstract
2.0
description
2.0
CRAP 8.3
« lib/Log/Munger/App/Command/test_all.pm ↑ Summary lib/Log/Munger/App/Command/munge.pm »
1 package Log::Munger::App::Command::explain;
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;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use YAML::XS qw(Dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
11 return (
12 1 [ 'rules|r=s@', 'Rule file to load (may be given more than once).' ],
13 [ 'string|s=s', 'Item to process. If not given, one is read from stdin.' ],
14 [ 'raw', 'Treat the item as a raw log line (matched as MESSAGE) instead of JSON.' ],
15 [ 'geoip|g=s', 'Path to a MaxMind .mmdb database for geoip enrichment.' ],
16 );
17 }
18  
19 0 ▶sub abstract { "Show which rule/pattern a log item matched and the fields it produced" }
Subroutine abstract: not called CC=1 SCAR=6.9
Pod documented
20  
21 0 ▶sub description {
Subroutine description: not called CC=1 SCAR=6.9
Pod documented
22 0 "Like munge, but reports the match itself rather than only its output: whether anything
23 matched, the name of the rule that fired, which of its patterns did it as a zero-based index,
24 the field that was matched against, and then the extracted fields as a YAML document.
25  
26 This is what to reach for when a rule file is not doing what you expected, since it tells you
27 whether the wrong rule won or the right one matched on the wrong pattern.
28 ";
29 }
30  
31 1 ▶sub validate { return 1 }
Subroutine validate: called CC=1 SCAR=0.0
Pod undocumented
32  
33 1 ▶sub execute {
Subroutine execute: called CC=10 SCAR=27.4
Pod documented
34 1 my ( $self, $opts, $args ) = @_;
35  
36 1 ▶ if ( !defined( $opts->{'rules'} ) || !defined( $opts->{'rules'}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts->{"rules"} and defined $opts->{"rules"}[0]) 0 1 1
Condition33%1/3
defined $opts->{"rules"} and defined $opts->{"rules"}[0]
!ll&&!rl&&r
001
MC/DC0%0/2
defined $opts->{"rules"} and defined $opts->{"rules"}[0]
defined $opts->{"rules"} defined $opts->{"rules"}[0]
Truth table33%1/3
A and B
A= defined $opts->{"rules"}B= defined $opts->{"rules"}[0]
ABresult
0X0
100
111
37 0 die('No rule files specified via -r');
38 }
39  
40 1 my $raw = $opts->{'string'};
41 1 ▶ if ( !defined($raw) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $raw) 0 1 1
42 0 local $/ = undef;
43 0 $raw = <STDIN>;
44 }
45 1 ▶ if ( !defined($raw) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $raw) 0 1 1
46 0 die('No item given via -s or on stdin');
47 }
48  
49 1 my $item;
50 1 ▶ if ( $opts->{'raw'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'raw'}) { } 0 1 1
51 0 chomp($raw);
52 0 $item = $raw;
53 } else {
54 1 eval { $item = decode_json($raw); };
55 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
56 0 die( 'Failed to decode the item as JSON (use --raw for a plain log line)... ' . $@ );
57 }
58 }
59  
60 1 my %margs = ( 'rules' => $opts->{'rules'} );
61 1 ▶ $margs{'geoip'} = $opts->{'geoip'} if ( defined( $opts->{'geoip'} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"geoip"} 0 1 1
62 1 my $munger = Log::Munger->new(%margs);
63  
64 1 my $why = $munger->explain_item( 'item' => $item );
65  
66 1 ▶ if ( !$why->{'matched'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($why->{"matched"}) 0 1 1
67 0 print "matched: no\n";
68 0 return;
69 }
70  
71 1 print 'matched: yes' . "\n";
72 1 ▶ print 'rule: ' . ( defined( $why->{'rule'} ) ? $why->{'rule'} : '(unnamed)' ) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $why->{'rule'} ? : 1 0 1
73 1 print 'pattern: ' . $why->{'pattern'} . "\n";
74 1 print 'field: ' . $why->{'field'} . "\n";
75 1 print "fields:\n";
76 1 print Dump( $why->{'fields'} );
77  
78 1 return;
79 } ## end sub execute
80  
81 16 1;
« lib/Log/Munger/App/Command/test_all.pm ↑ Summary lib/Log/Munger/App/Command/munge.pm »