| 1 | #!/usr/local/bin/perl | ||
| 2 | # PODNAME: eris-eris-client.pl | ||
| 3 | # ABSTRACT: Simple wrapper to spawn workers for handling syslog stream | ||
| 4 | ## no critic (RequireEndWithOne) | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use FindBin; | ||
| 9 | use Getopt::Long::Descriptive; | ||
| 10 | use Path::Tiny; | ||
| 11 | use POE qw( | ||
| 12 | Component::Client::eris | ||
| 13 | Component::Client::TCP | ||
| 14 | Component::WheelRun::Pool | ||
| 15 | Wheel::ReadWrite | ||
| 16 | Filter::Line | ||
| 17 | ); | ||
| 18 | use POSIX qw(strftime); | ||
| 19 | use Ref::Util qw(is_hashref); | ||
| 20 | use Sys::Hostname qw(hostname); | ||
| 21 | use YAML qw(); | ||
| 22 | |||
| 23 | my ($opt,$usage) = describe_options('%c - %o', | ||
| 24 | [ 'config=s', 'Eris YAML config file, required', { validate => { "Must be a readable file." => sub { -r $_[0] } } } ], | ||
| 25 | [ 'eris-host|eh=s', 'Eris Dispatcher Host, defaults to localhost' ], | ||
| 26 | [ 'eris-port|ep=s', 'Eris Dispatcher Port, defaults to 9514' ], | ||
| 27 | [ 'indexer=s', 'Script to run to handle indexing, defaults to the eris-es-indexer.pl script.' ], | ||
| 28 | [ 'workers|w=i', 'Number of workers to run, default 4', { default => 4 } ], | ||
| 29 | [], | ||
| 30 | [ 'stats-interval=i', 'Interval to send statistics, in seconds, default: 60', { default => 60 }], | ||
| 31 | [ 'graphite-host|g=s', 'Graphite host to dispatch metrics to, default disabled' ], | ||
| 32 | [ 'graphite-port|gp=s', 'Graphite port to dispatch metrics to, default 2003', { default => 2003 }], | ||
| 33 | [ 'graphite-prefix=s', 'Graphite prefix, default: eris.client', { default => "eris.client" }], | ||
| 34 | [], | ||
| 35 | [ 'help', 'Display this help' ], | ||
| 36 | ); | ||
| 37 | if( $opt->help ) { | ||
| 38 | print $usage->text; | ||
| 39 | exit 0; | ||
| 40 | } | ||
| 41 | |||
| 42 | my $main_session = POE::Session->create( | ||
| 43 | inline_states => { | ||
| 44 | _start => \&main_start, | ||
| 45 | _stop => \&main_stop, | ||
| 46 | stats => \&main_stats, | ||
| 47 | worker_stdout => \&worker_stdout, | ||
| 48 | }, | ||
| 49 | heap => { | ||
| 50 | stats => {}, | ||
| 51 | hostname => hostname(), | ||
| 52 | }, | ||
| 53 | ); | ||
| 54 | |||
| 55 | POE::Kernel->run(); | ||
| 56 | |||
| 57 | sub main_stop { } | ||
| 58 | |||
| 59 | sub main_start { | ||
| 60 | my ($kernel,$heap) = @_[KERNEL,HEAP]; | ||
| 61 | |||
| 62 | # Set out alias | ||
| 63 | $kernel->alias_set('main'); | ||
| 64 | |||
| 65 | # Load Config | ||
| 66 | my $cfg = {}; | ||
| 67 | if( $opt->config ) { | ||
| 68 | eval { | ||
| 69 | $cfg = YAML::LoadFile( $opt->config ); | ||
| 70 | 1; | ||
| 71 | } or do { | ||
| 72 | my $err = $@; | ||
| 73 | warn sprintf "Failed loading %s: %s", $opt->config, $err; | ||
| 74 | }; | ||
| 75 | } | ||
| 76 | # Store the Config | ||
| 77 | $heap->{cfg} = $cfg; | ||
| 78 | |||
| 79 | # Startup the Eris Client | ||
| 80 | my $eris_session = POE::Component::Client::eris->spawn( | ||
| 81 | # Defaults | ||
| 82 | Subscribe => 'fullfeed', | ||
| 83 | # Load the ConfigFile | ||
| 84 | exists $cfg->{client} ? %{ $cfg->{client} } : (), | ||
| 85 | # Some CLI Options take precedence | ||
| 86 | $opt->eris_host ? ( RemoteAddress => $opt->eris_host ) : (), | ||
| 87 | $opt->eris_port ? ( RemotePort => $opt->eris_port ) : (), | ||
| 88 | ReturnType => 'string', | ||
| 89 | MessageHandler => sub { | ||
| 90 | $heap->{stats}{dispatched} ||= 0; | ||
| 91 | $heap->{stats}{dispatched}++; | ||
| 92 | $kernel->post( pool => dispatch => @_ ); | ||
| 93 | }, | ||
| 94 | ); | ||
| 95 | |||
| 96 | # Start the Graphite Handler | ||
| 97 | if( $opt->graphite_host ) { | ||
| 98 | $heap->{_graphite} = POE::Component::Client::TCP->new( | ||
| 99 | Alias => 'graphite', | ||
| 100 | RemoteAddress => $opt->graphite_host, | ||
| 101 | RemotePort => $opt->graphite_port, | ||
| 102 | ConnectTimeout => 5, | ||
| 103 | Connected => sub { | ||
| 104 | # let the parent know we're able to write | ||
| 105 | $heap->{graphite} = 1; | ||
| 106 | }, | ||
| 107 | ConnectError => sub { | ||
| 108 | my ($op,$err_num,$err_str) = @_[ARG0..ARG2]; | ||
| 109 | delete $heap->{graphite} if exists $heap->{graphite}; | ||
| 110 | $heap->{stats}{graphite_errors} ||= 0; | ||
| 111 | $heap->{stats}{graphite_errors}++; | ||
| 112 | # Attempt to reconnect | ||
| 113 | $kernel->delay( reconnect => 60 ); | ||
| 114 | }, | ||
| 115 | Disconnected => sub { | ||
| 116 | $kernel->delay( reconnect => 60 ); | ||
| 117 | }, | ||
| 118 | Filter => "POE::Filter::Line", | ||
| 119 | InlineStates => { | ||
| 120 | send => sub { | ||
| 121 | $_[HEAP]->{server}->put($_[ARG0]); | ||
| 122 | }, | ||
| 123 | }, | ||
| 124 | ServerInput => sub { | ||
| 125 | # Shouldn't get any | ||
| 126 | $_[HEAP]->{stats}{graphite_feedback} ||= 0; | ||
| 127 | $_[HEAP]->{stats}{graphite_feedback}++; | ||
| 128 | }, | ||
| 129 | ServerError => sub { | ||
| 130 | $_[KERNEL]->yield( 'reconnect' ); | ||
| 131 | }, | ||
| 132 | ); | ||
| 133 | } | ||
| 134 | |||
| 135 | # Figure out where we're installed | ||
| 136 | my $bindir = path( "$FindBin::RealBin" ); | ||
| 137 | $heap->{workers} = POE::Component::WheelRun::Pool->spawn( | ||
| 138 | Alias => 'pool', | ||
| 139 | PoolSize => $opt->workers, | ||
| 140 | Program => $^X, | ||
| 141 | ProgramArgs => [ | ||
| 142 | '--', | ||
| 143 | $opt->indexer ? $opt->indexer : $bindir->child('eris-es-indexer.pl')->stringify, | ||
| 144 | $opt->config ? ('--config', $opt->config ) : (), | ||
| 145 | $opt->stats_interval ? ('--stats-interval', $opt->stats_interval ) : (), | ||
| 146 | ], | ||
| 147 | StdinFilter => POE::Filter::Line->new(), | ||
| 148 | StdoutFilter => POE::Filter::Reference->new(), | ||
| 149 | # Handlers | ||
| 150 | StatsHandler => sub { | ||
| 151 | my ($stats) = @_; | ||
| 152 | if( is_hashref($stats) ) { | ||
| 153 | foreach my $k ( keys %$stats ) { | ||
| 154 | $heap->{stats}{$k} ||= 0; | ||
| 155 | $heap->{stats}{$k} += $stats->{$k}; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | print STDERR "StatsHandler did not receive a hashref.\n"; | ||
| 160 | } | ||
| 161 | }, | ||
| 162 | StdoutHandler => sub { | ||
| 163 | $kernel->post( main => worker_stdout => @_); | ||
| 164 | }, | ||
| 165 | StderrHandler => sub { | ||
| 166 | printf STDERR "[worker_stderr] %s\n", $_ for @_; | ||
| 167 | }, | ||
| 168 | ); | ||
| 169 | |||
| 170 | $kernel->delay(stats => $opt->stats_interval); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub main_stats { | ||
| 174 | my ($kernel,$heap) = @_[KERNEL,HEAP]; | ||
| 175 | |||
| 176 | my $time = time(); | ||
| 177 | my $stats = exists $heap->{stats} ? delete $heap->{stats} : {}; | ||
| 178 | printf "%s STATS: %s\n", strftime("%H:%M",localtime), join(', ', map { sprintf "%s=%s", $_, $stats->{$_} } sort keys %{ $stats }); | ||
| 179 | |||
| 180 | # Are we graphiting? | ||
| 181 | if( exists $heap->{graphite} && $heap->{graphite} ) { | ||
| 182 | foreach my $stat (keys %{ $stats }) { | ||
| 183 | my $metric = join('.', $opt->graphite_prefix, $heap->{hostname}, $stat); | ||
| 184 | $kernel->post( graphite => send => join " ", $metric, $stats->{$stat}, $time); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | # Reschedule ourselves; | ||
| 189 | $heap->{stats} = {}; | ||
| 190 | $kernel->delay( stats => $opt->stats_interval ) unless exists $heap->{_shutdown}; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub worker_stdout { | ||
| 194 | my ($kernel,$heap,$stats) = @_[KERNEL,HEAP,ARG0]; | ||
| 195 | |||
| 196 | # Make sure we have stats | ||
| 197 | if( is_hashref($stats) ) { | ||
| 198 | # Aggregate stats from all our workers | ||
| 199 | foreach my $s (keys %{ $stats }) { | ||
| 200 | $heap->{stats}{$s} ||= 0; | ||
| 201 | $heap->{stats}{$s} += $stats->{$s}; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | else { | ||
| 205 | printf STDERR "Received bad data from the client\n%s\n", ref($stats) ? ref($stats) : $stats; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | __END__ | ||
| 210 | |||
| 211 | =pod | ||
| 212 | |||
| 213 | =encoding UTF-8 | ||
| 214 | |||
| 215 | =head1 NAME | ||
| 216 | |||
| 217 | eris-eris-client.pl - Simple wrapper to spawn workers for handling syslog stream | ||
| 218 | |||
| 219 | =head1 VERSION | ||
| 220 | |||
| 221 | version 0.008 | ||
| 222 | |||
| 223 | =head1 AUTHOR | ||
| 224 | |||
| 225 | Brad Lhotsky <brad@divisionbyzero.net> | ||
| 226 | |||
| 227 | =head1 COPYRIGHT AND LICENSE | ||
| 228 | |||
| 229 | This software is Copyright (c) 2015 by Brad Lhotsky. | ||
| 230 | |||
| 231 | This is free software, licensed under: | ||
| 232 | |||
| 233 | The (three-clause) BSD License | ||
| 234 | |||
| 235 | =cut |