Server/Logstash/Oneshot.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Server/Logstash/Indexer.pm ↑ Summary Server/Redis.pm »
1 #
2 # $Id$
3 #
4 # server::logstash::oneshot Brik
5 #
6 package Metabrik::Server::Logstash::Oneshot;
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 binary => [ qw(binary_path) ],
25 },
26 attributes_default => {
27 version => '5.5.2',
28 no_output => 0,
29 log_file => 'logstash.log',
30 },
31 commands => {
32 install => [ ], # Inherited
33 stdin_to_stdout => [ ],
34 stdin_to_json => [ ],
35 test_filter_against_string => [ qw(filter_file string|string_list) ],
36 test_filter_against_logs => [ qw(filter_file input_file) ],
37 },
38 require_modules => {
39 'Metabrik::File::Text' => [ ],
40 },
41 };
42 }
43  
44 sub stdin_to_stdout {
45 my $self = shift;
46  
47 my $binary = $self->get_binary or return;
48  
49 my $cmd = "$binary -e 'input { stdin { } } output { stdout {} }'";
50  
51 $self->log->info("stdin_to_stdout: starting...");
52  
53 return $self->system($cmd);
54 }
55  
56 sub stdin_to_json {
57 my $self = shift;
58  
59 my $binary = $self->get_binary or return;
60  
61 my $cmd = "$binary -e 'input { stdin { } } output { stdout { codec => json } }'";
62  
63 $self->log->info("stdin_to_json: starting...");
64  
65 return $self->system($cmd);
66 }
67  
68 sub test_filter_against_string {
69 my $self = shift;
70 my ($filter_file, $string) = @_;
71  
72 $self->brik_help_run_undef_arg('test_filter_against_string', $string) or return;
73 my $ref = $self->brik_help_run_invalid_arg('test_filter_against_string', $string,
74 'ARRAY', 'SCALAR') or return;
75 $self->brik_help_run_undef_arg('test_filter_against_string', $filter_file) or return;
76 $self->brik_help_run_file_not_found('test_filter_against_string', $filter_file) or return;
77  
78 my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
79 $ft->as_array(1);
80 my $lines = $ft->read($filter_file) or return;
81  
82 if (@$lines == 0) {
83 return $self->log->error("test_filter_against_string: file [$filter_file] is empty");
84 }
85  
86 my @lines = ();
87 for (@$lines) {
88 push @lines, $_ unless $_ =~ m{^\s*#}; # Remove comments, as it is not supported.
89 }
90  
91 my $binary = $self->get_binary or return;
92  
93 my $filter = join('', @lines);
94  
95 my $input =<<EOF
96 input {
97 stdin { }
98 }
99 EOF
100 ;
101  
102 my $output =<<EOF
103 output {
104 if "_grokparsefailure" in [tags]
105 or "_dateparsefailure" in [tags]
106 {
107 stdout {
108 codec => rubydebug
109 }
110 }
111 else {
112 null {}
113 }
114 }
115 EOF
116 ;
117  
118 my $cmd;
119 if ($ref eq 'ARRAY') {
120 my $tmp_file = $self->datadir.'/logstash-test-filter-tmp.txt';
121 $ft->write($string, $tmp_file) or return;
122 $cmd = "$binary -e '$input filter { $filter } $' < $tmp_file";
123 }
124 else {
125 $cmd = "echo \"$string\" | $binary -e '$input filter { $filter } $output'";
126 }
127  
128 $self->log->info("test_filter_against_string: starting...");
129  
130 return $self->system($cmd);
131 }
132  
133 sub test_filter_against_logs {
134 my $self = shift;
135 my ($filter_file, $input_file) = @_;
136  
137 $self->brik_help_run_undef_arg('test_filter_against_logs', $filter_file) or return;
138 $self->brik_help_run_file_not_found('test_filter_against_logs', $filter_file) or return;
139 $self->brik_help_run_undef_arg('test_filter_against_logs', $input_file) or return;
140 $self->brik_help_run_file_not_found('test_filter_against_logs', $input_file) or return;
141  
142 my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
143 $ft->as_array(1);
144 $ft->strip_crlf(1);
145 my $lines = $ft->read($filter_file) or return;
146  
147 if (@$lines == 0) {
148 return $self->log->error("test_filter_against_logs: file [$filter_file] is empty");
149 }
150  
151 my @lines = ();
152 for (@$lines) {
153 push @lines, $_ unless $_ =~ m{^\s*#}; # Remove comments, as it is not supported.
154 }
155  
156 my $filter_string = join('', @lines);
157  
158 my $conf =<<EOF
159 input { stdin {} }
160  
161 filter { $filter_string }
162  
163 output {
164 if "_grokparsefailure" in [tags]
165 or "_dateparsefailure" in [tags]
166 {
167 stdout {
168 codec => rubydebug
169 }
170 }
171 else {
172 null {}
173 }
174 }
175 EOF
176 ;
177  
178 my @conf_lines = split(/\n/, $conf);
179 my $conf_string = join('', @conf_lines);
180  
181 my $binary = $self->get_binary or return;
182  
183 my $cmd = "$binary -e '$conf_string' < $input_file";
184  
185 $self->log->info("test_filter_against_string: starting...");
186  
187 return $self->system($cmd);
188 }
189  
190 1;
191  
192 __END__
« Server/Logstash/Indexer.pm ↑ Summary Server/Redis.pm »