| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Log::Saftpresse; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 971 | use Moose; | 
|  | 1 |  |  |  |  | 291632 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | # ABSTRACT: a modular logfile analyzer | 
| 6 |  |  |  |  |  |  | our $VERSION = '1.4'; # VERSION | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 4719 | use Log::Saftpresse::Log4perl; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 95 |  | 
| 9 | 1 |  |  | 1 |  | 305 | use Log::Saftpresse::Config; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 |  | 333 | use Log::Saftpresse::Analyzer; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 12 | 1 |  |  | 1 |  | 338 | use Log::Saftpresse::Slurp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 13 | 1 |  |  | 1 |  | 325 | use Log::Saftpresse::CounterOutputs; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 14 | 1 |  |  | 1 |  | 272 | use Log::Saftpresse::Outputs; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 387 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | has 'config' => ( | 
| 18 |  |  |  |  |  |  | is => 'ro', isa => 'Log::Saftpresse::Config', lazy => 1, | 
| 19 |  |  |  |  |  |  | default => sub { Log::Saftpresse::Config->new }, | 
| 20 |  |  |  |  |  |  | handles => [ 'load_config' ], | 
| 21 |  |  |  |  |  |  | ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | has 'slurp' => ( | 
| 24 |  |  |  |  |  |  | is => 'ro', isa => 'Log::Saftpresse::Slurp', lazy => 1, | 
| 25 |  |  |  |  |  |  | default => sub { Log::Saftpresse::Slurp->new }, | 
| 26 |  |  |  |  |  |  | ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | has 'analyzer' => ( | 
| 29 |  |  |  |  |  |  | is => 'ro', isa => 'Log::Saftpresse::Analyzer', lazy => 1, | 
| 30 |  |  |  |  |  |  | default => sub { Log::Saftpresse::Analyzer->new }, | 
| 31 |  |  |  |  |  |  | ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | has 'counter_outputs' => ( | 
| 34 |  |  |  |  |  |  | is => 'ro', isa => 'Log::Saftpresse::CounterOutputs', lazy => 1, | 
| 35 |  |  |  |  |  |  | default => sub { Log::Saftpresse::CounterOutputs->new }, | 
| 36 |  |  |  |  |  |  | ); | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | has 'outputs' => ( | 
| 39 |  |  |  |  |  |  | is => 'ro', isa => 'Log::Saftpresse::Outputs', lazy => 1, | 
| 40 |  |  |  |  |  |  | default => sub { Log::Saftpresse::Outputs->new }, | 
| 41 |  |  |  |  |  |  | ); | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | has 'flush_interval' => ( is => 'rw', isa => 'Maybe[Int]' ); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | has '_last_flush_counters' => ( | 
| 46 |  |  |  |  |  |  | is => 'rw', isa => 'Int', | 
| 47 |  |  |  |  |  |  | default => sub { time }, | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub init { | 
| 52 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 53 | 0 |  |  |  |  |  | my $config = $self->config; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  |  | Log::Saftpresse::Log4perl->init( | 
| 56 |  |  |  |  |  |  | $config->get('logging', 'level'), | 
| 57 |  |  |  |  |  |  | $config->get('logging', 'file'), | 
| 58 |  |  |  |  |  |  | ); | 
| 59 | 0 |  |  |  |  |  | $self->flush_interval( $config->get('counters', 'flush_interval') ); | 
| 60 | 0 |  |  |  |  |  | $self->slurp->load_config( $config->get_node('Input') ); | 
| 61 | 0 |  |  |  |  |  | $self->analyzer->load_config( $config->get_node('Plugin') ); | 
| 62 | 0 |  |  |  |  |  | $self->counter_outputs->load_config( $config->get_node('CounterOutput') ); | 
| 63 | 0 |  |  |  |  |  | $self->outputs->load_config( $config->get_node('Output') ); | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 0 |  |  |  |  |  | return; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub _need_flush_counters { | 
| 69 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 0 | 0 | 0 |  |  |  | if( ! defined $self->flush_interval | 
| 72 |  |  |  |  |  |  | || $self->flush_interval < 1 ) { | 
| 73 | 0 |  |  |  |  |  | return 0; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 |  |  |  |  |  | my $next_flush = $self->_last_flush_counters + $self->flush_interval; | 
| 77 | 0 | 0 |  |  |  |  | if( time < $next_flush ) { | 
| 78 | 0 |  |  |  |  |  | return 0; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 |  |  |  |  |  | return 1; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub _flushed_counters { | 
| 85 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 86 | 0 |  |  |  |  |  | $self->_last_flush_counters( time ); | 
| 87 | 0 |  |  |  |  |  | return; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub run { | 
| 92 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 93 | 0 |  |  |  |  |  | my $slurp = $self->slurp; | 
| 94 | 0 |  |  |  |  |  | my $last_flush = time; | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 0 |  |  |  |  |  | $log->info('entering main loop'); | 
| 97 | 0 |  |  |  |  |  | for(;;) { # main loop | 
| 98 | 0 |  |  |  |  |  | my $events; | 
| 99 | 0 | 0 |  |  |  |  | if( $slurp->can_read(1) ) { | 
| 100 | 0 |  |  |  |  |  | $log->debug('checking for new input...'); | 
| 101 | 0 |  |  |  |  |  | $events = $slurp->read_events; | 
| 102 | 0 |  |  |  |  |  | foreach my $event ( @$events ) { | 
| 103 | 0 |  |  |  |  |  | $self->analyzer->process_event( $event ); | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  | } | 
| 106 | 0 | 0 |  |  |  |  | if( scalar @$events ) { | 
| 107 | 0 |  |  |  |  |  | $log->debug('sending '.scalar(@$events).' events to outputs...'); | 
| 108 | 0 |  |  |  |  |  | $self->outputs->output( @$events ); | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 0 | 0 |  |  |  |  | if( $self->_need_flush_counters ){ | 
| 112 | 0 |  |  |  |  |  | $log->debug('flushing counters...'); | 
| 113 | 0 |  |  |  |  |  | $self->counter_outputs->output( | 
| 114 |  |  |  |  |  |  | $self->analyzer->get_all_counters ); | 
| 115 | 0 |  |  |  |  |  | $self->_flushed_counters; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 0 |  |  |  |  |  | return; | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | 1; | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | __END__ | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =pod | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | =encoding UTF-8 | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =head1 NAME | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | Log::Saftpresse - a modular logfile analyzer | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head1 VERSION | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | version 1.4 | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =head1 Description | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | This is the central class of the saftpresse log analyzer. | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =head1 Synopsis | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | use Log::Saftpresse; | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | my $saft = Log:::Saftpresse->new; | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | $saft->load_config( $path ); | 
| 150 |  |  |  |  |  |  | $saft->init; | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | # start main loop | 
| 153 |  |  |  |  |  |  | $saft->run; | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =head1 Attributes | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =head2 config( L<Log::Saftpresse::Config>) | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | Holds the configuration. | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =head2 slurp( L<Log::Saftpresse::Slurp> ) | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | Holds the slurp class implementing the input. | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | =head2 analyzer( L<Log::Saftpresse::Analyzer> ) | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | Holds the analyzer object which controls the processing plugins. | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | =head2 counter_outputs( L<Log::Saftpresse::CounterOutputs> ) | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | Holds the counter output object which controls output of metrics. | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =head2 outputs( L<Log::Saftpresse::Outputs> ) | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | Holds the Outputs plugin which controls the event output. | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =head2 flush_interval( $seconds ) | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | How often to flush metrics to CounterOutputs. | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | =head1 Methods | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | =head2 init | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | Initialize saftpresse as configured in config file. | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | Will load slurp, analyzer, counter_outputs, outputs and flush_interval | 
| 188 |  |  |  |  |  |  | from configuration. | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | =head2 run | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | Run the main loop of saftpresse. | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =head1 See also | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | =over | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | =item L<Log::Saftpresse::App> | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | Commandline glue for this class. | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | =item bin/saftpresse | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | Commandline interface of saftpresse with end-user docs. | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | =back | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =head1 AUTHOR | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | Markus Benning <ich@markusbenning.de> | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning. | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | This is free software, licensed under: | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | The GNU General Public License, Version 2, June 1991 | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | =cut |