Availability/Logs.pm

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