| 1 | package Transform::Alert; | ||||||||||||
| 2 | |||||||||||||
| 3 | 5 | our $VERSION = '1.00'; # VERSION | |||||||||||
| 4 | # ABSTRACT: Transform alerts from one type to another type | ||||||||||||
| 5 | |||||||||||||
| 6 | 5 | ▶ | use sanity 0.94; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 5 | ▶ | use Moo 1.000000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 5 | ▶ | use MooX::Types::MooseLike 0.15; # ::Base got no $VERSION | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 5 | ▶ | use MooX::Types::MooseLike::Base qw(Str HashRef ScalarRef ArrayRef InstanceOf ConsumerOf); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | #with 'MooX::Singleton'; | ||||||||||||
| 12 | |||||||||||||
| 13 | 5 | ▶ | use Transform::Alert::InputGrp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 5 | ▶ | use Time::HiRes 'time'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 5 | ▶ | use List::AllUtils 'min'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 5 | ▶ | use File::Slurp 'read_file'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 5 | ▶ | use Storable 'dclone'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 5 | ▶ | use Class::Load 0.17 ('load_class'); # 0.17 = wheezy's version | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | 5 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | |||||||||||||
| 23 | 5 | has config => ( | |||||||||||
| 24 | is => 'ro', | ||||||||||||
| 25 | isa => HashRef, | ||||||||||||
| 26 | required => 1, | ||||||||||||
| 27 | ); | ||||||||||||
| 28 | 5 | has log => ( | |||||||||||
| 29 | is => 'ro', | ||||||||||||
| 30 | isa => InstanceOf['Log::Log4perl::Logger'], | ||||||||||||
| 31 | required => 1, | ||||||||||||
| 32 | ); | ||||||||||||
| 33 | |||||||||||||
| 34 | # added in via config and BUILDARGS | ||||||||||||
| 35 | has basedir => ( | ||||||||||||
| 36 | is => 'ro', | ||||||||||||
| 37 | isa => Str, | ||||||||||||
| 38 | 0 | ▶ | default => sub { '' }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 39 | 5 | ); | |||||||||||
| 40 | 5 | has inputs => ( | |||||||||||
| 41 | is => 'ro', | ||||||||||||
| 42 | isa => HashRef[InstanceOf['Transform::Alert::InputGrp']], | ||||||||||||
| 43 | required => 1, | ||||||||||||
| 44 | ); | ||||||||||||
| 45 | 5 | has outputs => ( | |||||||||||
| 46 | is => 'ro', | ||||||||||||
| 47 | isa => HashRef[ConsumerOf['Transform::Alert::Output']], | ||||||||||||
| 48 | required => 1, | ||||||||||||
| 49 | ); | ||||||||||||
| 50 | |||||||||||||
| 51 | # Punk to funk (recursively) | ||||||||||||
| 52 | 0 | ▶ | around BUILDARGS => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||
| 53 | 0 | my ($orig, $self) = (shift, shift); | |||||||||||
| 54 | 0 | my $hash = shift; | |||||||||||
| 55 | 0 | ▶ | $hash = { $hash, @_ } unless ref $hash; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | |||||||||||||
| 57 | 0 | my $conf = $hash->{config}; | |||||||||||
| 58 | 0 | my $basedir = $hash->{basedir} = $conf->{basedir}; | |||||||||||
| 59 | |||||||||||||
| 60 | 0 | chdir $basedir; # just go there to make relative pathing easier | |||||||||||
| 61 | |||||||||||||
| 62 | # process outputs first (needed for Template sets) | ||||||||||||
| 63 | 0 | $hash->{outputs} = {}; | |||||||||||
| 64 | 0 | foreach my $out_key (keys %{ $conf->{output} }) { | |||||||||||
| 65 | 0 | my $out_conf = dclone $conf->{output}{$out_key}; | |||||||||||
| 66 | |||||||||||||
| 67 | # create the new class | ||||||||||||
| 68 | 0 | ▶ | my $type = delete $out_conf->{type} || die "Output '$out_key' requires a Type!"; | ||||||||||
|
Condition0%0/2
delete $$out_conf{'type'} || die("Output '${out_key}' requires a Type!")
MC/DC0%0/1
delete $$out_conf{'type'} || die("Output '${out_key}' requires a Type!")
delete $$out_conf{'type'}
Truth table0%0/2
A
A= delete $$out_conf{'type'}
| |||||||||||||
| 69 | 0 | my $class = "Transform::Alert::Output::$type"; | |||||||||||
| 70 | 0 | load_class $class; | |||||||||||
| 71 | 0 | $hash->{outputs}{$out_key} = $class->new($out_conf); | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | # now process inputs | ||||||||||||
| 75 | 0 | $hash->{inputs} = {}; | |||||||||||
| 76 | 0 | foreach my $in_key (keys %{ $conf->{input} }) { | |||||||||||
| 77 | 0 | my $in_conf = dclone $conf->{input}{$in_key}; | |||||||||||
| 78 | |||||||||||||
| 79 | 0 | $in_conf->{name} = $in_key; | |||||||||||
| 80 | 0 | $in_conf->{output_objs} = $hash->{outputs}; | |||||||||||
| 81 | |||||||||||||
| 82 | # create the input group | ||||||||||||
| 83 | 0 | $hash->{inputs}{$in_key} = Transform::Alert::InputGrp->new($in_conf); | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 0 | $orig->($self, $hash); | |||||||||||
| 87 | 5 | }; | |||||||||||
| 88 | |||||||||||||
| 89 | # Tie new $self to inputs/outputs | ||||||||||||
| 90 | 0 | ▶ | sub BUILD { | ||||||||||
|
Subroutine
BUILD: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 91 | 0 | my $self = shift; | |||||||||||
| 92 | 0 | $_->_set_daemon($self) for (values %{ $self->inputs }, values %{ $self->outputs }); | |||||||||||
| 93 | }; | ||||||||||||
| 94 | |||||||||||||
| 95 | 0 | ▶ | sub heartbeat { | ||||||||||
|
Subroutine
heartbeat: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 96 | 0 | my $self = shift; | |||||||||||
| 97 | 0 | my $log = $self->log; | |||||||||||
| 98 | |||||||||||||
| 99 | 0 | $log->debug('START Heartbeat'); | |||||||||||
| 100 | 0 | foreach my $in_key (sort { | |||||||||||
| 101 | # sorting these by time_left, so that (hopefully) as much as possible is processed in one heartbeat | ||||||||||||
| 102 | 0 | $self->inputs->{$a}->time_left <=> $self->inputs->{$b}->time_left | |||||||||||
| 103 | 0 | } keys %{ $self->inputs }) { | |||||||||||
| 104 | 0 | $log->debug('Looking at Input "'.$in_key.'"...'); | |||||||||||
| 105 | 0 | my $in = $self->inputs->{$in_key}; | |||||||||||
| 106 | |||||||||||||
| 107 | # are we ready for another run? | ||||||||||||
| 108 | 0 | ▶ | if (time > $in->last_finished + $in->interval) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 109 | 0 | $in->process; | |||||||||||
| 110 | } | ||||||||||||
| 111 | } | ||||||||||||
| 112 | 0 | $log->debug('END Heartbeat'); | |||||||||||
| 113 | |||||||||||||
| 114 | # shut up until I'm ready... | ||||||||||||
| 115 | 0 | return min map { $_->time_left } values %{ $self->inputs }; | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | 0 | ▶ | sub close_all { | ||||||||||
|
Subroutine
close_all: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||
| 120 | 0 | my $log = $self->log; | |||||||||||
| 121 | 0 | $log->debug('Closing all I/O for ALL groups'); | |||||||||||
| 122 | |||||||||||||
| 123 | 0 | $_->close_all for (values %{ $self->inputs }); | |||||||||||
| 124 | |||||||||||||
| 125 | 0 | return 1; | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | 5 | 42; | |||||||||||
| 129 | |||||||||||||
| 130 | __END__ | ||||||||||||