Server/Logstash.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/Kibana.pm ↑ Summary Server/Logstash/Indexer.pm »
1 #
2 # $Id$
3 #
4 # server::logstash Brik
5 #
6 package Metabrik::Server::Logstash;
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::System::Process);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable elk) ],
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 => [ ],
33 get_binary => [ ],
34 check_config => [ qw(conf_file) ],
35 start => [ qw(conf_file|OPTIONAL) ],
36 start_in_foreground => [ qw(conf_file|OPTIONAL) ],
37 stop => [ ],
38 generate_conf => [ qw(conf_file|OPTIONAL) ],
39 status => [ ],
40 },
41 require_modules => {
42 'Metabrik::File::Text' => [ ],
43 },
44 require_binaries => {
45 tar => [ ],
46 },
47 need_packages => {
48 ubuntu => [ qw(tar openjdk-8-jre-headless) ],
49 debian => [ qw(tar openjdk-8-jre-headless) ],
50 kali => [ qw(tar openjdk-8-jre-headless) ],
51 freebsd => [ qw(openjdk logstash) ],
52 },
53 };
54 }
55  
56 sub brik_use_properties {
57 my $self = shift;
58  
59 my $datadir = $self->datadir;
60 my $version = $self->version;
61 my $conf_file = $datadir."/logstash-$version.conf";
62 my $log_file = $datadir."/logstash-$version.log";
63  
64 return {
65 attributes_default => {
66 conf_file => $conf_file,
67 log_file => $log_file,
68 },
69 };
70 }
71  
72 sub get_binary {
73 my $self = shift;
74  
75 my $binary = $self->binary;
76 if (! defined($binary)) {
77 my $datadir = $self->datadir;
78 my $version = $self->version;
79 $binary = $datadir.'/logstash-'.$version.'/bin/logstash';
80 }
81  
82 $self->brik_help_run_file_not_found('get_binary', $binary) or return;
83  
84 $self->log->verbose("get_binary: found binary [$binary]");
85  
86 return $binary;
87 }
88  
89 sub generate_conf {
90 my $self = shift;
91 my ($conf_file) = @_;
92  
93 $conf_file ||= $self->conf_file;
94  
95 my $conf =<<EOF
96 input {
97 file {
98 type => "apache"
99 path => "/var/log/www/example.com-access.log*"
100 add_field => { "site" => "www.example.com" }
101 start_position => "beginning" # Start from beginning of every files
102 sincedb_path => "/dev/null" # Read files entirely every times
103 ignore_older => "0" # Process every file, even older than 24 hours
104 }
105 }
106  
107 filter {
108 if [message] =~ /: logfile turned over\$/ {
109 drop {}
110 }
111 if [type] == "apache" {
112 # Defining patterns:
113 # https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html
114 grok {
115 match => { "message" => "%{COMBINEDAPACHELOG}" }
116 overwrite => [ "message" ]
117 }
118 geoip {
119 source => "clientip"
120 target => "geoip"
121 add_tag => [ "apache-geoip" ]
122 }
123 date {
124 match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
125 remove_field => [ "timestamp" ]
126 }
127 }
128 }
129  
130 output {
131 if "_grokparsefailure" in [tags] {
132 null {}
133 }
134 if [type] == "apache" {
135 redis {
136 host => "127.0.0.1"
137 data_type => "list"
138 key => "logstash"
139 codec => json
140 congestion_interval => 1
141 congestion_threshold => 20000000
142 # Batch processing requires redis >= 2.4.0
143 batch => true
144 batch_events => 50
145 batch_timeout => 5
146 }
147 }
148 else {
149 stdout {
150 codec => json
151 }
152 }
153 }
154 EOF
155 ;
156  
157 my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
158 $ft->append(0);
159 $ft->overwrite(1);
160  
161 $ft->write($conf, $conf_file) or return;
162  
163 return $conf_file;
164 }
165  
166 sub install {
167 my $self = shift;
168  
169 my $datadir = $self->datadir;
170 my $version = $self->version;
171  
172 my $url = 'https://artifacts.elastic.co/downloads/logstash/logstash-5.5.2.tar.gz';
173 if ($version eq '2.4.0') {
174 $url = 'https://download.elastic.co/logstash/logstash/logstash-2.4.0.tar.gz';
175 }
176 elsif ($version eq '5.0.0') {
177 $url = 'https://artifacts.elastic.co/downloads/logstash/logstash-5.0.0.tar.gz';
178 }
179  
180 my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
181 $cw->mirror($url, "$datadir/logstash.tar.gz") or return;
182  
183 my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp';
184  
185 if (defined($self->shell)) {
186 $self->shell->run_cd($datadir) or return;
187 }
188 else {
189 chdir($datadir) or return $self->log->error("install: chdir: $!");
190 }
191  
192 my $cmd = "tar zxvf logstash.tar.gz";
193 my $r = $self->execute($cmd) or return;
194  
195 if (defined($self->shell)) {
196 $self->shell->run_cd($cwd) or return;
197 }
198 else {
199 chdir($cwd) or return $self->log->error("install: chdir: $!");
200 }
201  
202 return 1;
203 }
204  
205 sub check_config {
206 my $self = shift;
207 my ($conf_file) = @_;
208  
209 $self->brik_help_run_undef_arg('start', $conf_file) or return;
210 $self->brik_help_run_file_not_found('start', $conf_file) or return;
211  
212 my $datadir = $self->datadir;
213 my $version = $self->version;
214 my $log_file = $self->log_file;
215  
216 my $binary = $self->get_binary or return;
217  
218 my $cmd = "$binary -t -f $conf_file -l $log_file";
219  
220 $self->log->info("check_config: started...");
221  
222 return $self->system($cmd);
223 }
224  
225 #
226 # logstash -f <config_file> -l <log_file> --debug
227 #
228 sub start {
229 my $self = shift;
230 my ($conf_file) = @_;
231  
232 if ($self->status) {
233 return $self->info_process_is_running;
234 }
235  
236 $conf_file ||= $self->conf_file;
237 $self->brik_help_run_undef_arg('start', $conf_file) or return;
238 $self->brik_help_run_file_not_found('start', $conf_file) or return;
239  
240 # Make if a full path file
241 if ($conf_file !~ m{^/}) {
242 my $cwd = define($self->shell) && $self->shell->full_pwd || '/tmp';
243 $conf_file = $cwd.'/'.$conf_file;
244 }
245  
246 my $log_file = $self->log_file;
247 my $no_output = $self->no_output;
248  
249 my $binary = $self->get_binary or return;
250  
251 $self->close_output_on_start($no_output);
252  
253 $self->SUPER::start(sub {
254 $self->log->verbose("Within daemon");
255  
256 my $cmd = "$binary -f $conf_file -l $log_file";
257 if ($self->log->level > 2) {
258 $cmd .= ' --debug';
259 }
260  
261 $self->system($cmd);
262  
263 $self->log->error("start: son failed to start");
264 exit(1);
265 });
266  
267 return 1;
268 }
269  
270 sub start_in_foreground {
271 my $self = shift;
272 my ($conf_file) = @_;
273  
274 $conf_file ||= $self->conf_file;
275 $self->brik_help_run_undef_arg('start_in_foreground', $conf_file) or return;
276 $self->brik_help_run_file_not_found('start_in_foreground', $conf_file) or return;
277  
278 if ($self->status) {
279 return $self->error_process_is_running;
280 }
281  
282 my $log_file = $self->log_file;
283  
284 my $binary = $self->get_binary or return;
285  
286 my $cmd = "$binary -f $conf_file -l $log_file";
287 if ($self->log->level > 2) {
288 $cmd .= ' --debug';
289 }
290  
291 return $self->system($cmd);
292 }
293  
294 sub stop {
295 my $self = shift;
296  
297 if (! $self->status) {
298 return $self->info_process_is_not_running;
299 }
300  
301 my $conf_file = $self->conf_file;
302  
303 my $string = "-f $conf_file";
304 my $pid = $self->get_pid_from_string($string) or return;
305  
306 return $self->kill($pid);
307 }
308  
309 sub status {
310 my $self = shift;
311  
312 my $conf_file = $self->conf_file;
313  
314 my $string = "-f $conf_file";
315 if ($self->is_running_from_string($string)) {
316 $self->verbose_process_is_running;
317 return 1;
318 }
319  
320 $self->verbose_process_is_not_running;
321 return 0;
322 }
323  
324 1;
325  
326 __END__
« Server/Kibana.pm ↑ Summary Server/Logstash/Indexer.pm »