| 1 | #!/usr/local/bin/perl | ||
| 2 | # PODNAME: eris-es-indexer.pl | ||
| 3 | # ABSTRACT: Sample implementation using the eris toolkit to index data to elasticsearch | ||
| 4 | ## no critic (RequireEndWithOne) | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Getopt::Long::Descriptive; | ||
| 9 | use Path::Tiny; | ||
| 10 | sub POE::Kernel::ASSERT_DEFAULT { 1 } | ||
| 11 | use POE qw( | ||
| 12 | Component::ElasticSearch::Indexer | ||
| 13 | Wheel::ReadWrite | ||
| 14 | Filter::Line | ||
| 15 | Filter::Reference | ||
| 16 | ); | ||
| 17 | use YAML qw(); | ||
| 18 | |||
| 19 | # Load the eris libraries | ||
| 20 | use eris::log::contextualizer; | ||
| 21 | use eris::schemas; | ||
| 22 | |||
| 23 | # Options | ||
| 24 | my ($opt,$usage) = describe_options('%c - %o', | ||
| 25 | [ 'config:s', 'Config file, required.', { callbacks => { "Must be a readable file" => sub { -r $_[0] } } } ], | ||
| 26 | [ 'stats-interval:i', 'Interval in seconds to send statistics, default: 60', { default => 60 }], | ||
| 27 | [ 'flush-interval|F:i', 'Interval in seconds to flush the queue to thebulk handler, default: 10', { default => 10 } ], | ||
| 28 | [ 'flush-size|S:i', 'Override the default FlushSize from POE::Component::ElasticSearch::Indexer' ], | ||
| 29 | [], | ||
| 30 | [ 'help', 'Display this help' ], | ||
| 31 | ); | ||
| 32 | if( $opt->help ) { | ||
| 33 | print $usage->text; | ||
| 34 | exit 0; | ||
| 35 | } | ||
| 36 | |||
| 37 | # Load the Configuration | ||
| 38 | my $config = {}; | ||
| 39 | if( $opt->config ) { | ||
| 40 | eval { | ||
| 41 | $config = YAML::LoadFile( $opt->config ); | ||
| 42 | 1; | ||
| 43 | } or do { | ||
| 44 | my $err = $@; | ||
| 45 | die sprintf "Failed loading your requested config %s: %s", | ||
| 46 | $opt->config, $@; | ||
| 47 | }; | ||
| 48 | } | ||
| 49 | # Instantiate our object | ||
| 50 | my $eris = eris::log::contextualizer->new( | ||
| 51 | config => $config, | ||
| 52 | ); | ||
| 53 | my $schemas = eris::schemas->new( | ||
| 54 | exists $config->{schemas} ? ( %{ $config->{schemas} } ) : (), | ||
| 55 | ); | ||
| 56 | |||
| 57 | # POE Sessions | ||
| 58 | my $main_session = POE::Session->create( | ||
| 59 | inline_states => { | ||
| 60 | _start => \&main_start, | ||
| 61 | _stop => \&main_stop, | ||
| 62 | _child => \&main_child, | ||
| 63 | stats => \&main_stats, | ||
| 64 | syslog_input => \&syslog_input, | ||
| 65 | syslog_error => \&syslog_error, | ||
| 66 | es_bulk => \&es_bulk, | ||
| 67 | }, | ||
| 68 | heap => { | ||
| 69 | bulk_queue => [], | ||
| 70 | }, | ||
| 71 | ); | ||
| 72 | |||
| 73 | POE::Kernel->run(); | ||
| 74 | exit 0; | ||
| 75 | |||
| 76 | sub main_stop { | ||
| 77 | $poe_kernel->post( es => 'shutdown' ); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub main_child { | ||
| 81 | my ($kernel,$heap,$reason,$child) = @_[KERNEL,HEAP,ARG0,ARG1]; | ||
| 82 | |||
| 83 | my $stat = "child_$reason"; | ||
| 84 | $heap->{stats}{$stat} ||= 0; | ||
| 85 | $heap->{stats}{$stat}++; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub main_start { | ||
| 89 | my ($kernel,$heap) = @_[KERNEL,HEAP]; | ||
| 90 | |||
| 91 | # Set our alias | ||
| 92 | $kernel->alias_set('main'); | ||
| 93 | |||
| 94 | # Handle to the syslog daemon | ||
| 95 | $heap->{io} = POE::Wheel::ReadWrite->new( | ||
| 96 | InputHandle => \*STDIN, | ||
| 97 | OutputHandle => \*STDOUT, | ||
| 98 | InputEvent => 'syslog_input', | ||
| 99 | InputFilter => POE::Filter::Line->new(), | ||
| 100 | OutputFilter => POE::Filter::Reference->new(), | ||
| 101 | ErrorEvent => 'syslog_error', | ||
| 102 | ); | ||
| 103 | |||
| 104 | # Handle ElasticSearch Indexing | ||
| 105 | $heap->{indexer} = POE::Component::ElasticSearch::Indexer->spawn( | ||
| 106 | Alias => 'es', | ||
| 107 | StatsInterval => $opt->stats_interval, | ||
| 108 | StatsHandler => sub { | ||
| 109 | my ($stats) = @_; | ||
| 110 | foreach my $k ( keys %{ $stats } ) { | ||
| 111 | $heap->{stats}{$k} ||= 0; | ||
| 112 | $heap->{stats}{$k} += $stats->{$k}; | ||
| 113 | } | ||
| 114 | }, | ||
| 115 | $config->{elasticsearch} ? %{ $config->{elasticsearch} } : (), | ||
| 116 | # Allow to override if specififed | ||
| 117 | $opt->flush_size ? (FlushSize => $opt->flush_size) : (), | ||
| 118 | $opt->flush_interval ? (FlushInterval => $opt->flush_interval) : (), | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub main_stats { | ||
| 123 | my ($kernel,$heap) = @_[KERNEL,HEAP]; | ||
| 124 | |||
| 125 | my $stats = exists $heap->{stats} ? delete $heap->{stats} : {}; | ||
| 126 | |||
| 127 | # Send the stats upstream | ||
| 128 | $heap->{io}->put( $stats ); | ||
| 129 | |||
| 130 | # Reset the stats | ||
| 131 | $heap->{stats} = {}; | ||
| 132 | $kernel->delay( stats => $opt->stats_interval ); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub syslog_input { | ||
| 136 | my ($kernel,$heap,$msg) = @_[KERNEL,HEAP,ARG0]; | ||
| 137 | |||
| 138 | return unless defined $msg; | ||
| 139 | return unless length $msg; | ||
| 140 | |||
| 141 | # Parse to a eris::log object | ||
| 142 | my $log = $eris->parse($msg); | ||
| 143 | # Transform into a bulk request data blob | ||
| 144 | my @bulk_data = $schemas->as_bulk( $log ); | ||
| 145 | |||
| 146 | if( @bulk_data ) { | ||
| 147 | $heap->{stats}{queued} ||= 0; | ||
| 148 | $heap->{stats}{queued} += @bulk_data; | ||
| 149 | |||
| 150 | # on the first batch, make sure we schedule a run | ||
| 151 | $kernel->delay_add( es_bulk => $opt->flush_interval / 2 ) | ||
| 152 | unless @{ $heap->{bulk_queue} }; | ||
| 153 | |||
| 154 | # Add the data | ||
| 155 | push @{ $heap->{bulk_queue} }, @bulk_data; | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | $heap->{stats}{no_schema} ||= 0; | ||
| 159 | $heap->{stats}{no_schema}++; | ||
| 160 | } | ||
| 161 | |||
| 162 | if( @{ $heap->{bulk_queue} } > 10 ) { | ||
| 163 | $kernel->yield('es_bulk'); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | sub es_bulk { | ||
| 168 | my ($kernel,$heap) = @_[KERNEL,HEAP]; | ||
| 169 | |||
| 170 | # Grab the batch | ||
| 171 | my $batch = delete $heap->{bulk_queue}; | ||
| 172 | $heap->{bulk_queue} = []; | ||
| 173 | |||
| 174 | return unless scalar @{ $batch }; | ||
| 175 | |||
| 176 | # Reschedule if necessary | ||
| 177 | $kernel->delay( es_bulk => $opt->flush_interval / 2 ); | ||
| 178 | |||
| 179 | # Pass along to the indexer | ||
| 180 | $kernel->post( es => queue => $batch ); | ||
| 181 | } | ||
| 182 | |||
| 183 | sub syslog_error { | ||
| 184 | my ($kernel,$heap) = @_[KERNEL,HEAP]; | ||
| 185 | delete $heap->{io}; | ||
| 186 | $kernel->post( es => 'shutdown' ); | ||
| 187 | } | ||
| 188 | |||
| 189 | __END__ | ||
| 190 | |||
| 191 | =pod | ||
| 192 | |||
| 193 | =encoding UTF-8 | ||
| 194 | |||
| 195 | =head1 NAME | ||
| 196 | |||
| 197 | eris-es-indexer.pl - Sample implementation using the eris toolkit to index data to elasticsearch | ||
| 198 | |||
| 199 | =head1 VERSION | ||
| 200 | |||
| 201 | version 0.008 | ||
| 202 | |||
| 203 | =head1 AUTHOR | ||
| 204 | |||
| 205 | Brad Lhotsky <brad@divisionbyzero.net> | ||
| 206 | |||
| 207 | =head1 COPYRIGHT AND LICENSE | ||
| 208 | |||
| 209 | This software is Copyright (c) 2015 by Brad Lhotsky. | ||
| 210 | |||
| 211 | This is free software, licensed under: | ||
| 212 | |||
| 213 | The (three-clause) BSD License | ||
| 214 | |||
| 215 | =cut |