POE-Component-ElasticSearch-Indexer
view release on metacpan or search on metacpan
bin/file-to-elasticsearch.pl view on Meta::CPAN
#!perl
# PODNAME: file-to-elasticsearch.pl
# ABSTRACT: A simple utility to tail a file and index each line as a document in ElasticSearch
use strict;
use warnings;
use Getopt::Long::Descriptive qw(describe_options);
use Hash::Merge::Simple qw(merge);
use JSON::MaybeXS qw(decode_json encode_json);
use Log::Log4perl qw(:easy);
use Module::Load qw(load);
use Module::Loaded qw(is_loaded);
use Pod::Usage;
use Ref::Util qw(is_arrayref is_hashref);
use YAML ();
sub POE::Kernel::ASSERT_DEFAULT { 1 }
use POE qw(
Component::ElasticSearch::Indexer
Wheel::FollowTail
);
my %DEFAULT = (
config => '/etc/file-to-elasticsearch.yaml',
stats_interval => 60,
);
my ($opt,$usage) = describe_options('%c %o',
['config|c=s', "Config file, default: $DEFAULT{config}",
{ default => $DEFAULT{config}, callbacks => { "must be a readable file" => sub { -r $_[0] } } }
],
['log4perl-config|L=s', "Log4perl Configuration to use, defaults to STDERR",
{ callbacks => { "must be a readable file" => sub { -r $_[0] } } }
],
['stats-interval|s=i', "Seconds between displaying statistics, default: $DEFAULT{stats_interval}",
{ default => $DEFAULT{stats_interval} },
],
['debug', "Enable most verbose output" ],
[],
['help', "Display this help.", { shortcircuit => 1 }],
['manual|pod', "Display the user manaul.", { shortcircuit => 1 }],
);
if( $opt->help ) {
print $usage->text;
exit 0;
}
pod2usage( -verbose => 2, -exitval => 0 ) if $opt->manual;
my $config = YAML::LoadFile( $opt->config );
# Initialize Logging
my $level = $opt->debug ? 'TRACE' : 'DEBUG';
my $loggingConfig = $opt->log4perl_config || \qq{
log4perl.logger = $level, Screen
log4perl.appender.Screen = Log::Log4perl::Appender::ScreenColoredLevels
log4perl.appender.Screen.layout = PatternLayout
log4perl.appender.Screen.layout.ConversionPattern = %d [%P] %p - %m%n
};
Log::Log4perl->init($loggingConfig);
my $main = POE::Session->create(
inline_states => {
_start => \&main_start,
_stop => \&main_stop,
_child => \&main_child,
stats => \&main_stats,
got_new_line => \&got_new_line,
get_error => \&got_error,
log4perl_refresh => \&log4perl_refresh,
},
heap => {
config => $config,
( run in 2.667 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )