| 1 | package Log::Munger::App::Command::enrich; | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | return ( | ||||||||||||||||||||||||||||
| 11 | 1 | [ 'rules|r=s@', 'Rule file to load (may be given more than once).' ], | |||||||||||||||||||||||||||
| 12 | [ 'raw', 'Each input line is a raw log line (matched as MESSAGE) instead of NDJSON.' ], | ||||||||||||||||||||||||||||
| 13 | [ 'geoip|g=s', 'Path to a MaxMind .mmdb database for geoip enrichment.' ], | ||||||||||||||||||||||||||||
| 14 | [ 'into|i=s', 'Key to nest the enrichment under. Default: enriched.', { default => 'enriched' } ], | ||||||||||||||||||||||||||||
| 15 | [ 'flat', 'Merge the extracted fields into the record instead of nesting them.' ], | ||||||||||||||||||||||||||||
| 16 | [ 'drop-unmatched', 'Do not emit records that did not match any rule.' ], | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | } ## end sub opt_spec | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub abstract { "Stream log lines from stdin and emit enriched NDJSON on stdout" } | ||||||||||||||||||||||||||
|
Subroutine
abstract: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub description { | ||||||||||||||||||||||||||
|
Subroutine
description: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | "Reads one record per line from stdin, NDJSON or raw log lines with --raw, runs each through | |||||||||||||||||||||||||||
| 24 | the rules, and emits NDJSON. The extracted fields are nested under a key, 'enriched' unless | ||||||||||||||||||||||||||||
| 25 | --into says otherwise, or merged into the record itself with --flat. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Records that matched nothing pass through unchanged unless --drop-unmatched is given. A line | ||||||||||||||||||||||||||||
| 28 | that will not decode as JSON is warned about and passed through, so nothing is ever dropped | ||||||||||||||||||||||||||||
| 29 | silently. | ||||||||||||||||||||||||||||
| 30 | "; | ||||||||||||||||||||||||||||
| 31 | } ## end sub description | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | sub validate { return 1 } | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=14 SCAR=35.3
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 1 | my ( $self, $opts, $args ) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | if ( !defined( $opts->{'rules'} ) || !defined( $opts->{'rules'}[0] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opts->{"rules"} and defined $opts->{"rules"}[0]
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]
| |||||||||||||||||||||||||||||
| 39 | 0 | die('No rule files specified via -r'); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | my %margs = ( 'rules' => $opts->{'rules'} ); | |||||||||||||||||||||||||||
| 43 | 1 | ▶ | $margs{'geoip'} = $opts->{'geoip'} if ( defined( $opts->{'geoip'} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 1 | my $munger = Log::Munger->new(%margs); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | my $json = JSON->new->utf8->canonical; | |||||||||||||||||||||||||||
| 47 | 1 | my $into = $opts->{'into'}; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | while ( defined( my $line = <STDIN> ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 1 | chomp($line); | |||||||||||||||||||||||||||
| 51 | 1 | ▶ | next if ( $line eq '' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # build the base record + the item handed to the munger | ||||||||||||||||||||||||||||
| 54 | 1 | my ( $record, $item ); | |||||||||||||||||||||||||||
| 55 | 1 | ▶ | if ( $opts->{'raw'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | $record = { 'MESSAGE' => $line }; | |||||||||||||||||||||||||||
| 57 | 0 | $item = $line; | |||||||||||||||||||||||||||
| 58 | } else { | ||||||||||||||||||||||||||||
| 59 | 1 | eval { $record = $json->decode($line); }; | |||||||||||||||||||||||||||
| 60 | 1 | ▶ | if ( $@ || ref($record) ne 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or ref $record ne "HASH"
MC/DC0%0/2
$@ or ref $record ne "HASH"
$@
ref $record ne "HASH"
Truth table33%1/3
A or B
A= $@B= ref $record ne "HASH"
| |||||||||||||||||||||||||||||
| 61 | # not decodable JSON: never drop data -- pass the line through. | ||||||||||||||||||||||||||||
| 62 | # The warned copy has control characters stripped so a hostile | ||||||||||||||||||||||||||||
| 63 | # line cannot drive escape sequences into a watching terminal | ||||||||||||||||||||||||||||
| 64 | 0 | my $shown = $line; | |||||||||||||||||||||||||||
| 65 | 0 | $shown =~ s/[\x00-\x1f\x7f]/./g; | |||||||||||||||||||||||||||
| 66 | 0 | warn("skipping undecodable line: $shown\n"); | |||||||||||||||||||||||||||
| 67 | 0 | print "$line\n"; | |||||||||||||||||||||||||||
| 68 | 0 | next; | |||||||||||||||||||||||||||
| 69 | } ## end if ( $@ || ref($record) ne 'HASH' ) | ||||||||||||||||||||||||||||
| 70 | 1 | $item = $record; | |||||||||||||||||||||||||||
| 71 | } ## end else [ if ( $opts->{'raw'} ) ] | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | my $fields = $munger->process_item( 'item' => $item ); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | if ( !defined($fields) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | next if ( $opts->{'drop_unmatched'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | print $json->encode($record) . "\n"; | |||||||||||||||||||||||||||
| 78 | 0 | next; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | if ( $opts->{'flat'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | foreach my $key ( keys( %{$fields} ) ) { | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | $record->{$key} = $fields->{$key} unless ( exists( $record->{$key} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||||||||||
| 86 | 1 | $record->{$into} = $fields; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | print $json->encode($record) . "\n"; | |||||||||||||||||||||||||||
| 90 | } ## end while ( defined( my $line = <STDIN> ) ) | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | return; | |||||||||||||||||||||||||||
| 93 | } ## end sub execute | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 16 | 1; | |||||||||||||||||||||||||||