/
Logs.pm
347 lines (275 loc) · 8.67 KB
/
Logs.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package Monitoring::Availability::Logs;
use 5.008;
use strict;
use warnings;
use Data::Dumper;
use Carp;
use POSIX qw(strftime);
use constant {
STATE_UP => 0,
STATE_DOWN => 1,
STATE_UNREACHABLE => 2,
STATE_OK => 0,
STATE_WARNING => 1,
STATE_CRITICAL => 2,
STATE_UNKNOWN => 3,
START_NORMAL => 1,
START_RESTART => 2,
STOP_NORMAL => 0,
STOP_ERROR => -1,
};
=head1 NAME
Monitoring::Availability::Logs - Load/Store/Access Logfiles
=head1 DESCRIPTION
Store for logfiles
=head2 new ( [ARGS] )
Creates an C<Monitoring::Availability::Log> object.
=cut
sub new {
my $class = shift;
my(%options) = @_;
my $self = {
'verbose' => 0, # enable verbose output
'logger' => undef, # logger object used for verbose output
'log_string' => undef, # logs from string
'log_livestatus' => undef, # logs from a livestatus query
'log_file' => undef, # logs from a file
'log_dir' => undef, # logs from a dir
};
bless $self, $class;
for my $opt_key (keys %options) {
if(exists $self->{$opt_key}) {
$self->{$opt_key} = $options{$opt_key};
}
else {
croak("unknown option: $opt_key");
}
}
# create an empty log store
$self->{'logs'} = [];
# which source do we use?
if(defined $self->{'log_string'}) {
$self->_store_logs_from_string($self->{'log_string'});
}
if(defined $self->{'log_file'}) {
$self->_store_logs_from_file($self->{'log_file'});
}
if(defined $self->{'log_dir'}) {
$self->_store_logs_from_dir($self->{'log_dir'});
}
if(defined $self->{'log_livestatus'}) {
$self->_store_logs_from_livestatus($self->{'log_livestatus'});
}
return $self;
}
########################################
=head1 METHODS
=head2 get_logs
get_logs()
returns all read logs as array of hashrefs
=cut
sub get_logs {
my $self = shift;
return($self->{'logs'});
}
########################################
# INTERNAL SUBS
########################################
sub _store_logs_from_string {
my $self = shift;
my $string = shift;
return unless defined $string;
for my $line (split/\n/mx, $string) {
my $data = $self->_parse_line($line);
push @{$self->{'logs'}}, $data if defined $data;
}
return 1;
}
########################################
sub _store_logs_from_file {
my $self = shift;
my $file = shift;
return unless defined $file;
open(my $FH, '<', $file) or croak('cannot read file '.$file.': '.$!);
while(my $line = <$FH>) {
chomp($line);
my $data = $self->_parse_line($line);
push @{$self->{'logs'}}, $data if defined $data;
}
close($FH);
return 1;
}
########################################
sub _store_logs_from_dir {
my $self = shift;
my $dir = shift;
return unless defined $dir;
opendir(my $dh, $dir) or croak('cannot open directory '.$dir.': '.$!);
while(my $file = readdir($dh)) {
if($file =~ m/\.log$/mx) {
$self->_store_logs_from_file($dir.'/'.$file);
}
}
closedir $dh;
return 1;
}
########################################
sub _store_logs_from_livestatus {
my $self = shift;
my $log_array = shift;
return unless defined $log_array;
for my $entry (@{$log_array}) {
my $data = $self->_parse_livestatus_entry($entry);
push @{$self->{'logs'}}, $data if defined $data;
}
return 1;
}
########################################
sub _parse_livestatus_entry {
my $self = shift;
my $entry = shift;
my $string = $entry->{'options'} || '';
if($string eq '') {
# extract starts/stops
$self->_set_from_type($entry, $string);
return $entry;
}
# extract more information from our options
$self->_set_from_options($entry, $string);
return $entry;
}
########################################
sub _parse_line {
my $self = shift;
my $string = shift;
my $return = {
'time' => '',
'type' => '',
};
return if substr($string, 0, 1, '') ne '[';
$return->{'time'} = substr($string, 0, 10, '');
return if substr($string, 0, 2, '') ne '] ';
$return->{'type'} = $self->_strtok($string, ': ');
if(!defined $string) {
# extract starts/stops
$self->_set_from_type($return, $string);
return $return;
}
# extract more information from our options
$self->_set_from_options($return, $string);
return $return;
}
########################################
# search for a token and return first occurance, trim that part from string
sub _strtok {
my $index = index($_[1], $_[2]);
if($index != -1) {
my $value = substr($_[1], 0, $index, '');
substr($_[1], 0, length($_[2]), '');
return($value);
}
my $value = $_[1];
undef $_[1];
# seperator not found
return($value);
}
########################################
sub _set_from_options {
my $self = shift;
my $data = shift;
my $string = shift;
# Host States
if( $data->{'type'} eq 'HOST ALERT'
or $data->{'type'} eq 'CURRENT HOST STATE'
or $data->{'type'} eq 'INITIAL HOST STATE'
) {
$data->{'host_name'} = $self->_strtok($string, ';');
$data->{'state'} = $self->_statestr_to_state($self->_strtok($string, ';'));
$data->{'hard'} = $self->_softstr_to_hard($self->_strtok($string, ';'));
$self->_strtok($string, ';');
$data->{'plugin_output'} = $self->_strtok($string, ';');
}
# Service States
elsif( $data->{'type'} eq 'SERVICE ALERT'
or $data->{'type'} eq 'CURRENT SERVICE STATE'
or $data->{'type'} eq 'INITIAL SERVICE STATE'
) {
$data->{'host_name'} = $self->_strtok($string, ';');
$data->{'service_description'} = $self->_strtok($string, ';');
$data->{'state'} = $self->_statestr_to_state($self->_strtok($string, ';'));
$data->{'hard'} = $self->_softstr_to_hard($self->_strtok($string, ';'));
$self->_strtok($string, ';');
$data->{'plugin_output'} = $self->_strtok($string, ';');
}
# Host Downtimes
elsif($data->{'type'} eq 'HOST DOWNTIME ALERT') {
$data->{'host_name'} = $self->_strtok($string, ';');
$data->{'start'} = $self->_startstr_to_start($self->_strtok($string, ';'));
}
# Service Downtimes
elsif($data->{'type'} eq 'SERVICE DOWNTIME ALERT') {
$data->{'host_name'} = $self->_strtok($string, ';');
$data->{'service_description'} = $self->_strtok($string, ';');
$data->{'start'} = $self->_startstr_to_start($self->_strtok($string, ';'));
}
return 1;
}
########################################
sub _set_from_type {
my $self = shift;
my $data = shift;
my $string = shift;
# program starts
if($data->{'type'} =~ m/\ starting\.\.\./mx) {
$data->{'proc_start'} = START_NORMAL;
}
elsif($data->{'type'} =~ m/\ restarting\.\.\./mx) {
$data->{'proc_start'} = START_RESTART;
}
# program stops
elsif($data->{'type'} =~ m/shutting\ down\.\.\./mx) {
$data->{'proc_start'} = STOP_NORMAL;
}
elsif($data->{'type'} =~ m/Bailing\ out/mx) {
$data->{'proc_start'} = STOP_ERROR;
}
return 1;
}
########################################
sub _startstr_to_start {
my $self = shift;
my $string = shift;
return 1 if $string eq 'STARTED';
return 0;
}
########################################
sub _softstr_to_hard {
my $self = shift;
my $string = shift;
return 1 if $string eq 'HARD';
return 0;
}
########################################
sub _statestr_to_state {
my $self = shift;
my $string = shift;
return 0 if $string eq 'UP';
return 0 if $string eq 'OK';
return 1 if $string eq 'WARNING';
return 1 if $string eq 'DOWN';
return 2 if $string eq 'CRITICAL';
return 2 if $string eq 'UNREACHABLE';
return 3 if $string eq 'UNKNOWN';
return 0 if $string eq 'RECOVERY';
confess("unknown state: $string");
}
########################################
1;
=head1 AUTHOR
Sven Nierlein, E<lt>nierlein@cpan.orgE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2010 by Sven Nierlein
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
__END__