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

filter: Statementstmt 79.1% 38 / 48 Branchbran 50.0% 7 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 9 / 11 Podpod 80.0% 4 / 5 totaltotal 71.0% 59 / 83 CC 9 SCAR 20.1
CC
9
Cov
71%
execute
14.4
abstract
2.0
description
2.0
CRAP 7.4
« lib/Log/Munger/App/Command/explain.pm ↑ Summary lib/Log/Munger/RulesUsable.pm »
1 package Log::Munger::App::Command::munge;
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 { "Run one log item through the rules and dump the extracted fields" }
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 "Reads a single item from -s, or from stdin if -s is not given, runs it through the rules, and
23 prints the winning rule's captured fields as YAML. If nothing matched it prints '--- ~', a YAML
24 null.
25  
26 --raw only sets MESSAGE, so a rule gating on another field, as sshd gates on PROGRAM, will not
27 match a raw line. Feed those a JSON record that includes the gate field. --raw is for gateless
28 whole-line rules such as http_access_logs.
29 ";
30 } ## end sub description
31  
32 1 ▶sub validate { return 1 }
Subroutine validate: called CC=1 SCAR=0.0
Pod undocumented
33  
34 1 ▶sub execute {
Subroutine execute: called CC=9 SCAR=26.7
Pod documented
35 1 my ( $self, $opts, $args ) = @_;
36  
37 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
38 0 die('No rule files specified via -r');
39 }
40  
41 1 my $raw = $opts->{'string'};
42 1 ▶ if ( !defined($raw) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $raw) 0 1 1
43 0 local $/ = undef;
44 0 $raw = <STDIN>;
45 }
46 1 ▶ if ( !defined($raw) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $raw) 0 1 1
47 0 die('No item given via -s or on stdin');
48 }
49  
50 1 my $item;
51 1 ▶ if ( $opts->{'raw'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'raw'}) { } 0 1 1
52 0 chomp($raw);
53 0 $item = $raw;
54 } else {
55 1 eval { $item = decode_json($raw); };
56 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
57 0 die( 'Failed to decode the item as JSON (use --raw for a plain log line)... ' . $@ );
58 }
59 }
60  
61 1 my %args = ( 'rules' => $opts->{'rules'} );
62 1 ▶ $args{'geoip'} = $opts->{'geoip'} if ( defined( $opts->{'geoip'} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"geoip"} 0 1 1
63 1 my $munger = Log::Munger->new(%args);
64  
65 1 my $fields = $munger->process_item( 'item' => $item );
66  
67 1 ▶ if ( defined($fields) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fields) { } 1 0 1
68 1 print Dump($fields);
69 } else {
70 0 print "--- ~\n"; # YAML null -- nothing matched
71 }
72  
73 1 return;
74 } ## end sub execute
75  
76 16 1;
« lib/Log/Munger/App/Command/explain.pm ↑ Summary lib/Log/Munger/RulesUsable.pm »