| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # server::logstash::indexer Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Server::Logstash::Indexer; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::Server::Logstash); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | datadir => [ qw(datadir) ], | ||
| 20 | conf_file => [ qw(file) ], | ||
| 21 | log_file => [ qw(file) ], | ||
| 22 | version => [ qw(2.4.0|5.0.0|5.5.2) ], | ||
| 23 | no_output => [ qw(0|1) ], | ||
| 24 | redis_host => [ qw(host) ], | ||
| 25 | es_nodes => [ qw(node_list) ], | ||
| 26 | binary => [ qw(binary_path) ], | ||
| 27 | }, | ||
| 28 | attributes_default => { | ||
| 29 | version => '5.5.2', | ||
| 30 | no_output => 0, | ||
| 31 | log_file => 'logstash.log', | ||
| 32 | redis_host => '127.0.0.1', | ||
| 33 | es_nodes => [ '127.0.0.1:9200' ], | ||
| 34 | }, | ||
| 35 | commands => { | ||
| 36 | install => [ ], | ||
| 37 | get_binary => [ ], | ||
| 38 | check_config => [ qw(conf_file) ], | ||
| 39 | start => [ qw(conf_file|OPTIONAL) ], | ||
| 40 | start_in_foreground => [ qw(conf_file|OPTIONAL) ], | ||
| 41 | stop => [ ], | ||
| 42 | generate_conf => [ qw(conf_file|OPTIONAL redis_host|OPTIONAL) ], | ||
| 43 | status => [ ], | ||
| 44 | restart => [ ], | ||
| 45 | }, | ||
| 46 | }; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub generate_conf { | ||
| 50 | my $self = shift; | ||
| 51 | my ($conf_file, $redis_host) = @_; | ||
| 52 | |||
| 53 | my $es_nodes = $self->es_nodes; | ||
| 54 | $self->brik_help_run_undef_arg('generate_conf', $es_nodes) or return; | ||
| 55 | $self->brik_help_run_invalid_arg('generate_conf', $es_nodes, 'ARRAY') or return; | ||
| 56 | |||
| 57 | $conf_file ||= $self->conf_file; | ||
| 58 | $redis_host ||= $self->redis_host; | ||
| 59 | |||
| 60 | my $es_hosts = '[ '; | ||
| 61 | for my $this (@$es_nodes) { | ||
| 62 | $es_hosts .= "\"$this\", "; | ||
| 63 | } | ||
| 64 | $es_hosts =~ s{, $}{ \]}; | ||
| 65 | |||
| 66 | my $conf =<<EOF | ||
| 67 | input { | ||
| 68 | redis { | ||
| 69 | host => "$redis_host" | ||
| 70 | key => "logstash" | ||
| 71 | data_type => "list" | ||
| 72 | codec => json | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | output { | ||
| 77 | if "_grokparsefailure" in [tags] { | ||
| 78 | null {} | ||
| 79 | } | ||
| 80 | if [type] == "example" { | ||
| 81 | elasticsearch { | ||
| 82 | hosts => $es_hosts | ||
| 83 | index => "example-%{+YYYY-MM-dd}" | ||
| 84 | document_type => "document" | ||
| 85 | template_name => "example-*" | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | EOF | ||
| 90 | ; | ||
| 91 | |||
| 92 | my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; | ||
| 93 | $ft->append(0); | ||
| 94 | $ft->overwrite(1); | ||
| 95 | |||
| 96 | $ft->write($conf, $conf_file) or return; | ||
| 97 | |||
| 98 | return $conf_file; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub restart { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | $self->stop; | ||
| 105 | |||
| 106 | sleep(2); # Wait for process to stop | ||
| 107 | |||
| 108 | return $self->start; | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; | ||
| 112 | |||
| 113 | __END__ | ||