Availability.pm

filter: Statementstmt 79.4% 550 / 692 Branchbran 64.0% 288 / 450 Conditioncond 66.4% 89 / 134 MC/DCmcdc 41.5% 32 / 77 Subroutinesub 89.4% 34 / 38 Podpod 100% 4 / 4 totaltotal 71.4% 997 / 1395 CC 296 SCAR 38.5
CC
296
Cov
73%
_process_log_line
112.0
_verify_options
81.5
_calculate_log
51.6
CRAP 47.2
↑ Summary Availability/Logs.pm »
1 package Monitoring::Availability;
2  
3 17 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use POSIX qw(strftime mktime);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Monitoring::Availability::Logs;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 our $VERSION = '0.46';
12  
13  
14 =head1 NAME
15  
16 Monitoring::Availability - Calculate Availability Data from
17 Nagios / Icinga and Shinken Logfiles.
18  
19 =head1 SYNOPSIS
20  
21 use Monitoring::Availability;
22 my $ma = Monitoring::Availability->new();
23  
24 =head1 DESCRIPTION
25  
26 This module calculates the availability for hosts/server from given logfiles.
27 The Logfileformat is Nagios/Icinga only.
28  
29 =head1 REPOSITORY
30  
31 Git: http://github.com/sni/Monitoring-Availability
32  
33 =head1 CONSTRUCTOR
34  
35 =head2 new ( [ARGS] )
36  
37 Creates an C<Monitoring::Availability> object. C<new> takes at least the
38 logs parameter. Arguments are in key-value pairs.
39  
40 =over 4
41  
42 =item rpttimeperiod
43  
44 report timeperiod. defines a timeperiod for this report. Will use 24x7 if not
45 specified.
46  
47 =item assumeinitialstates
48  
49 Assume the initial host/service state if none is found, default: yes
50  
51 =item assumestateretention
52  
53 Assume state retention, default: yes
54  
55 =item assumestatesduringnotrunning
56  
57 Assume state during times when the monitoring process is not running, default: yes
58  
59 =item includesoftstates
60  
61 Include soft states in the calculation. Only hard states are used otherwise, default: no
62  
63 =item initialassumedhoststate
64  
65 Assumed host state if none is found, default: unspecified
66  
67 valid options are: unspecified, current, up, down and unreachable
68  
69 =item initialassumedservicestate
70  
71 Assumed service state if none is found, default: unspecified
72  
73 valid options are: unspecified, current, ok, warning, unknown and critical
74  
75 =item backtrack
76  
77 Go back this amount of days to find initial states, default: 4
78  
79 =item showscheduleddowntime
80  
81 Include downtimes in calculation, default: yes
82  
83 =item timeformat
84  
85 Time format for the log output, default: %s
86  
87 =item verbose
88  
89 verbose mode
90  
91 =item breakdown
92  
93 Breakdown availability into 'months', 'weeks', 'days', 'none'
94  
95 adds additional 'breakdown' hash to each result with broken down results
96  
97 =back
98  
99 =cut
100  
101 17 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
102 17 TRUE => 1,
103 FALSE => 0,
104  
105 STATE_NOT_RUNNING => -3,
106 STATE_UNSPECIFIED => -2,
107 STATE_CURRENT => -1,
108  
109 STATE_UP => 0,
110 STATE_DOWN => 1,
111 STATE_UNREACHABLE => 2,
112  
113 STATE_OK => 0,
114 STATE_WARNING => 1,
115 STATE_CRITICAL => 2,
116 STATE_UNKNOWN => 3,
117  
118 START_NORMAL => 1,
119 START_RESTART => 2,
120 STOP_NORMAL => 0,
121 STOP_ERROR => -1,
122  
123 HOST_ONLY => 2,
124 SERVICE_ONLY => 3,
125  
126 BREAK_NONE => 0,
127 BREAK_DAYS => 1,
128 BREAK_WEEKS => 2,
129 BREAK_MONTHS => 3,
130 17 };
131  
132 17 my $verbose = 0;
133 17 my $report_options_start;
134 my $report_options_end;
135 17 my $report_options_includesoftstates;
136 17 my $report_options_calc_all;
137  
138 18 ▶sub new {
Subroutine new: called CC=8 SCAR=24.5
Pod documented
139 18 my $class = shift;
140 18 my(%options) = @_;
141  
142 18 my $self = {
143 'verbose' => 0, # enable verbose output
144 'logger' => undef, # logger object used for verbose output
145 'timeformat' => undef,
146 'rpttimeperiod' => undef,
147 'assumeinitialstates' => undef,
148 'assumestateretention' => undef,
149 'assumestatesduringnotrunning' => undef,
150 'includesoftstates' => undef,
151 'initialassumedhoststate' => undef,
152 'initialassumedservicestate' => undef,
153 'backtrack' => undef,
154 'showscheduleddowntime' => undef,
155 'breakdown' => undef,
156 };
157  
158 18 bless $self, $class;
159  
160 # verify the options we got so far
161 18 $self = $self->_verify_options($self);
162  
163 18 for my $opt_key (keys %options) {
164 124 ▶ if(exists $self->{$opt_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$opt_key}) { } 124 0 124
165 124 $self->{$opt_key} = $options{$opt_key};
166 }
167 else {
168 0 croak("unknown option: $opt_key");
169 }
170 }
171  
172 # translation hash
173 18 $self->{'state_string_2_int'} = {
174 'ok' => STATE_OK,
175 'warning' => STATE_WARNING,
176 'unknown' => STATE_UNKNOWN,
177 'critical' => STATE_CRITICAL,
178 'up' => STATE_UP,
179 'down' => STATE_DOWN,
180 'unreachable' => STATE_UNREACHABLE,
181 '0' => STATE_OK,
182 '1' => STATE_WARNING,
183 '2' => STATE_CRITICAL,
184 '3' => STATE_UNKNOWN,
185 };
186  
187 # allow setting debug mode from env
188 18 ▶ if(defined $ENV{'MONITORING_AVAILABILITY_DEBUG'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"MONITORING_AVAILABILITY_DEBUG"}) 0 18 18
189 0 $self->{'verbose'} = 1;
190 }
191  
192 # init log4perl, may require additional modules
193 18 ▶ if($self->{'verbose'} and !defined $self->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"} and not defined $self->{"logger"}) 0 18 18
Condition33%1/3
$self->{"verbose"} and not defined $self->{"logger"}
!ll&&!rl&&r
1800
MC/DC0%0/2
$self->{"verbose"} and not defined $self->{"logger"}
$self->{"verbose"} not defined $self->{"logger"}
Truth table33%1/3
A and not(B)
A= $self->{"verbose"}B= not defined $self->{"logger"}
ABresult
0X0
100
111
194 0 require Log::Log4perl;
195 0 Log::Log4perl->import(qw(:easy));
196 0 Log::Log4perl->easy_init({
197 level => 'DEBUG',
198 file => ">/tmp/Monitoring-Availability-Debug.log"
199 });
200 0 $self->{'logger'} = get_logger();
201 }
202  
203 18 ▶ $self->_log('initialized '.$class) if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 18 18
204 18 ▶ $self->_log($self) if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 18 18
205  
206 18 $verbose = $self->{'verbose'};
207  
208 18 return $self;
209 }
210  
211  
212 ########################################
213  
214 =head1 METHODS
215  
216 =head2 calculate
217  
218 calculate()
219  
220 Calculate the availability
221  
222 =over 4
223  
224 =item start
225  
226 Timestamp of start
227  
228 =item end
229  
230 Timestamp of end
231  
232 =item log_string
233  
234 String containing the logs
235  
236 =item log_file
237  
238 File containing the logs
239  
240 =item log_dir
241  
242 Directory containing *.log files
243  
244 =item log_livestatus
245  
246 Array with logs from a livestatus query
247  
248 a sample query could be:
249 selectall_arrayref(GET logs...\nColumns: time type options, {Slice => 1})
250  
251 =item log_iterator
252  
253 Iterator object for logentry objects. For example a L<MongoDB::Cursor> object.
254  
255 =item hosts
256  
257 array with hostnames for which the report should be generated
258  
259 =item services
260  
261 array with hashes of services for which the report should be generated.
262 The array should look like this:
263  
264 [{host => 'hostname', service => 'description'}, ...]
265  
266 =item initial_states
267  
268 if you use the "current" option for initialassumedservicestate or initialassumedhoststate you
269 have to provide the current states with a hash like this:
270  
271 {
272 hosts => {
273 'hostname' => 'ok',
274 ...
275 },
276 services => {
277 'hostname' => {
278 'description' => 'warning',
279 ...
280 }
281 }
282 }
283  
284 valid values for hosts are: up, down and unreachable
285  
286 valid values for services are: ok, warning, unknown and critical
287  
288 =back
289  
290 =cut
291  
292 20 ▶sub calculate {
Subroutine calculate: called CC=25 SCAR=37.5
Pod documented
293 20 my $self = shift;
294 20 my(%opts) = @_;
295  
296 # clean up namespace
297 20 $self->_reset();
298  
299 $self->{'report_options'} = {
300 'start' => undef,
301 'end' => undef,
302 'hosts' => [],
303 'services' => [],
304 'initial_states' => {},
305 'log_string' => undef, # logs from string
306 'log_livestatus' => undef, # logs from a livestatus query
307 'log_file' => undef, # logs from a file
308 'log_dir' => undef, # logs from a dir
309 'log_iterator' => undef, # logs from a iterator object
310 'rpttimeperiod' => $self->{'rpttimeperiod'} || '',
311 'assumeinitialstates' => $self->{'assumeinitialstates'},
312 'assumestateretention' => $self->{'assumestateretention'},
313 'assumestatesduringnotrunning' => $self->{'assumestatesduringnotrunning'},
314 'includesoftstates' => $self->{'includesoftstates'},
315 'initialassumedhoststate' => $self->{'initialassumedhoststate'},
316 'initialassumedservicestate' => $self->{'initialassumedservicestate'},
317 'backtrack' => $self->{'backtrack'},
318 'showscheduleddowntime' => $self->{'showscheduleddowntime'},
319 'timeformat' => $self->{'timeformat'},
320 20 ▶ 'breakdown' => $self->{'breakdown'},
Condition100%2/2
$self->{'rpttimeperiod'} || ''
l!l
119
MC/DC100%1/1
$self->{'rpttimeperiod'} || ''
$self->{'rpttimeperiod'}
Truth table100%2/2
A
A= $self->{'rpttimeperiod'}
Aresult
11
00
321 };
322 20 ▶ $self->_log('calculate()') if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 20 20
323 20 ▶ $self->_log($self->{'report_options'}) if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 20 20
324 20 my $result;
325  
326 20 for my $opt_key (keys %opts) {
327 86 ▶ if(exists $self->{'report_options'}->{$opt_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'report_options'}{$opt_key}) { } 86 0 86
328 86 $self->{'report_options'}->{$opt_key} = $opts{$opt_key};
329 }
330 else {
331 0 croak("unknown option: $opt_key");
332 }
333 }
334 20 $verbose = $self->{'verbose'};
335  
336 20 $self->{'report_options'} = $self->_set_default_options($self->{'report_options'});
337 20 $self->{'report_options'} = $self->_verify_options($self->{'report_options'});
338  
339 # create lookup hash for faster access
340 20 $result->{'hosts'} = {};
341 20 $result->{'services'} = {};
342 20 for my $host (@{$self->{'report_options'}->{'hosts'}}) {
343 8 $result->{'hosts'}->{$host} = 1;
344 }
345 20 for my $service (@{$self->{'report_options'}->{'services'}}) {
346 32 ▶ if(ref $service ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $service ne "HASH") 0 32 32
347 0 croak("services have to be an array of hashes, for example: [{host => 'hostname', service => 'description'}, ...]\ngot: ".Dumper($service));
348 }
349 32 ▶ if(!defined $service->{'host'} or !defined $service->{'service'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $service->{"host"} and defined $service->{"service"}) 0 32 32
Condition33%1/3
defined $service->{"host"} and defined $service->{"service"}
!ll&&!rl&&r
0032
MC/DC0%0/2
defined $service->{"host"} and defined $service->{"service"}
defined $service->{"host"} defined $service->{"service"}
Truth table33%1/3
A and B
A= defined $service->{"host"}B= defined $service->{"service"}
ABresult
0X0
100
111
350 0 croak("services have to be an array of hashes, for example: [{host => 'hostname', service => 'description'}, ...]\ngot: ".Dumper($service));
351 }
352 32 $result->{'services'}->{$service->{'host'}}->{$service->{'service'}} = 1;
353 }
354  
355 # if we have more than one host or service, we dont build up a log
356 20 ▶ if(scalar @{$self->{'report_options'}->{'hosts'}} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @{$self->{'report_options'}{'hosts'};} == 1) { } 8 12 20
elsif (scalar @{$self->{'report_options'}{'services'};} == 1) { } 12 0 12
357 8 $self->{'report_options'}->{'build_log'} = HOST_ONLY;
358 }
359 12 elsif(scalar @{$self->{'report_options'}->{'services'}} == 1) {
360 12 $self->{'report_options'}->{'build_log'} = SERVICE_ONLY;
361 }
362 else {
363 0 $self->{'report_options'}->{'build_log'} = FALSE;
364 }
365  
366 20 $self->{'report_options'}->{'calc_all'} = FALSE;
367 20 ▶ if(scalar keys %{$result->{'services'}} == 0 and scalar keys %{$result->{'hosts'}} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$result{"services"};} == 0 and scalar keys %{$$result{"hosts"};} == 0) 0 20 20
Condition66%2/3
scalar keys %{$$result{"services"};} == 0 and scalar keys %{$$result{"hosts"};} == 0
!ll&&!rl&&r
1370
MC/DC0%0/2
scalar keys %{$$result{"services"};} == 0 and scalar keys %{$$result{"hosts"};} == 0
scalar keys %{$$result{"services"};} == 0 scalar keys %{$$result{"hosts"};} == 0
Truth table66%2/3
A and B
A= scalar keys %{$$result{"services"};} == 0B= scalar keys %{$$result{"hosts"};} == 0
ABresult
0X0
100
111
368 0 ▶ $self->_log('will calculate availability for all hosts/services found') if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
369 0 $self->{'report_options'}->{'calc_all'} = TRUE;
370 }
371  
372 20 $self->_set_breakpoints();
373  
374 20 ▶ unless($self->{'report_options'}->{'calc_all'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"report_options"}{"calc_all"}) 20 0 20
375 20 $self->_set_empty_hosts($result);
376 20 $self->_set_empty_services($result);
377 }
378  
379 # set some variables for faster access
380 20 $report_options_start = $self->{'report_options'}->{'start'};
381 20 $report_options_end = $self->{'report_options'}->{'end'};
382 20 $report_options_includesoftstates = $self->{'report_options'}->{'includesoftstates'};
383 20 $report_options_calc_all = $self->{'report_options'}->{'calc_all'};
384  
385 # read in logs
386 20 ▶ if($self->{'report_options'}->{'log_file'} and !$self->{'report_options'}->{'log_string'} and !$self->{'report_options'}->{'log_dir'}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'report_options'}{'log_file'} and not $self->{'report_options'}{'log_string'} and not $self->{'report_options'}{'log_dir'}) { } 0 20 20
elsif (defined $self->{'report_options'}{'log_string'} or $self->{'report_options'}{'log_file'} or $self->{'report_options'}{'log_dir'}) { } 19 1 20
elsif (defined $self->{'report_options'}{'log_livestatus'}) { } 1 0 1
elsif (defined $self->{'report_options'}{'log_iterator'}) { } 0 0 0
Condition33%2/6
$self->{"report_options"}{"log_file"} and not $self->{"report_options"}{"log_string"} and not $self->{"report_options"}{"log_dir"}
!ll&&!rl&&r
2000
$self->{"report_options"}{"log_file"} and not $self->{"report_options"}{"log_string"}
!ll&&!rl&&r
2000
MC/DC0%0/3
$self->{"report_options"}{"log_file"} and not $self->{"report_options"}{"log_string"} and not $self->{"report_options"}{"log_dir"}
$self->{"report_options"}{"log_file"} not $self->{"report_options"}{"log_string"} not $self->{"report_options"}{"log_dir"}
Truth table25%1/4
A and not(B) and not(C)
A= $self->{"report_options"}{"log_file"}B= not $self->{"report_options"}{"log_string"}C= not $self->{"report_options"}{"log_dir"}
ABCresult
0XX0
10X0
1100
1111
387 # single file can be read line by line to save memory
388 0 $self->_compute_availability_line_by_line($result, $self->{'report_options'}->{'log_file'});
389 }
390 ▶ elsif(defined $self->{'report_options'}->{'log_string'} or $self->{'report_options'}->{'log_file'} or $self->{'report_options'}->{'log_dir'}) {
Condition66%4/6
defined $self->{"report_options"}{"log_string"} or $self->{"report_options"}{"log_file"} or $self->{"report_options"}{"log_dir"}
l!l&&r!l&&!r
1901
defined $self->{"report_options"}{"log_string"} or $self->{"report_options"}{"log_file"}
l!l&&r!l&&!r
1901
MC/DC33%1/3
defined $self->{"report_options"}{"log_string"} or $self->{"report_options"}{"log_file"} or $self->{"report_options"}{"log_dir"}
defined $self->{"report_options"}{"log_string"} $self->{"report_options"}{"log_file"} $self->{"report_options"}{"log_dir"}
Truth table50%2/4
A or B or C
A= defined $self->{"report_options"}{"log_string"}B= $self->{"report_options"}{"log_file"}C= $self->{"report_options"}{"log_dir"}
ABCresult
1XX1
01X1
0011
0000
391 my $mal = Monitoring::Availability::Logs->new(
392 'log_string' => $self->{'report_options'}->{'log_string'},
393 'log_file' => $self->{'report_options'}->{'log_file'},
394 19 'log_dir' => $self->{'report_options'}->{'log_dir'},
395 );
396 19 my $logs = $mal->get_logs();
397 19 $self->_compute_availability_from_log_store($result, $logs);
398 }
399 elsif(defined $self->{'report_options'}->{'log_livestatus'}) {
400 1 $self->_compute_availability_on_the_fly($result, $self->{'report_options'}->{'log_livestatus'});
401 }
402 elsif(defined $self->{'report_options'}->{'log_iterator'}) {
403 0 $self->_compute_availability_from_iterator($result, $self->{'report_options'}->{'log_iterator'});
404 }
405 20 return($result);
406 }
407  
408  
409 ########################################
410  
411 =head2 get_condensed_logs
412  
413 get_condensed_logs()
414  
415 returns an array of hashes with the condensed log used for this report
416  
417 =cut
418  
419 8 ▶sub get_condensed_logs {
Subroutine get_condensed_logs: called CC=3 SCAR=11.4
Pod documented
420 8 my $self = shift;
421  
422 8 ▶ return if $self->{'report_options'}->{'build_log'} == FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"report_options"}{"build_log"} == 0 0 8 8
423  
424 8 ▶ $self->_calculate_log() unless $self->{'log_output_calculated'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"log_output_calculated"} 8 0 8
425  
426 8 return $self->{'log_output'};
427 }
428  
429  
430 ########################################
431  
432 =head2 get_full_logs
433  
434 get_full_logs()
435  
436 returns an array of hashes with the full log used for this report
437  
438 =cut
439  
440 9 ▶sub get_full_logs {
Subroutine get_full_logs: called CC=3 SCAR=11.0
Pod documented
441 9 my $self = shift;
442  
443 9 ▶ return if $self->{'report_options'}->{'build_log'} == FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"report_options"}{"build_log"} == 0 0 9 9
444  
445 9 ▶ $self->_calculate_log() unless $self->{'log_output_calculated'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"log_output_calculated"} 1 8 9
446  
447 9 return $self->{'full_log_output'};
448 }
449  
450  
451 ########################################
452 # INTERNAL SUBS
453 ########################################
454 20 ▶sub _reset {
Subroutine _reset: called CC=2 SCAR=6.9
455 20 my $self = shift;
456 20 ▶ $self->_log('_reset()') if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 20 20
457  
458 20 undef $self->{'full_log_store'};
459 20 $self->{'full_log_store'} = [];
460  
461 20 delete $self->{'first_known_state_before_report'};
462 20 delete $self->{'first_known_proc_before_report'};
463 20 delete $self->{'log_output_calculated'};
464  
465 20 delete $self->{'report_options'};
466 20 delete $self->{'full_log_output'};
467 20 delete $self->{'log_output'};
468  
469 20 return 1;
470 }
471  
472 ########################################
473 20 ▶sub _set_empty_hosts {
Subroutine _set_empty_hosts: called CC=6 SCAR=20.9
474 20 my $self = shift;
475 20 my $data = shift;
476  
477 20 my $initial_assumend_state = STATE_UNSPECIFIED;
478 20 ▶ if($self->{'report_options'}->{'assumeinitialstates'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"report_options"}{"assumeinitialstates"}) 20 0 20
479 20 $initial_assumend_state = $self->{'report_options'}->{'initialassumedhoststate'};
480 }
481  
482 20 ▶ $self->_log('_set_empty_hosts()') if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 20 20
483 20 for my $hostname (keys %{$data->{'hosts'}}) {
484 8 my $first_state = $initial_assumend_state;
485 8 ▶ if($initial_assumend_state == STATE_CURRENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initial_assumend_state == -1) 0 8 8
486 0 eval { $first_state = $self->_state_to_int($self->{'report_options'}->{'initial_states'}->{'hosts'}->{$hostname}); };
487 0 ▶ if($@) { croak("found no initial state for host '$hostname'\ngot: ".Dumper($self->{'report_options'}->{'initial_states'}).Dumper($@)); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
488 0 $self->{'report_options'}->{'first_state'} = $first_state;
489 }
490 8 $data->{'hosts'}->{$hostname} = $self->_new_host_data($self->{'report_options'}->{'breakdown'});
491 8 $self->{'host_data'}->{$hostname} = {
492 'in_downtime' => 0,
493 'last_state' => $initial_assumend_state,
494 'last_known_state' => undef,
495 'last_state_time' => 0,
496 };
497 }
498 20 return 1;
499 }
500  
501 ########################################
502 20 ▶sub _set_empty_services {
Subroutine _set_empty_services: called CC=9 SCAR=23.6
503 20 my $self = shift;
504 20 my $data = shift;
505 20 ▶ $self->_log('_set_empty_services()') if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 20 20
506  
507 20 my $initial_assumend_state = STATE_UNSPECIFIED;
508 20 my $initial_assumend_host_state = STATE_UNSPECIFIED;
509 20 ▶ if($self->{'report_options'}->{'assumeinitialstates'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"report_options"}{"assumeinitialstates"}) 20 0 20
510 20 $initial_assumend_state = $self->{'report_options'}->{'initialassumedservicestate'};
511 20 $initial_assumend_host_state = $self->{'report_options'}->{'initialassumedhoststate'};
512 }
513  
514 20 for my $hostname (keys %{$data->{'services'}}) {
515 13 for my $service_description (keys %{$data->{'services'}->{$hostname}}) {
516 32 my $first_state = $initial_assumend_state;
517 32 ▶ if($initial_assumend_state == STATE_CURRENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initial_assumend_state == -1) 1 31 32
518 1 eval { $first_state = $self->_state_to_int($self->{'report_options'}->{'initial_states'}->{'services'}->{$hostname}->{$service_description}); };
519 0 ▶ if($@) { croak("found no initial state for service '$service_description' on host '$hostname'\ngot: ".Dumper($self->{'report_options'}->{'initial_states'}).Dumper($@)); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
520 1 $self->{'report_options'}->{'first_state'} = $first_state;
521 }
522 32 $data->{'services'}->{$hostname}->{$service_description} = $self->_new_service_data($self->{'report_options'}->{'breakdown'});
523  
524 # create last service data
525 32 $self->{'service_data'}->{$hostname}->{$service_description} = {
526 'in_downtime' => 0,
527 'last_state' => $first_state,
528 'last_known_state' => undef,
529 'last_state_time' => 0,
530 };
531 }
532 13 my $first_host_state = $initial_assumend_host_state;
533 13 ▶ if($initial_assumend_host_state == STATE_CURRENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initial_assumend_host_state == -1) 0 13 13
534 0 eval { $first_host_state = $self->_state_to_int($self->{'report_options'}->{'initial_states'}->{'hosts'}->{$hostname}); };
535 0 ▶ if($@) { $first_host_state = STATE_UNSPECIFIED; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
536 }
537 13 $self->{'host_data'}->{$hostname} = {
538 'in_downtime' => 0,
539 'last_state' => $first_host_state,
540 'last_known_state' => undef,
541 'last_state_time' => 0,
542 };
543 }
544 20 return 1;
545 }
546  
547 ########################################
548 176 ▶sub _compute_for_data {
Subroutine _compute_for_data: called CC=12 SCAR=24.9
549 176 my($self, $last_time, $data, $result) = @_;
550  
551 # if we reach the start date of our report, insert a fake entry
552 176 ▶ if($last_time < $report_options_start and $data->{'time'} >= $report_options_start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_time < $report_options_start and $data->{"time"} >= $report_options_start) 20 156 176
Condition100%3/3
$last_time < $report_options_start and $data->{"time"} >= $report_options_start
!ll&&!rl&&r
149720
MC/DC100%2/2
$last_time < $report_options_start and $data->{"time"} >= $report_options_start
$last_time < $report_options_start $data->{"time"} >= $report_options_start
Truth table100%3/3
A and B
A= $last_time < $report_options_startB= $data->{"time"} >= $report_options_start
ABresult
0X0
100
111
553 20 $self->_insert_fake_event($result, $report_options_start);
554 }
555  
556 # if we passed a breakdown point, insert fake event
557 176 ▶ if($self->{'report_options'}->{'breakdown'} != BREAK_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"report_options"}{"breakdown"} != 0) 6 170 176
558 6 my $breakpoint = $self->{'breakpoints'}->[0];
559 6 ▶ while(defined $breakpoint and $last_time < $breakpoint and $data->{'time'} >= $breakpoint) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $breakpoint and $last_time < $breakpoint and $data->{"time"} >= $breakpoint) 6 6 12
Condition83%5/6
defined $breakpoint and $last_time < $breakpoint and $data->{"time"} >= $breakpoint
!ll&&!rl&&r
426
defined $breakpoint and $last_time < $breakpoint
!ll&&!rl&&r
408
MC/DC66%2/3
defined $breakpoint and $last_time < $breakpoint and $data->{"time"} >= $breakpoint
defined $breakpoint $last_time < $breakpoint $data->{"time"} >= $breakpoint
Truth table75%3/4
A and B and C
A= defined $breakpointB= $last_time < $breakpointC= $data->{"time"} >= $breakpoint
ABCresult
0XX0
10X0
1100
1111
560 6 ▶ $self->_log('_compute_for_data(): inserted breakpoint: '.$breakpoint." (".scalar localtime($breakpoint).")") if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 6 6
561 6 $self->_insert_fake_event($result, $breakpoint);
562 6 shift(@{$self->{'breakpoints'}});
563 6 $breakpoint = $self->{'breakpoints'}->[0];
564 }
565 }
566  
567 # end of report reached, insert fake end event
568 176 ▶ if($data->{'time'} >= $report_options_end and $last_time < $report_options_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"time"} >= $report_options_end and $last_time < $report_options_end) 3 173 176
Condition66%2/3
$data->{"time"} >= $report_options_end and $last_time < $report_options_end
!ll&&!rl&&r
17303
MC/DC50%1/2
$data->{"time"} >= $report_options_end and $last_time < $report_options_end
$data->{"time"} >= $report_options_end $last_time < $report_options_end
Truth table66%2/3
A and B
A= $data->{"time"} >= $report_options_endB= $last_time < $report_options_end
ABresult
0X0
100
111
569 3 $self->_insert_fake_event($result, $report_options_end);
570  
571 # set a log entry
572 $self->_add_log_entry(
573 'full_only' => 1,
574 'log' => {
575 'start' => $report_options_end,
576 },
577 3 ▶ ) unless defined $data->{'fake'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{"fake"} 2 1 3
578 }
579  
580 # now process the real line
581 176 ▶ &_process_log_line($self,$result, $data) unless defined $data->{'fake'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{"fake"} 175 1 176
582  
583 176 return 1;
584 }
585  
586 ########################################
587 0 ▶sub _compute_availability_line_by_line {
Subroutine _compute_availability_line_by_line: not called CC=5 SCAR=34.0
588 0 my($self,$result,$file) = @_;
589  
590 0 ▶ if($self->{'verbose'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
591 0 $self->_log('_compute_availability_line_by_line()');
592 0 $self->_log('_compute_availability_line_by_line() report start: '.(scalar localtime $self->{'report_options'}->{'start'}));
593 0 $self->_log('_compute_availability_line_by_line() report end: '.(scalar localtime $self->{'report_options'}->{'end'}));
594 }
595  
596 0 my $last_time = -1;
597  
598 0 ▶ open(my $fh, '<', $file) or die("cannot read ".$file.": ".$!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die 'cannot read ' . $file . ': ' . $! 0 0 0
599 0 binmode($fh);
600  
601 # process all log lines we got
602 # logs should be sorted already
603 0 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
604 0 chop;
605 0 my $data = &Monitoring::Availability::Logs::parse_line($_);
606 0 ▶ next unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
607 0 &_compute_for_data($self,$last_time, $data, $result);
608 # set timestamp of last log line
609 0 $last_time = $data->{'time'};
610 }
611 0 close($fh);
612  
613 0 $self->_add_last_time_event($last_time, $result);
614  
615 0 return 1;
616 }
617  
618  
619 ########################################
620 0 ▶sub _compute_availability_from_iterator {
Subroutine _compute_availability_from_iterator: not called CC=4 SCAR=30.0
621 0 my $self = shift;
622 0 my $result = shift;
623 0 my $logs = shift;
624  
625 0 ▶ if($self->{'verbose'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
626 0 $self->_log('_compute_availability_from_iterator()');
627 0 $self->_log('_compute_availability_from_iterator() report start: '.(scalar localtime $self->{'report_options'}->{'start'}));
628 0 $self->_log('_compute_availability_from_iterator() report end: '.(scalar localtime $self->{'report_options'}->{'end'}));
629 }
630  
631 0 my $last_time = -1;
632 # no logs at all?
633 0 ▶ unless($logs->has_next) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($logs->has_next) 0 0 0
634 $self->_compute_for_data(-1,
635 0 {time => $self->{'report_options'}->{'end'}, fake => 1},
636 $result);
637 0 $last_time = $self->{'report_options'}->{'end'};
638 }
639  
640 # process all log lines we got
641 # logs should be sorted already
642 0 ▶ while(my $data = $logs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $logs->next) 0 0 0
643 0 $self->_compute_for_data($last_time,
644 Monitoring::Availability::Logs->_parse_livestatus_entry($data),
645 $result);
646  
647 # set timestamp of last log line
648 0 $last_time = $data->{'time'};
649 }
650  
651 # processing logfiles finished
652  
653 0 $self->_add_last_time_event($last_time, $result);
654  
655 0 return 1;
656 }
657  
658  
659 ########################################
660 1 ▶sub _compute_availability_on_the_fly {
Subroutine _compute_availability_on_the_fly: called CC=4 SCAR=15.8
661 1 my $self = shift;
662 1 my $result = shift;
663 1 my $logs = shift;
664  
665 1 ▶ if($self->{'verbose'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 1 1
666 0 $self->_log('_compute_availability_on_the_fly()');
667 0 $self->_log('_compute_availability_on_the_fly() report start: '.(scalar localtime $self->{'report_options'}->{'start'}));
668 0 $self->_log('_compute_availability_on_the_fly() report end: '.(scalar localtime $self->{'report_options'}->{'end'}));
669 }
670  
671 1 my $last_time = -1;
672 1 ▶ if(scalar @{$logs} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$logs;} == 0) 1 0 1
673 $self->_compute_for_data(-1,
674 1 {time => $self->{'report_options'}->{'end'}, fake => 1},
675 $result);
676 1 $last_time = $self->{'report_options'}->{'end'};
677 }
678  
679 # process all log lines we got
680 # make sure our logs are sorted by time
681 0 for my $data ( sort { $a->{'time'} <=> $b->{'time'} } @{$logs} ) {
682 0 eval {
683 0 $self->_compute_for_data($last_time,
684 Monitoring::Availability::Logs->_parse_livestatus_entry($data),
685 $result);
686  
687 # set timestamp of last log line
688 0 $last_time = $data->{'time'};
689 }
690 }
691  
692 # processing logfiles finished
693  
694 1 $self->_add_last_time_event($last_time, $result);
695  
696 1 return 1;
697 }
698  
699  
700 ########################################
701 19 ▶sub _compute_availability_from_log_store {
Subroutine _compute_availability_from_log_store: called CC=4 SCAR=14.3
702 19 my $self = shift;
703 19 my $result = shift;
704 19 my $logs = shift;
705  
706 19 ▶ if($self->{'verbose'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 19 19
707 0 $self->_log('_compute_availability_from_log_store()');
708 0 $self->_log('_compute_availability_from_log_store() report start: '.(scalar localtime $self->{'report_options'}->{'start'}));
709 0 $self->_log('_compute_availability_from_log_store() report end: '.(scalar localtime $self->{'report_options'}->{'end'}));
710 }
711  
712 # make sure our logs are sorted by time
713 19 @{$logs} = sort { $a->{'time'} <=> $b->{'time'} } @{$logs};
714  
715 19 ▶ $self->_log('_compute_availability_from_log_store() sorted logs') if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 19 19
716  
717 # process all log lines we got
718 19 my $last_time = -1;
719 19 for my $data (@{$logs}) {
720  
721 175 $self->_compute_for_data($last_time, $data, $result);
722  
723 # set timestamp of last log line
724 175 $last_time = $data->{'time'};
725 }
726  
727 # processing logfiles finished
728  
729 19 $self->_add_last_time_event($last_time, $result);
730  
731 19 return 1;
732 }
733  
734  
735 ########################################
736 20 ▶sub _add_last_time_event {
Subroutine _add_last_time_event: called CC=5 SCAR=20.4
737 20 my($self, $last_time, $result) = @_;
738  
739 # no start event yet, insert a fake entry
740 20 ▶ if($last_time < $self->{'report_options'}->{'start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_time < $self->{"report_options"}{"start"}) 0 20 20
741 0 $self->_insert_fake_event($result, $self->{'report_options'}->{'start'});
742 }
743  
744 # breakpoints left?
745 20 my $breakpoint = $self->{'breakpoints'}->[0];
746 20 ▶ while(defined $breakpoint) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $breakpoint) 0 20 20
747 0 ▶ $self->_log('_add_last_time_event(): inserted breakpoint: '.$breakpoint." (".scalar localtime($breakpoint).")") if $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
748 0 $self->_insert_fake_event($result, $breakpoint);
749 0 shift(@{$self->{'breakpoints'}});
750 0 $breakpoint = $self->{'breakpoints'}->[0];
751 }
752  
753  
754 # no end event yet, insert fake end event
755 20 ▶ if($last_time < $self->{'report_options'}->{'end'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_time < $self->{"report_options"}{"end"}) 17 3 20
756 17 $self->_insert_fake_event($result, $self->{'report_options'}->{'end'});
757 }
758  
759 20 return 1;
760 }
761  
762 ########################################
763 175 ▶sub _process_log_line {
Subroutine _process_log_line: called CC=79 SCAR=47.2
764 175 my($self, $result, $data) = @_;
765  
766 175 ▶ if($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 175 175
767 0 $self->_log('#######################################');
768 0 $self->_log('_process_log_line() at '.(scalar localtime $data->{'time'}));
769 0 $self->_log($data);
770 }
771  
772 # only hard states?
773 175 ▶ if(exists $data->{'hard'} and !$report_options_includesoftstates and $data->{'hard'} != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"hard"} and not $report_options_includesoftstates and $data->{"hard"} != 1) 2 173 175
Condition83%5/6
exists $data->{"hard"} and not $report_options_includesoftstates and $data->{"hard"} != 1
!ll&&!rl&&r
117562
exists $data->{"hard"} and not $report_options_includesoftstates
!ll&&!rl&&r
117058
MC/DC66%2/3
exists $data->{"hard"} and not $report_options_includesoftstates and $data->{"hard"} != 1
exists $data->{"hard"} not $report_options_includesoftstates $data->{"hard"} != 1
Truth table75%3/4
A and not(B) and C
A= exists $data->{"hard"}B= not $report_options_includesoftstatesC= $data->{"hard"} != 1
ABCresult
0XX0
10X0
1100
1111
774 2 ▶ $self->_log(' -> skipped soft state') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
775 2 return;
776 }
777  
778 # process starts / stops?
779 173 ▶ if(defined $data->{'proc_start'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $data->{'proc_start'}) { } 57 116 173
elsif (defined $data->{'timeperiod'}) { } 6 110 116
780 57 ▶ unless($self->{'report_options'}->{'assumestatesduringnotrunning'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"report_options"}{"assumestatesduringnotrunning"}) 12 45 57
781 12 ▶ if($data->{'proc_start'} == START_NORMAL or $data->{'proc_start'} == START_RESTART) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'proc_start'} == 1 or $data->{'proc_start'} == 2) { } 10 2 12
Condition66%2/3
$data->{"proc_start"} == 1 or $data->{"proc_start"} == 2
l!l&&r!l&&!r
1002
MC/DC50%1/2
$data->{"proc_start"} == 1 or $data->{"proc_start"} == 2
$data->{"proc_start"} == 1 $data->{"proc_start"} == 2
Truth table66%2/3
A or B
A= $data->{"proc_start"} == 1B= $data->{"proc_start"} == 2
ABresult
1X1
011
000
782 # set an event for all services and set state to no_data
783 10 ▶ $self->_log('_process_log_line() process start, inserting fake event for all services') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 10 10
784 10 for my $host_name (keys %{$self->{'service_data'}}) {
785 7 for my $service_description (keys %{$self->{'service_data'}->{$host_name}}) {
786 6 my $last_known_state = $self->{'service_data'}->{$host_name}->{$service_description}->{'last_known_state'};
787 6 my $last_state = STATE_UNSPECIFIED;
788 6 ▶ $last_state = $last_known_state if(defined $last_known_state and $last_known_state >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_known_state and $last_known_state >= 0 1 5 6
Condition66%2/3
defined $last_known_state and $last_known_state >= 0
!ll&&!rl&&r
501
MC/DC50%1/2
defined $last_known_state and $last_known_state >= 0
defined $last_known_state $last_known_state >= 0
Truth table66%2/3
A and B
A= defined $last_known_stateB= $last_known_state >= 0
ABresult
0X0
100
111
789 6 &_set_service_event($self, $host_name, $service_description, $result, { 'start' => $data->{'start'}, 'end' => $data->{'end'}, 'time' => $data->{'time'}, 'state' => $last_state });
790 }
791 }
792 10 for my $host_name (keys %{$self->{'host_data'}}) {
793 10 my $last_known_state = $self->{'host_data'}->{$host_name}->{'last_known_state'};
794 10 my $last_state = STATE_UNSPECIFIED;
795 10 ▶ $last_state = $last_known_state if(defined $last_known_state and $last_known_state >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_known_state and $last_known_state >= 0 1 9 10
Condition66%2/3
defined $last_known_state and $last_known_state >= 0
!ll&&!rl&&r
901
MC/DC50%1/2
defined $last_known_state and $last_known_state >= 0
defined $last_known_state $last_known_state >= 0
Truth table66%2/3
A and B
A= defined $last_known_stateB= $last_known_state >= 0
ABresult
0X0
100
111
796 10 &_set_host_event($self, $host_name, $result, { 'time' => $data->{'time'}, 'state' => $last_state });
797 }
798 } else {
799 # set an event for all services and set state to not running
800 2 ▶ $self->_log('_process_log_line() process stop, inserting fake event for all services') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
801 2 for my $host_name (keys %{$self->{'service_data'}}) {
802 2 for my $service_description (keys %{$self->{'service_data'}->{$host_name}}) {
803 1 &_set_service_event($self, $host_name, $service_description, $result, { 'time' => $data->{'time'}, 'state' => STATE_NOT_RUNNING });
804 }
805 }
806 2 for my $host_name (keys %{$self->{'host_data'}}) {
807 2 &_set_host_event($self, $host_name, $result, { 'time' => $data->{'time'}, 'state' => STATE_NOT_RUNNING });
808 }
809 }
810 }
811 # set a log entry
812 57 ▶ if($data->{'proc_start'} == START_NORMAL or $data->{'proc_start'} == START_RESTART) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'proc_start'} == 1 or $data->{'proc_start'} == 2) { } 39 18 57
Condition100%3/3
$data->{"proc_start"} == 1 or $data->{"proc_start"} == 2
l!l&&r!l&&!r
38118
MC/DC100%2/2
$data->{"proc_start"} == 1 or $data->{"proc_start"} == 2
$data->{"proc_start"} == 1 $data->{"proc_start"} == 2
Truth table100%3/3
A or B
A= $data->{"proc_start"} == 1B= $data->{"proc_start"} == 2
ABresult
1X1
011
000
813 39 my $plugin_output = 'Program start';
814 39 ▶ $plugin_output = 'Program restart' if $data->{'proc_start'} == START_RESTART;
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"proc_start"} == 2 1 38 39
815 $self->_add_log_entry(
816 'full_only' => 1,
817 'proc_start' => $data->{'proc_start'},
818 'log' => {
819 39 'start' => $data->{'time'},
820 'type' => 'PROGRAM (RE)START',
821 'plugin_output' => $plugin_output,
822 'class' => 'INDETERMINATE',
823 },
824 );
825 } else {
826 18 my $plugin_output = 'Normal program termination';
827 18 ▶ $plugin_output = 'Abnormal program termination' if $data->{'proc_start'} == STOP_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"proc_start"} == -1 0 18 18
828 $self->_add_log_entry(
829 'full_only' => 1,
830 'log' => {
831 18 'start' => $data->{'time'},
832 'type' => 'PROGRAM END',
833 'plugin_output' => $plugin_output,
834 'class' => 'INDETERMINATE',
835 },
836 );
837 }
838 57 return;
839 }
840  
841 # timeperiod transitions
842 elsif(defined $data->{'timeperiod'}) {
843 6 ▶ if($self->{'report_options'}->{'rpttimeperiod'} eq $data->{'timeperiod'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"report_options"}{"rpttimeperiod"} eq $data->{"timeperiod"}) 3 3 6
844 3 ▶ $self->_log('_process_log_line() timeperiod translation, inserting fake event for all hosts/services') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 3 3
845 3 for my $host_name (keys %{$self->{'service_data'}}) {
846 3 for my $service_description (keys %{$self->{'service_data'}->{$host_name}}) {
847 3 my $last_known_state = $self->{'service_data'}->{$host_name}->{$service_description}->{'last_known_state'};
848 3 my $last_state = STATE_UNSPECIFIED;
849 3 ▶ $last_state = $last_known_state if(defined $last_known_state and $last_known_state >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_known_state and $last_known_state >= 0 3 0 3
Condition33%1/3
defined $last_known_state and $last_known_state >= 0
!ll&&!rl&&r
003
MC/DC0%0/2
defined $last_known_state and $last_known_state >= 0
defined $last_known_state $last_known_state >= 0
Truth table33%1/3
A and B
A= defined $last_known_stateB= $last_known_state >= 0
ABresult
0X0
100
111
850 3 &_set_service_event($self, $host_name, $service_description, $result, { 'start' => $data->{'start'}, 'end' => $data->{'end'}, 'time' => $data->{'time'}, 'state' => $last_state });
851 }
852 }
853 3 for my $host_name (keys %{$self->{'host_data'}}) {
854 3 my $last_known_state = $self->{'host_data'}->{$host_name}->{'last_known_state'};
855 3 my $last_state = STATE_UNSPECIFIED;
856 3 ▶ $last_state = $last_known_state if(defined $last_known_state and $last_known_state >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_known_state and $last_known_state >= 0 0 3 3
Condition33%1/3
defined $last_known_state and $last_known_state >= 0
!ll&&!rl&&r
300
MC/DC0%0/2
defined $last_known_state and $last_known_state >= 0
defined $last_known_state $last_known_state >= 0
Truth table33%1/3
A and B
A= defined $last_known_stateB= $last_known_state >= 0
ABresult
0X0
100
111
857 3 &_set_host_event($self,$host_name, $result, { 'time' => $data->{'time'}, 'state' => $last_state });
858 }
859 3 $self->{'in_timeperiod'} = $data->{'to'};
860  
861 # set a log entry
862 3 my $start = 'STOP';
863 3 my $plugin_output = 'leaving timeperiod: '.$data->{'timeperiod'};
864 3 ▶ if($self->{'in_timeperiod'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_timeperiod"}) 2 1 3
865 2 $plugin_output = 'entering timeperiod: '.$data->{'timeperiod'};
866 2 $start = 'START';
867 }
868 $self->_add_log_entry(
869 'full_only' => 1,
870 'log' => {
871 3 'start' => $data->{'time'},
872 'type' => 'TIMEPERIOD '.$start,
873 'plugin_output' => $plugin_output,
874 'class' => 'INDETERMINATE',
875 },
876 );
877 }
878 6 return;
879 }
880  
881 # skip hosts we dont need
882 110 ▶ if($report_options_calc_all == 0 and defined $data->{'host_name'} and !defined $self->{'host_data'}->{$data->{'host_name'}} and !defined $self->{'service_data'}->{$data->{'host_name'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($report_options_calc_all == 0 and defined $data->{"host_name"} and not defined $self->{"host_data"}{$data->{"host_name"}} and not defined $self->{"service_data"}{$data->{"host_name"}}) 2 108 110
Condition77%7/9
$report_options_calc_all == 0 and defined $data->{"host_name"} and not defined $self->{"host_data"}{$data->{"host_name"}} and not defined $self->{"service_data"}{$data->{"host_name"}}
!ll&&!rl&&r
10802
$report_options_calc_all == 0 and defined $data->{"host_name"} and not defined $self->{"host_data"}{$data->{"host_name"}}
!ll&&!rl&&r
45632
$report_options_calc_all == 0 and defined $data->{"host_name"}
!ll&&!rl&&r
04565
MC/DC50%2/4
$report_options_calc_all == 0 and defined $data->{"host_name"} and not defined $self->{"host_data"}{$data->{"host_name"}} and not defined $self->{"service_data"}{$data->{"host_name"}}
$report_options_calc_all == 0 defined $data->{"host_name"} not defined $self->{"host_data"}{$data->{"host_name"}} not defined $self->{"service_data"}{$data->{"host_name"}}
Truth table60%3/5
A and B and not(C) and not(D)
A= $report_options_calc_all == 0B= defined $data->{"host_name"}C= not defined $self->{"host_data"}{$data->{"host_name"}}D= not defined $self->{"service_data"}{$data->{"host_name"}}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
883 2 ▶ $self->_log(' -> skipped not needed host event') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
884 2 return;
885 }
886  
887 # skip services we dont need
888 108 ▶ if($report_options_calc_all == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($report_options_calc_all == 0 and $data->{"host_name"} and $data->{"service_description"} and not defined $self->{"service_data"}{$data->{"host_name"}}{$data->{"service_description"}}) 0 108 108
Condition77%7/9
$report_options_calc_all == 0 and $data->{"host_name"} and $data->{"service_description"} and not defined $self->{"service_data"}{$data->{"host_name"}}{$data->{"service_description"}}
!ll&&!rl&&r
61470
$report_options_calc_all == 0 and $data->{"host_name"} and $data->{"service_description"}
!ll&&!rl&&r
451647
$report_options_calc_all == 0 and $data->{"host_name"}
!ll&&!rl&&r
04563
MC/DC0%0/4
$report_options_calc_all == 0 and $data->{"host_name"} and $data->{"service_description"} and not defined $self->{"service_data"}{$data->{"host_name"}}{$data->{"service_description"}}
$report_options_calc_all == 0 $data->{"host_name"} $data->{"service_description"} not defined $self->{"service_data"}{$data->{"host_name"}}{$data->{"service_description"}}
Truth table60%3/5
A and B and C and not(D)
A= $report_options_calc_all == 0B= $data->{"host_name"}C= $data->{"service_description"}D= not defined $self->{"service_data"}{$data->{"host_name"}}{$data->{"service_description"}}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
889 and $data->{'host_name'}
890 and $data->{'service_description'}
891 and !defined $self->{'service_data'}->{$data->{'host_name'}}->{$data->{'service_description'}}
892 ) {
893 0 ▶ $self->_log(' -> skipped not needed service event') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
894 0 return;
895 }
896  
897 # service events
898 108 ▶ if($data->{'service_description'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data->{'service_description'}) { } 47 61 108
elsif (defined $data->{'host_name'}) { } 16 45 61
899 47 my $service_hist = $self->{'service_data'}->{$data->{'host_name'}}->{$data->{'service_description'}};
900  
901 47 ▶ if($data->{'type'} eq 'CURRENT SERVICE STATE' or $data->{'type'} eq 'SERVICE ALERT' or $data->{'type'} eq 'INITIAL SERVICE STATE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($data->{'type'} eq 'CURRENT SERVICE STATE' or $data->{'type'} eq 'SERVICE ALERT' or $data->{'type'} eq 'INITIAL SERVICE STATE') { } 45 2 47
elsif ($data->{'type'} eq 'SERVICE DOWNTIME ALERT') { } 2 0 2
Condition83%5/6
$data->{"type"} eq "CURRENT SERVICE STATE" or $data->{"type"} eq "SERVICE ALERT" or $data->{"type"} eq "INITIAL SERVICE STATE"
l!l&&r!l&&!r
4502
$data->{"type"} eq "CURRENT SERVICE STATE" or $data->{"type"} eq "SERVICE ALERT"
l!l&&r!l&&!r
3962
MC/DC66%2/3
$data->{"type"} eq "CURRENT SERVICE STATE" or $data->{"type"} eq "SERVICE ALERT" or $data->{"type"} eq "INITIAL SERVICE STATE"
$data->{"type"} eq "CURRENT SERVICE STATE" $data->{"type"} eq "SERVICE ALERT" $data->{"type"} eq "INITIAL SERVICE STATE"
Truth table75%3/4
A or B or C
A= $data->{"type"} eq "CURRENT SERVICE STATE"B= $data->{"type"} eq "SERVICE ALERT"C= $data->{"type"} eq "INITIAL SERVICE STATE"
ABCresult
1XX1
01X1
0011
0000
902 45 &_set_service_event($self,$data->{'host_name'}, $data->{'service_description'}, $result, $data);
903  
904 # set a log entry
905 45 my $state_text;
906 45 ▶ if( $data->{'state'} == STATE_OK ) { $state_text = "OK"; }
Branch: 4 branches
BranchTrueFalseTotal
if ($data->{'state'} == 0) { } 35 10 45
elsif ($data->{'state'} == 1) { } 3 7 10
elsif ($data->{'state'} == 3) { } 3 4 7
elsif ($data->{'state'} == 2) { } 4 0 4
907 3 elsif($data->{'state'} == STATE_WARNING) { $state_text = "WARNING"; }
908 3 elsif($data->{'state'} == STATE_UNKNOWN) { $state_text = "UNKNOWN"; }
909 4 elsif($data->{'state'} == STATE_CRITICAL) { $state_text = "CRITICAL"; }
910 45 ▶ if(defined $state_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state_text) 45 0 45
911 45 my $hard = "";
912 45 ▶ $hard = " (HARD)" if $data->{'hard'};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"hard"} 45 0 45
913 $self->_add_log_entry(
914 'log' => {
915 'start' => $data->{'time'},
916 'type' => 'SERVICE '.$state_text.$hard,
917 'plugin_output' => $data->{'plugin_output'},
918 'class' => $state_text,
919 'in_downtime' => $service_hist->{'in_downtime'},
920 },
921 45 ▶ ) unless $self->{'report_options'}->{'build_log'} == HOST_ONLY;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"report_options"}{"build_log"} == 2 25 20 45
922 }
923 }
924 elsif($data->{'type'} eq 'SERVICE DOWNTIME ALERT') {
925 2 ▶ next unless $self->{'report_options'}->{'showscheduleddowntime'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"report_options"}{"showscheduleddowntime"} 0 2 2
926  
927 2 undef $data->{'state'}; # we dont know the current state, so make sure it wont be overwritten
928 2 &_set_service_event($self,$data->{'host_name'}, $data->{'service_description'}, $result, $data);
929  
930 2 my $start;
931 my $plugin_output;
932 2 ▶ if($data->{'start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'start'}) { } 1 1 2
933 1 $start = "START";
934 1 $plugin_output = 'Start of scheduled downtime';
935 1 $service_hist->{'in_downtime'} = 1;
936 }
937 else {
938 1 $start = "END";
939 1 $plugin_output = 'End of scheduled downtime';
940 1 $service_hist->{'in_downtime'} = 0;
941 }
942  
943 # set a log entry
944 $self->_add_log_entry(
945 'log' => {
946 'start' => $data->{'time'},
947 'type' => 'SERVICE DOWNTIME '.$start,
948 'plugin_output' => $plugin_output,
949 'class' => 'INDETERMINATE',
950 'in_downtime' => $service_hist->{'in_downtime'},
951 },
952 2 ▶ ) unless $self->{'report_options'}->{'build_log'} == HOST_ONLY;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"report_options"}{"build_log"} == 2 2 0 2
953 }
954 else {
955 0 ▶ $self->_log(' -> unknown log type') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
956 }
957 }
958  
959 # host events
960 elsif(defined $data->{'host_name'}) {
961 16 my $host_hist = $self->{'host_data'}->{$data->{'host_name'}};
962  
963 16 ▶ if($data->{'type'} eq 'CURRENT HOST STATE' or $data->{'type'} eq 'HOST ALERT' or $data->{'type'} eq 'INITIAL HOST STATE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($data->{'type'} eq 'CURRENT HOST STATE' or $data->{'type'} eq 'HOST ALERT' or $data->{'type'} eq 'INITIAL HOST STATE') { } 11 5 16
elsif ($data->{'type'} eq 'HOST DOWNTIME ALERT') { } 5 0 5
Condition83%5/6
$data->{"type"} eq "CURRENT HOST STATE" or $data->{"type"} eq "HOST ALERT" or $data->{"type"} eq "INITIAL HOST STATE"
l!l&&r!l&&!r
1105
$data->{"type"} eq "CURRENT HOST STATE" or $data->{"type"} eq "HOST ALERT"
l!l&&r!l&&!r
745
MC/DC66%2/3
$data->{"type"} eq "CURRENT HOST STATE" or $data->{"type"} eq "HOST ALERT" or $data->{"type"} eq "INITIAL HOST STATE"
$data->{"type"} eq "CURRENT HOST STATE" $data->{"type"} eq "HOST ALERT" $data->{"type"} eq "INITIAL HOST STATE"
Truth table75%3/4
A or B or C
A= $data->{"type"} eq "CURRENT HOST STATE"B= $data->{"type"} eq "HOST ALERT"C= $data->{"type"} eq "INITIAL HOST STATE"
ABCresult
1XX1
01X1
0011
0000
964 11 &_set_host_event($self,$data->{'host_name'}, $result, $data);
965  
966 # set a log entry
967 11 my $state_text;
968 11 ▶ if( $data->{'state'} == STATE_UP) { $state_text = "UP"; }
Branch: 3 branches
BranchTrueFalseTotal
if ($data->{'state'} == 0) { } 7 4 11
elsif ($data->{'state'} == 1) { } 3 1 4
elsif ($data->{'state'} == 2) { } 1 0 1
969 3 elsif($data->{'state'} == STATE_DOWN) { $state_text = "DOWN"; }
970 1 elsif($data->{'state'} == STATE_UNREACHABLE) { $state_text = "UNREACHABLE"; }
971 11 ▶ if(defined $state_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state_text) 11 0 11
972 11 my $hard = "";
973 11 ▶ $hard = " (HARD)" if $data->{'hard'};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"hard"} 11 0 11
974 $self->_add_log_entry(
975 'log' => {
976 'start' => $data->{'time'},
977 'type' => 'HOST '.$state_text.$hard,
978 'plugin_output' => $data->{'plugin_output'},
979 'class' => $state_text,
980 11 'in_downtime' => $host_hist->{'in_downtime'},
981 },
982 );
983 }
984 }
985 elsif($data->{'type'} eq 'HOST DOWNTIME ALERT') {
986 5 ▶ next unless $self->{'report_options'}->{'showscheduleddowntime'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"report_options"}{"showscheduleddowntime"} 0 5 5
987  
988 5 my $last_state_time = $host_hist->{'last_state_time'};
989  
990 5 ▶ $self->_log('_process_log_line() hostdowntime, inserting fake event for all hosts/services') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 5 5
991 # set an event for all services
992 5 for my $service_description (keys %{$self->{'service_data'}->{$data->{'host_name'}}}) {
993 3 $last_state_time = $self->{'service_data'}->{$data->{'host_name'}}->{$service_description}->{'last_state_time'};
994 3 &_set_service_event($self, $data->{'host_name'}, $service_description, $result, { 'start' => $data->{'start'}, 'end' => $data->{'end'}, 'time' => $data->{'time'} });
995 }
996  
997 5 undef $data->{'state'}; # we dont know the current state, so make sure it wont be overwritten
998  
999 # set the host event itself
1000 5 &_set_host_event($self,$data->{'host_name'}, $result, $data);
1001  
1002 5 my $start;
1003 my $plugin_output;
1004 5 ▶ if($data->{'start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'start'}) { } 3 2 5
1005 3 $start = "START";
1006 3 $plugin_output = 'Start of scheduled downtime';
1007 3 $host_hist->{'in_downtime'} = 1;
1008 }
1009 else {
1010 2 $start = "STOP";
1011 2 $plugin_output = 'End of scheduled downtime';
1012 2 $host_hist->{'in_downtime'} = 0;
1013 }
1014  
1015 # set a log entry
1016 $self->_add_log_entry(
1017 'log' => {
1018 'start' => $data->{'time'},
1019 'type' => 'HOST DOWNTIME '.$start,
1020 'plugin_output' => $plugin_output,
1021 'class' => 'INDETERMINATE',
1022 5 'in_downtime' => $host_hist->{'in_downtime'},
1023 },
1024 );
1025 }
1026 else {
1027 0 ▶ $self->_log(' -> unknown log type') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
1028 }
1029 }
1030 else {
1031 45 ▶ $self->_log(' -> unknown log type') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 45 45
1032 }
1033  
1034 108 return 1;
1035 }
1036  
1037  
1038 ########################################
1039 124 ▶sub _set_service_event {
Subroutine _set_service_event: called CC=21 SCAR=31.5
1040 124 my($self, $host_name, $service_description, $result, $data) = @_;
1041  
1042 124 ▶ $self->_log('_set_service_event()') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 124 124
1043  
1044 124 my $host_hist = $self->{'host_data'}->{$host_name};
1045 124 my $service_hist = $self->{'service_data'}->{$host_name}->{$service_description};
1046 124 my $service_data = $result->{'services'}->{$host_name}->{$service_description};
1047  
1048 # check if we are inside the report time
1049 124 ▶ if($report_options_start < $data->{'time'} and $report_options_end >= $data->{'time'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($report_options_start < $data->{"time"} and $report_options_end >= $data->{"time"}) 86 38 124
Condition66%2/3
$report_options_start < $data->{"time"} and $report_options_end >= $data->{"time"}
!ll&&!rl&&r
38086
MC/DC50%1/2
$report_options_start < $data->{"time"} and $report_options_end >= $data->{"time"}
$report_options_start < $data->{"time"} $report_options_end >= $data->{"time"}
Truth table66%2/3
A and B
A= $report_options_start < $data->{"time"}B= $report_options_end >= $data->{"time"}
ABresult
0X0
100
111
1050 # we got a last state?
1051 86 ▶ if(defined $service_hist->{'last_state'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $service_hist->{"last_state"}) 86 0 86
1052 86 my $diff = $data->{'time'} - $service_hist->{'last_state_time'};
1053  
1054 # outside timeperiod
1055 86 ▶ if(defined $self->{'in_timeperiod'} and !$self->{'in_timeperiod'}) {
Branch: 7 branches
BranchTrueFalseTotal
if (defined $self->{'in_timeperiod'} and not $self->{'in_timeperiod'}) { } 2 84 86
elsif ($service_hist->{'last_state'} == 0) { } 39 45 84
elsif ($service_hist->{'last_state'} == 1) { } 3 42 45
elsif ($service_hist->{'last_state'} == 2) { } 5 37 42
elsif ($service_hist->{'last_state'} == 3) { } 3 34 37
elsif ($service_hist->{'last_state'} == -2) { } 33 1 34
elsif ($service_hist->{'last_state'} == -3) { } 1 0 1
Condition100%3/3
defined $self->{"in_timeperiod"} and not $self->{"in_timeperiod"}
!ll&&!rl&&r
8132
MC/DC100%2/2
defined $self->{"in_timeperiod"} and not $self->{"in_timeperiod"}
defined $self->{"in_timeperiod"} not $self->{"in_timeperiod"}
Truth table100%3/3
A and not(B)
A= defined $self->{"in_timeperiod"}B= not $self->{"in_timeperiod"}
ABresult
0X0
100
111
1056 2 $self->_add_time($service_data, $data->{'time'}, 'time_indeterminate_outside_timeperiod', $diff);
1057 }
1058  
1059 # ok
1060 elsif($service_hist->{'last_state'} == STATE_OK) {
1061 39 ▶ $self->_add_time($service_data, $data->{'time'}, 'time_ok', $diff, ($service_hist->{'in_downtime'} or $host_hist->{'in_downtime'}));
Condition66%2/3
$service_hist->{'in_downtime'} || $host_hist->{'in_downtime'}
l!l&&r!l&&!r
0138
MC/DC50%1/2
$service_hist->{'in_downtime'} || $host_hist->{'in_downtime'}
$service_hist->{'in_downtime'} $host_hist->{'in_downtime'}
Truth table66%2/3
A || B
A= $service_hist->{'in_downtime'}B= $host_hist->{'in_downtime'}
ABresult
1X1
011
000
1062 }
1063  
1064 # warning
1065 elsif($service_hist->{'last_state'} == STATE_WARNING) {
1066 3 ▶ $self->_add_time($service_data, $data->{'time'}, 'time_warning', $diff, ($service_hist->{'in_downtime'} or $host_hist->{'in_downtime'}));
Condition33%1/3
$service_hist->{'in_downtime'} || $host_hist->{'in_downtime'}
l!l&&r!l&&!r
003
MC/DC0%0/2
$service_hist->{'in_downtime'} || $host_hist->{'in_downtime'}
$service_hist->{'in_downtime'} $host_hist->{'in_downtime'}
Truth table33%1/3
A || B
A= $service_hist->{'in_downtime'}B= $host_hist->{'in_downtime'}
ABresult
1X1
011
000
1067 }
1068  
1069 # critical
1070 elsif($service_hist->{'last_state'} == STATE_CRITICAL) {
1071 5 ▶ $self->_add_time($service_data, $data->{'time'}, 'time_critical', $diff, ($service_hist->{'in_downtime'} or $host_hist->{'in_downtime'}));
Condition66%2/3
$service_hist->{'in_downtime'} || $host_hist->{'in_downtime'}
l!l&&r!l&&!r
014
MC/DC50%1/2
$service_hist->{'in_downtime'} || $host_hist->{'in_downtime'}
$service_hist->{'in_downtime'} $host_hist->{'in_downtime'}
Truth table66%2/3
A || B
A= $service_hist->{'in_downtime'}B= $host_hist->{'in_downtime'}
ABresult
1X1
011
000
1072 }
1073  
1074 # unknown
1075 elsif($service_hist->{'last_state'} == STATE_UNKNOWN) {
1076 3 ▶ $self->_add_time($service_data, $data->{'time'}, 'time_unknown', $diff, ($service_hist->{'in_downtime'} or $host_hist->{'in_downtime'}));
Condition33%1/3
$service_hist->{'in_downtime'} || $host_hist->{'in_downtime'}
l!l&&r!l&&!r
003
MC/DC0%0/2
$service_hist->{'in_downtime'} || $host_hist->{'in_downtime'}
$service_hist->{'in_downtime'} $host_hist->{'in_downtime'}
Truth table33%1/3
A || B
A= $service_hist->{'in_downtime'}B= $host_hist->{'in_downtime'}
ABresult
1X1
011
000
1077 }
1078  
1079 # no data yet
1080 elsif($service_hist->{'last_state'} == STATE_UNSPECIFIED) {
1081 33 ▶ $self->_add_time($service_data, $data->{'time'}, 'time_indeterminate_nodata', $diff, ($service_hist->{'in_downtime'} or $host_hist->{'in_downtime'}), 'scheduled_time_indeterminate');
Condition66%2/3
$service_hist->{'in_downtime'} || $host_hist->{'in_downtime'}
l!l&&r!l&&!r
1032
MC/DC50%1/2
$service_hist->{'in_downtime'} || $host_hist->{'in_downtime'}
$service_hist->{'in_downtime'} $host_hist->{'in_downtime'}
Truth table66%2/3
A || B
A= $service_hist->{'in_downtime'}B= $host_hist->{'in_downtime'}
ABresult
1X1
011
000
1082 }
1083  
1084 # not running
1085 elsif($service_hist->{'last_state'} == STATE_NOT_RUNNING) {
1086 1 $self->_add_time($service_data, $data->{'time'}, 'time_indeterminate_notrunning', $diff);
1087 }
1088  
1089 }
1090 }
1091  
1092 # set last state
1093 124 ▶ if(defined $data->{'state'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"state"}) 119 5 124
1094 119 ▶ $self->_log('_set_service_event() set last state = '.$data->{'state'}) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 119 119
1095 119 $service_hist->{'last_state'} = $data->{'state'};
1096 119 ▶ $service_hist->{'last_known_state'} = $data->{'state'} if $data->{'state'} >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"state"} >= 0 85 34 119
1097 }
1098  
1099 124 $service_hist->{'last_state_time'} = $data->{'time'};
1100  
1101 124 return 1;
1102 }
1103  
1104  
1105 ########################################
1106 53 ▶sub _set_host_event {
Subroutine _set_host_event: called CC=15 SCAR=27.2
1107 53 my($self, $host_name, $result, $data) = @_;
1108  
1109 53 ▶ $self->_log('_set_host_event()') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 53 53
1110  
1111 53 my $host_hist = $self->{'host_data'}->{$host_name};
1112 53 my $host_data = $result->{'hosts'}->{$host_name};
1113  
1114 # check if we are inside the report time
1115 53 ▶ if($report_options_start < $data->{'time'} and $report_options_end >= $data->{'time'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($report_options_start < $data->{"time"} and $report_options_end >= $data->{"time"}) 40 13 53
Condition66%2/3
$report_options_start < $data->{"time"} and $report_options_end >= $data->{"time"}
!ll&&!rl&&r
13040
MC/DC50%1/2
$report_options_start < $data->{"time"} and $report_options_end >= $data->{"time"}
$report_options_start < $data->{"time"} $report_options_end >= $data->{"time"}
Truth table66%2/3
A and B
A= $report_options_start < $data->{"time"}B= $report_options_end >= $data->{"time"}
ABresult
0X0
100
111
1116 # we got a last state?
1117 40 ▶ if(defined $host_hist->{'last_state'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $host_hist->{"last_state"}) 40 0 40
1118 40 my $diff = $data->{'time'} - $host_hist->{'last_state_time'};
1119  
1120 # outside timeperiod
1121 40 ▶ if(defined $self->{'in_timeperiod'} and !$self->{'in_timeperiod'}) {
Branch: 6 branches
BranchTrueFalseTotal
if (defined $self->{'in_timeperiod'} and not $self->{'in_timeperiod'}) { } 1 39 40
elsif ($host_hist->{'last_state'} == 0) { } 15 24 39
elsif ($host_hist->{'last_state'} == 1) { } 3 21 24
elsif ($host_hist->{'last_state'} == 2) { } 1 20 21
elsif ($host_hist->{'last_state'} == -2) { } 18 2 20
elsif ($host_hist->{'last_state'} == -3) { } 2 0 2
Condition100%3/3
defined $self->{"in_timeperiod"} and not $self->{"in_timeperiod"}
!ll&&!rl&&r
3811
MC/DC100%2/2
defined $self->{"in_timeperiod"} and not $self->{"in_timeperiod"}
defined $self->{"in_timeperiod"} not $self->{"in_timeperiod"}
Truth table100%3/3
A and not(B)
A= defined $self->{"in_timeperiod"}B= not $self->{"in_timeperiod"}
ABresult
0X0
100
111
1122 1 $self->_add_time($host_data, $data->{'time'}, 'time_indeterminate_outside_timeperiod', $diff);
1123 }
1124  
1125 # up
1126 elsif($host_hist->{'last_state'} == STATE_UP) {
1127 15 $self->_add_time($host_data, $data->{'time'}, 'time_up', $diff, $host_hist->{'in_downtime'});
1128 }
1129  
1130 # down
1131 elsif($host_hist->{'last_state'} == STATE_DOWN) {
1132 3 $self->_add_time($host_data, $data->{'time'}, 'time_down', $diff, $host_hist->{'in_downtime'});
1133 }
1134  
1135 # unreachable
1136 elsif($host_hist->{'last_state'} == STATE_UNREACHABLE) {
1137 1 $self->_add_time($host_data, $data->{'time'}, 'time_unreachable', $diff, $host_hist->{'in_downtime'});
1138 }
1139  
1140 # no data yet
1141 elsif($host_hist->{'last_state'} == STATE_UNSPECIFIED) {
1142 18 $self->_add_time($host_data, $data->{'time'}, 'time_indeterminate_nodata', $diff, $host_hist->{'in_downtime'}, 'scheduled_time_indeterminate');
1143 }
1144  
1145 # not running
1146 elsif($host_hist->{'last_state'} == STATE_NOT_RUNNING) {
1147 2 $self->_add_time($host_data, $data->{'time'}, 'time_indeterminate_notrunning', $diff);
1148 }
1149 }
1150 }
1151  
1152 # set last state
1153 53 ▶ if(defined $data->{'state'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"state"}) 48 5 53
1154 48 ▶ $self->_log('_set_host_event() set last state = '.$data->{'state'}) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 48 48
1155 48 $host_hist->{'last_state'} = $data->{'state'};
1156 48 ▶ $host_hist->{'last_known_state'} = $data->{'state'} if $data->{'state'} >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"state"} >= 0 25 23 48
1157 }
1158 53 $host_hist->{'last_state_time'} = $data->{'time'};
1159  
1160 53 return 1;
1161 }
1162  
1163 ########################################
1164 126 ▶sub _add_time {
Subroutine _add_time: called CC=9 SCAR=23.4
1165 126 my($self, $data, $date, $type, $diff, $in_downtime, $scheduled_type) = @_;
1166 126 ▶ $scheduled_type = 'scheduled_'.$type unless defined $scheduled_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scheduled_type 75 51 126
1167 126 ▶ $self->_log('_add_time() '.$type.' + '.$diff.' seconds ('.$self->_duration($diff).')') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 126 126
1168 126 $data->{$type} += $diff;
1169 126 ▶ if($in_downtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_downtime) 5 121 126
1170 5 ▶ $self->_log('_add_time() '.$type.' scheduled + '.$diff.' seconds') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 5 5
1171 5 $data->{$scheduled_type} += $diff;
1172 }
1173  
1174 # breakdowns?
1175 126 ▶ if($self->{'report_options'}->{'breakdown'} != BREAK_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"report_options"}{"breakdown"} != 0) 10 116 126
1176 10 my $timestr = $self->_get_break_timestr($date-1);
1177 10 $data->{'breakdown'}->{$timestr}->{$type} += $diff;
1178 10 ▶ $self->_log('_add_time() breakdown('.$timestr.') '.$type.' + '.$diff.' seconds ('.$self->_duration($diff).')') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 10 10
1179 10 ▶ if($in_downtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_downtime) 0 10 10
1180 0 ▶ $self->_log('_add_time() breakdown('.$timestr.') '.$type.' scheduled + '.$diff.' seconds ('.$self->_duration($diff).')') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
1181 0 $data->{'breakdown'}->{$timestr}->{$scheduled_type} += $diff;
1182 }
1183 }
1184 126 return;
1185 }
1186  
1187  
1188 ########################################
1189 0 ▶sub _log {
Subroutine _log: not called CC=3 SCAR=24.8
1190 0 my($self, $text) = @_;
1191 0 ▶ return 1 unless $verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose 0 0 0
1192  
1193 0 ▶ if(ref $text ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $text ne "") 0 0 0
1194 0 $Data::Dumper::Sortkeys = \&_logging_filter;
1195 0 $text = Dumper($text);
1196 }
1197 0 $self->{'logger'}->debug($text);
1198  
1199 0 return 1;
1200 }
1201  
1202 ########################################
1203 0 ▶sub _logging_filter {
Subroutine _logging_filter: not called CC=1 SCAR=6.9
1204 0 my ($hash) = @_;
1205 0 my @keys = keys %{$hash};
1206 # filter a few keys we don't want to log
1207 0 @keys = grep {!/^(state_string_2_int
1208 |logger
1209 |peer_addr
1210 |peer_name
1211 |peer_key
1212 |log_string
1213 |log_livestatus
1214 |log_file
1215 |log_dir
1216 |log_iterator
1217 |current_host_groups
1218 )$/mx} @keys;
1219 0 return \@keys;
1220 }
1221 ##############################################
1222 # calculate a duration in the
1223 # format: 0d 0h 29m 43s
1224 81 ▶sub _duration {
Subroutine _duration: called CC=7 SCAR=19.5
1225 81 my($self, $duration) = @_;
1226  
1227 81 ▶ croak("undef duration in duration(): ".$duration) unless defined $duration;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $duration 0 81 81
1228 81 ▶ $duration = $duration * -1 if $duration < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $duration < 0 74 7 81
1229  
1230 0 ▶ if($duration < 0) { $duration = time() + $duration; }
Branch: 1 branch
BranchTrueFalseTotal
if ($duration < 0) 0 81 81
1231  
1232 81 my $days = 0;
1233 81 my $hours = 0;
1234 81 my $minutes = 0;
1235 81 my $seconds = 0;
1236 81 ▶ if($duration >= 86400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duration >= 86400) 14 67 81
1237 14 $days = int($duration/86400);
1238 14 $duration = $duration%86400;
1239 }
1240 81 ▶ if($duration >= 3600) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duration >= 3600) 23 58 81
1241 23 $hours = int($duration/3600);
1242 23 $duration = $duration%3600;
1243 }
1244 81 ▶ if($duration >= 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duration >= 60) 37 44 81
1245 37 $minutes = int($duration/60);
1246 37 $duration = $duration%60;
1247 }
1248 81 $seconds = $duration;
1249  
1250 81 return($days."d ".$hours."h ".$minutes."m ".$seconds."s");
1251 }
1252  
1253 ########################################
1254 46 ▶sub _insert_fake_event {
Subroutine _insert_fake_event: called CC=9 SCAR=22.1
1255 46 my($self, $result, $time) = @_;
1256  
1257 46 ▶ $self->_log('_insert_fake_event()') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 46 46
1258 46 for my $host (keys %{$result->{'services'}}) {
1259 26 for my $service (keys %{$result->{'services'}->{$host}}) {
1260 64 my $last_service_state = STATE_UNSPECIFIED;
1261 64 ▶ if(defined $self->{'service_data'}->{$host}->{$service}->{'last_known_state'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'service_data'}{$host}{$service}{'last_known_state'}) { } 35 29 64
elsif (defined $self->{'service_data'}{$host}{$service}{'last_state'}) { } 29 0 29
1262 35 $last_service_state = $self->{'service_data'}->{$host}->{$service}->{'last_known_state'};
1263 }
1264 elsif(defined $self->{'service_data'}->{$host}->{$service}->{'last_state'}) {
1265 29 $last_service_state = $self->{'service_data'}->{$host}->{$service}->{'last_state'};
1266 }
1267 64 my $fakedata = {
1268 'service_description' => $service,
1269 'time' => $time,
1270 'host_name' => $host,
1271 'type' => 'INITIAL SERVICE STATE',
1272 'hard' => 1,
1273 'state' => $last_service_state,
1274 };
1275 64 $self->_set_service_event($host, $service, $result, $fakedata);
1276 }
1277 }
1278  
1279 46 for my $host (keys %{$result->{'hosts'}}) {
1280 22 my $last_host_state = STATE_UNSPECIFIED;
1281 22 ▶ if(defined $self->{'host_data'}->{$host}->{'last_known_state'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'host_data'}{$host}{'last_known_state'}) { } 12 10 22
elsif (defined $self->{'host_data'}{$host}{'last_state'}) { } 10 0 10
1282 12 $last_host_state = $self->{'host_data'}->{$host}->{'last_known_state'};
1283 }
1284 elsif(defined $self->{'host_data'}->{$host}->{'last_state'}) {
1285 10 $last_host_state = $self->{'host_data'}->{$host}->{'last_state'};
1286 }
1287 22 my $fakedata = {
1288 'time' => $time,
1289 'host_name' => $host,
1290 'type' => 'INITIAL HOST STATE',
1291 'hard' => 1,
1292 'state' => $last_host_state,
1293 };
1294 22 &_set_host_event($self, $host, $result, $fakedata);
1295 }
1296  
1297 46 return 1;
1298 }
1299  
1300 ########################################
1301 20 ▶sub _set_default_options {
Subroutine _set_default_options: called CC=11 SCAR=24.2
1302 20 my $self = shift;
1303 20 my $options = shift;
1304  
1305 20 ▶ $options->{'backtrack'} = 4 unless defined $options->{'backtrack'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"backtrack"} 1 19 20
1306 20 ▶ $options->{'assumeinitialstates'} = 'yes' unless defined $options->{'assumeinitialstates'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"assumeinitialstates"} 0 20 20
1307 20 ▶ $options->{'assumestateretention'} = 'yes' unless defined $options->{'assumestateretention'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"assumestateretention"} 1 19 20
1308 20 ▶ $options->{'assumestatesduringnotrunning'} = 'yes' unless defined $options->{'assumestatesduringnotrunning'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"assumestatesduringnotrunning"} 1 19 20
1309 20 ▶ $options->{'includesoftstates'} = 'no' unless defined $options->{'includesoftstates'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"includesoftstates"} 20 0 20
1310 20 ▶ $options->{'initialassumedhoststate'} = 'unspecified' unless defined $options->{'initialassumedhoststate'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"initialassumedhoststate"} 0 20 20
1311 20 ▶ $options->{'initialassumedservicestate'} = 'unspecified' unless defined $options->{'initialassumedservicestate'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"initialassumedservicestate"} 0 20 20
1312 20 ▶ $options->{'showscheduleddowntime'} = 'yes' unless defined $options->{'showscheduleddowntime'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"showscheduleddowntime"} 19 1 20
1313 20 ▶ $options->{'timeformat'} = '%s' unless defined $options->{'timeformat'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"timeformat"} 12 8 20
1314 20 ▶ $options->{'breakdown'} = BREAK_NONE unless defined $options->{'breakdown'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"breakdown"} 18 2 20
1315  
1316 20 return $options;
1317 }
1318  
1319 ########################################
1320 38 ▶sub _verify_options {
Subroutine _verify_options: called CC=30 SCAR=44.0
1321 38 my $self = shift;
1322 38 my $options = shift;
1323  
1324 # set default backtrack to 4 days
1325 38 ▶ if(defined $options->{'backtrack'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"backtrack"}) 20 18 38
1326 20 ▶ if($options->{'backtrack'} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"backtrack"} < 0) 0 20 20
1327 0 croak('backtrack has to be a positive integer');
1328 }
1329 }
1330  
1331 # our yes no options
1332 38 for my $yes_no (qw/assumeinitialstates
1333 assumestateretention
1334 assumestatesduringnotrunning
1335 includesoftstates
1336 showscheduleddowntime
1337 /) {
1338 190 ▶ if(defined $options->{$yes_no}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{$yes_no}) 100 90 190
1339 100 ▶ if(lc $options->{$yes_no} eq 'yes') {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $options->{$yes_no} eq 'yes') { } 70 30 100
elsif (lc $options->{$yes_no} eq 'no') { } 30 0 30
1340 70 $options->{$yes_no} = TRUE;
1341 }
1342 elsif(lc $options->{$yes_no} eq 'no') {
1343 30 $options->{$yes_no} = FALSE;
1344 } else {
1345 0 croak($yes_no.' unknown, please use \'yes\' or \'no\'. Got: '.$options->{$yes_no});
1346 }
1347 }
1348 }
1349  
1350 # set initial assumed host state
1351 38 ▶ if(defined $options->{'initialassumedhoststate'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"initialassumedhoststate"}) 20 18 38
1352 20 ▶ if(lc $options->{'initialassumedhoststate'} eq 'unspecified') {
Branch: 5 branches
BranchTrueFalseTotal
if (lc $options->{'initialassumedhoststate'} eq 'unspecified') { } 18 2 20
elsif (lc $options->{'initialassumedhoststate'} eq 'current') { } 0 2 2
elsif (lc $options->{'initialassumedhoststate'} eq 'up') { } 2 0 2
elsif (lc $options->{'initialassumedhoststate'} eq 'down') { } 0 0 0
elsif (lc $options->{'initialassumedhoststate'} eq 'unreachable') { } 0 0 0
1353 18 $options->{'initialassumedhoststate'} = STATE_UNSPECIFIED;
1354 }
1355 elsif(lc $options->{'initialassumedhoststate'} eq 'current') {
1356 0 $options->{'initialassumedhoststate'} = STATE_CURRENT;
1357 }
1358 elsif(lc $options->{'initialassumedhoststate'} eq 'up') {
1359 2 $options->{'initialassumedhoststate'} = STATE_UP;
1360 }
1361 elsif(lc $options->{'initialassumedhoststate'} eq 'down') {
1362 0 $options->{'initialassumedhoststate'} = STATE_DOWN;
1363 }
1364 elsif(lc $options->{'initialassumedhoststate'} eq 'unreachable') {
1365 0 $options->{'initialassumedhoststate'} = STATE_UNREACHABLE;
1366 }
1367 else {
1368 0 croak('initialassumedhoststate unknown, please use one of: unspecified, current, up, down or unreachable. Got: '.$options->{'initialassumedhoststate'});
1369 }
1370 }
1371  
1372 # set initial assumed service state
1373 38 ▶ if(defined $options->{'initialassumedservicestate'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"initialassumedservicestate"}) 20 18 38
1374 20 ▶ if(lc $options->{'initialassumedservicestate'} eq 'unspecified') {
Branch: 6 branches
BranchTrueFalseTotal
if (lc $options->{'initialassumedservicestate'} eq 'unspecified') { } 16 4 20
elsif (lc $options->{'initialassumedservicestate'} eq 'current') { } 1 3 4
elsif (lc $options->{'initialassumedservicestate'} eq 'ok') { } 2 1 3
elsif (lc $options->{'initialassumedservicestate'} eq 'warning') { } 0 1 1
elsif (lc $options->{'initialassumedservicestate'} eq 'unknown') { } 1 0 1
elsif (lc $options->{'initialassumedservicestate'} eq 'critical') { } 0 0 0
1375 16 $options->{'initialassumedservicestate'} = STATE_UNSPECIFIED;
1376 }
1377 elsif(lc $options->{'initialassumedservicestate'} eq 'current') {
1378 1 $options->{'initialassumedservicestate'} = STATE_CURRENT;
1379 }
1380 elsif(lc $options->{'initialassumedservicestate'} eq 'ok') {
1381 2 $options->{'initialassumedservicestate'} = STATE_OK;
1382 }
1383 elsif(lc $options->{'initialassumedservicestate'} eq 'warning') {
1384 0 $options->{'initialassumedservicestate'} = STATE_WARNING;
1385 }
1386 elsif(lc $options->{'initialassumedservicestate'} eq 'unknown') {
1387 1 $options->{'initialassumedservicestate'} = STATE_UNKNOWN;
1388 }
1389 elsif(lc $options->{'initialassumedservicestate'} eq 'critical') {
1390 0 $options->{'initialassumedservicestate'} = STATE_CRITICAL;
1391 }
1392 else {
1393 0 croak('initialassumedservicestate unknown, please use one of: unspecified, current, ok, warning, unknown or critical. Got: '.$options->{'initialassumedservicestate'});
1394 }
1395 }
1396  
1397 # set breakdown
1398 38 ▶ if(defined $options->{'breakdown'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"breakdown"}) 20 18 38
1399 20 ▶ if(lc $options->{'breakdown'} eq '') {
Branch: 6 branches
BranchTrueFalseTotal
if (lc $options->{'breakdown'} eq '') { } 0 20 20
elsif (lc $options->{'breakdown'} eq 'months') { } 0 20 20
elsif (lc $options->{'breakdown'} eq 'weeks') { } 0 20 20
elsif (lc $options->{'breakdown'} eq 'days') { } 2 18 20
elsif (lc $options->{'breakdown'} eq 'none') { } 0 18 18
elsif ($options->{'breakdown'} == 0 or $options->{'breakdown'} == 1 or $options->{'breakdown'} == 2 or $options->{'breakdown'} == 3) { } 18 0 18
1400 0 $options->{'breakdown'} = BREAK_NONE;
1401 }
1402 elsif(lc $options->{'breakdown'} eq 'months') {
1403 0 $options->{'breakdown'} = BREAK_MONTHS;
1404 }
1405 elsif(lc $options->{'breakdown'} eq 'weeks') {
1406 0 $options->{'breakdown'} = BREAK_WEEKS;
1407 }
1408 elsif(lc $options->{'breakdown'} eq 'days') {
1409 2 $options->{'breakdown'} = BREAK_DAYS;
1410 }
1411 elsif(lc $options->{'breakdown'} eq 'none') {
1412 0 $options->{'breakdown'} = BREAK_NONE;
1413 }
1414 ▶ elsif( $options->{'breakdown'} == BREAK_NONE
Condition33%3/9
$options->{"breakdown"} == 0 or $options->{"breakdown"} == 1 or $options->{"breakdown"} == 2 or $options->{"breakdown"} == 3
l!l&&r!l&&!r
1800
$options->{"breakdown"} == 0 or $options->{"breakdown"} == 1 or $options->{"breakdown"} == 2
l!l&&r!l&&!r
1800
$options->{"breakdown"} == 0 or $options->{"breakdown"} == 1
l!l&&r!l&&!r
1800
MC/DC0%0/4
$options->{"breakdown"} == 0 or $options->{"breakdown"} == 1 or $options->{"breakdown"} == 2 or $options->{"breakdown"} == 3
$options->{"breakdown"} == 0 $options->{"breakdown"} == 1 $options->{"breakdown"} == 2 $options->{"breakdown"} == 3
Truth table20%1/5
A or B or C or D
A= $options->{"breakdown"} == 0B= $options->{"breakdown"} == 1C= $options->{"breakdown"} == 2D= $options->{"breakdown"} == 3
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1415 or $options->{'breakdown'} == BREAK_DAYS
1416 or $options->{'breakdown'} == BREAK_WEEKS
1417 or $options->{'breakdown'} == BREAK_MONTHS) {
1418 # ok
1419 }
1420 else {
1421 0 croak('breakdown unknown, please use one of: months, weeks, days or none. Got: '.$options->{'breakdown'});
1422 }
1423 }
1424  
1425 38 return $options;
1426 }
1427  
1428 ########################################
1429 105 ▶sub _add_log_entry {
Subroutine _add_log_entry: called CC=3 SCAR=11.2
1430 105 my($self, %opts) = @_;
1431  
1432 105 ▶ $self->_log('_add_log_entry()') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 105 105
1433  
1434 # do we build up a log?
1435 105 ▶ return if $self->{'report_options'}->{'build_log'} == FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"report_options"}{"build_log"} == 0 0 105 105
1436  
1437 105 push @{$self->{'full_log_store'}}, \%opts;
1438  
1439 105 return 1;
1440 }
1441  
1442 ########################################
1443 9 ▶sub _calculate_log {
Subroutine _calculate_log: called CC=27 SCAR=39.4
1444 9 my($self) = @_;
1445  
1446 9 ▶ $self->_log('_calculate_log()') if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 9 9
1447  
1448 # combine outside report range log events
1449 9 my $changed = FALSE;
1450 9 ▶ if(defined $self->{'first_known_state_before_report'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"first_known_state_before_report"}) 0 9 9
1451 0 push @{$self->{'full_log_store'}}, $self->{'first_known_state_before_report'};
1452 0 $changed = TRUE;
1453 }
1454 9 ▶ if(defined $self->{'first_known_proc_before_report'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"first_known_proc_before_report"}) 0 9 9
1455 0 push @{$self->{'full_log_store'}}, $self->{'first_known_proc_before_report'};
1456 0 $changed = TRUE;
1457 }
1458  
1459 # sort once more if changed
1460 9 ▶ if($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 0 9 9
1461 0 @{$self->{'full_log_store'}} = sort { $a->{'log'}->{'start'} <=> $b->{'log'}->{'start'} } @{$self->{'full_log_store'}};
1462 }
1463  
1464 # insert fakelog service entry when initial state is fixed
1465 9 ▶ if($self->{'report_options'}->{'initialassumedservicestate'} != STATE_UNSPECIFIED
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"report_options"}{"initialassumedservicestate"} != -2 and scalar @{$self->{"report_options"}{"services"};} == 1) 2 7 9
Condition100%3/3
$self->{"report_options"}{"initialassumedservicestate"} != -2 and scalar @{$self->{"report_options"}{"services"};} == 1
!ll&&!rl&&r
612
MC/DC100%2/2
$self->{"report_options"}{"initialassumedservicestate"} != -2 and scalar @{$self->{"report_options"}{"services"};} == 1
$self->{"report_options"}{"initialassumedservicestate"} != -2 scalar @{$self->{"report_options"}{"services"};} == 1
Truth table100%3/3
A and B
A= $self->{"report_options"}{"initialassumedservicestate"} != -2B= scalar @{$self->{"report_options"}{"services"};} == 1
ABresult
0X0
100
111
1466 3 and scalar @{$self->{'report_options'}->{'services'}} == 1
1467 ) {
1468 2 my $type;
1469 2 my $first_state = $self->{'report_options'}->{'initialassumedservicestate'};
1470 2 ▶ if($first_state == STATE_CURRENT) { $first_state = $self->{'report_options'}->{'first_state'}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($first_state == -1) 1 1 2
1471 0 ▶ if($first_state == STATE_OK) { $type = 'OK'; }
Branch: 4 branches
BranchTrueFalseTotal
if ($first_state == 0) { } 0 2 2
elsif ($first_state == 1) { } 1 1 2
elsif ($first_state == 3) { } 1 0 1
elsif ($first_state == 2) { } 0 0 0
1472 1 elsif($first_state == STATE_WARNING) { $type = 'WARNING'; }
1473 1 elsif($first_state == STATE_UNKNOWN) { $type = 'UNKNOWN'; }
1474 0 elsif($first_state == STATE_CRITICAL) { $type = 'CRITICAL'; }
1475 2 my $fake_start = $self->{'report_options'}->{'start'};
1476 2 ▶ if(defined $self->{'full_log_store'}->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"full_log_store"}[0]) 2 0 2
1477 2 ▶ if($fake_start >= $self->{'full_log_store'}->[0]->{'log'}->{'start'}) { $fake_start = $self->{'full_log_store'}->[0]->{'log'}->{'start'} - 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($fake_start >= $self->{"full_log_store"}[0]{"log"}{"start"}) 2 0 2
1478 }
1479 2 my $fakelog = {
1480 'log' => {
1481 'type' => 'SERVICE '.$type.' (HARD)',
1482 'class' => $type,
1483 'start' => $fake_start,
1484 'plugin_output' => 'First Service State Assumed (Faked Log Entry)',
1485 }
1486 };
1487 2 unshift @{$self->{'full_log_store'}}, $fakelog;
1488 }
1489  
1490 # insert fakelog host entry when initial state is fixed
1491 9 ▶ if($self->{'report_options'}->{'initialassumedhoststate'} != STATE_UNSPECIFIED
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"report_options"}{"initialassumedhoststate"} != -2 and scalar @{$self->{"report_options"}{"hosts"};} == 1) 1 8 9
Condition66%2/3
$self->{"report_options"}{"initialassumedhoststate"} != -2 and scalar @{$self->{"report_options"}{"hosts"};} == 1
!ll&&!rl&&r
801
MC/DC50%1/2
$self->{"report_options"}{"initialassumedhoststate"} != -2 and scalar @{$self->{"report_options"}{"hosts"};} == 1
$self->{"report_options"}{"initialassumedhoststate"} != -2 scalar @{$self->{"report_options"}{"hosts"};} == 1
Truth table66%2/3
A and B
A= $self->{"report_options"}{"initialassumedhoststate"} != -2B= scalar @{$self->{"report_options"}{"hosts"};} == 1
ABresult
0X0
100
111
1492 1 and scalar @{$self->{'report_options'}->{'hosts'}} == 1
1493 ) {
1494 1 my $type;
1495 1 my $first_state = $self->{'report_options'}->{'initialassumedhoststate'};
1496 0 ▶ if($first_state == STATE_CURRENT) { $first_state = $self->{'report_options'}->{'first_state'}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($first_state == -1) 0 1 1
1497 1 ▶ if($first_state == STATE_UP) { $type = 'UP'; }
Branch: 3 branches
BranchTrueFalseTotal
if ($first_state == 0) { } 1 0 1
elsif ($first_state == 1) { } 0 0 0
elsif ($first_state == 2) { } 0 0 0
1498 0 elsif($first_state == STATE_DOWN) { $type = 'DOWN'; }
1499 0 elsif($first_state == STATE_UNREACHABLE) { $type = 'UNREACHABLE'; }
1500 1 my $fake_start = $self->{'report_options'}->{'start'};
1501 0 ▶ if(defined $self->{'full_log_store'}->[0] and $fake_start >= $self->{'full_log_store'}->[0]->{'log'}->{'start'}) { $fake_start = $self->{'full_log_store'}->[0]->{'log'}->{'start'} - 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"full_log_store"}[0] and $fake_start >= $self->{"full_log_store"}[0]{"log"}{"start"}) 0 1 1
Condition33%1/3
defined $self->{"full_log_store"}[0] and $fake_start >= $self->{"full_log_store"}[0]{"log"}{"start"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->{"full_log_store"}[0] and $fake_start >= $self->{"full_log_store"}[0]{"log"}{"start"}
defined $self->{"full_log_store"}[0] $fake_start >= $self->{"full_log_store"}[0]{"log"}{"start"}
Truth table33%1/3
A and B
A= defined $self->{"full_log_store"}[0]B= $fake_start >= $self->{"full_log_store"}[0]{"log"}{"start"}
ABresult
0X0
100
111
1502 1 my $fakelog = {
1503 'log' => {
1504 'type' => 'HOST '.$type.' (HARD)',
1505 'class' => $type,
1506 'start' => $fake_start,
1507 'plugin_output' => 'First Host State Assumed (Faked Log Entry)',
1508 }
1509 };
1510 1 unshift @{$self->{'full_log_store'}}, $fakelog;
1511 }
1512  
1513 9 ▶ if($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 9 9
1514 0 $self->_log("#################################");
1515 0 $self->_log("LOG STORE:");
1516 0 $self->_log(\@{$self->{'full_log_store'}});
1517 0 $self->_log("#################################");
1518 }
1519  
1520 9 ▶ for(my $x = 0; $x < scalar @{$self->{'full_log_store'}}; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < scalar @{$self->{"full_log_store"};}) 81 9 90
1521 81 my $log_entry = $self->{'full_log_store'}->[$x];
1522 81 my $next_log_entry = $self->{'full_log_store'}->[$x+1];
1523 81 my $log = $log_entry->{'log'};
1524  
1525 # set end date of current log entry
1526 81 ▶ if(defined $next_log_entry->{'log'}->{'start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_log_entry->{'log'}{'start'}) { } 72 9 81
1527 72 $log->{'end'} = $next_log_entry->{'log'}->{'start'};
1528 72 $log->{'duration'} = $self->_duration($log->{'start'} - $log->{'end'});
1529 } else {
1530 9 $log->{'end'} = $self->{'report_options'}->{'end'};
1531 9 $log->{'duration'} = $self->_duration($log->{'start'} - $log->{'end'}).'+';
1532 }
1533  
1534 # convert time format
1535 81 ▶ if($self->{'report_options'}->{'timeformat'} ne '%s') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"report_options"}{"timeformat"} ne "%s") 80 1 81
1536 80 $log->{'end'} = strftime $self->{'report_options'}->{'timeformat'}, localtime($log->{'end'});
1537 80 $log->{'start'} = strftime $self->{'report_options'}->{'timeformat'}, localtime($log->{'start'});
1538 }
1539  
1540 81 ▶ push @{$self->{'log_output'}}, $log unless defined $log_entry->{'full_only'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $log_entry->{"full_only"} 30 51 81
1541 81 push @{$self->{'full_log_output'}}, $log;
1542 }
1543  
1544 9 $self->{'log_output_calculated'} = TRUE;
1545 9 return 1;
1546 }
1547  
1548  
1549 ########################################
1550 1 ▶sub _state_to_int {
Subroutine _state_to_int: called CC=3 SCAR=12.0
1551 1 my($self, $string) = @_;
1552  
1553 1 ▶ return unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 1 1
1554  
1555 1 ▶ if(defined $self->{'state_string_2_int'}->{$string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"state_string_2_int"}{$string}) 1 0 1
1556 1 return $self->{'state_string_2_int'}->{$string};
1557 }
1558  
1559 0 croak("valid values for services are: ok, warning, unknown and critical\nvalues for host: up, down and unreachable");
1560 }
1561  
1562 ########################################
1563 32 ▶sub _new_service_data {
Subroutine _new_service_data: called CC=5 SCAR=23.1
1564 32 my($self, $breakdown, $timestamp) = @_;
1565 32 my $data = {
1566 time_ok => 0,
1567 time_warning => 0,
1568 time_unknown => 0,
1569 time_critical => 0,
1570  
1571 scheduled_time_ok => 0,
1572 scheduled_time_warning => 0,
1573 scheduled_time_unknown => 0,
1574 scheduled_time_critical => 0,
1575 scheduled_time_indeterminate => 0,
1576  
1577 time_indeterminate_nodata => 0,
1578 time_indeterminate_notrunning => 0,
1579 time_indeterminate_outside_timeperiod => 0,
1580 };
1581 32 ▶ $data->{'timestamp'} = $timestamp if $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if $timestamp 0 32 32
1582 32 ▶ if($breakdown != BREAK_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($breakdown != 0) 0 32 32
1583 0 $data->{'breakdown'} = {};
1584 0 for my $cur (@{$self->{'breakpoints'}}) {
1585 0 my $timestr = $self->_get_break_timestr($cur);
1586 0 ▶ next if defined $data->{'breakdown'}->{$timestr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{"breakdown"}{$timestr} 0 0 0
1587 0 $data->{'breakdown'}->{$timestr} = $self->_new_service_data(BREAK_NONE, $cur);
1588 }
1589 }
1590 32 return $data;
1591 }
1592  
1593 ########################################
1594 14 ▶sub _new_host_data {
Subroutine _new_host_data: called CC=5 SCAR=16.1
1595 14 my($self, $breakdown, $timestamp) = @_;
1596 14 my $data = {
1597 time_up => 0,
1598 time_down => 0,
1599 time_unreachable => 0,
1600  
1601 scheduled_time_up => 0,
1602 scheduled_time_down => 0,
1603 scheduled_time_unreachable => 0,
1604 scheduled_time_indeterminate => 0,
1605  
1606 time_indeterminate_nodata => 0,
1607 time_indeterminate_notrunning => 0,
1608 time_indeterminate_outside_timeperiod => 0,
1609 };
1610 14 ▶ $data->{'timestamp'} = $timestamp if $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if $timestamp 6 8 14
1611 14 ▶ if($breakdown != BREAK_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($breakdown != 0) 2 12 14
1612 2 $data->{'breakdown'} = {};
1613 2 for my $cur (@{$self->{'breakpoints'}}) {
1614 6 my $timestr = $self->_get_break_timestr($cur);
1615 6 ▶ next if defined $data->{'breakdown'}->{$timestr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{"breakdown"}{$timestr} 0 6 6
1616 6 $data->{'breakdown'}->{$timestr} = $self->_new_host_data(BREAK_NONE, $cur);
1617 }
1618 }
1619 14 return $data;
1620 }
1621  
1622 ########################################
1623 16 ▶sub _get_break_timestr {
Subroutine _get_break_timestr: called CC=4 SCAR=21.1
1624 16 my($self, $timestamp) = @_;
1625  
1626 16 my @localtime = localtime($timestamp);
1627  
1628 16 ▶ if($self->{'report_options'}->{'breakdown'} == BREAK_DAYS) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'report_options'}{'breakdown'} == 1) { } 16 0 16
elsif ($self->{'report_options'}{'breakdown'} == 2) { } 0 0 0
elsif ($self->{'report_options'}{'breakdown'} == 3) { } 0 0 0
1629 16 return strftime('%Y-%m-%d', @localtime);
1630 }
1631 elsif($self->{'report_options'}->{'breakdown'} == BREAK_WEEKS) {
1632 0 return strftime('%G-WK%V', @localtime);
1633 }
1634 elsif($self->{'report_options'}->{'breakdown'} == BREAK_MONTHS) {
1635 0 return strftime('%Y-%m', @localtime);
1636 }
1637 0 die('huh?');
1638 0 return;
1639 }
1640  
1641 ########################################
1642 20 ▶sub _set_breakpoints {
Subroutine _set_breakpoints: called CC=3 SCAR=11.0
1643 20 my($self) = @_;
1644 20 $self->{'breakpoints'} = [];
1645  
1646 20 ▶ return if $self->{'report_options'}->{'breakdown'} == BREAK_NONE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"report_options"}{"breakdown"} == 0 18 2 20
1647  
1648 2 my $cur = $self->{'report_options'}->{'start'};
1649 # round to next 0:00
1650 2 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($cur);
1651 2 $cur = mktime(0, 0, 0, $mday, $mon, $year, $wday, $yday, $isdst);
1652 2 ▶ while($cur < $self->{'report_options'}->{'end'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur < $self->{"report_options"}{"end"}) 6 2 8
1653 6 push @{$self->{'breakpoints'}}, $cur;
1654 6 $cur = $cur + 86400;
1655 }
1656 2 return;
1657 }
1658  
1659 ########################################
1660  
1661 17 1;
1662  
1663 =head1 BUGS
1664  
1665 Please report any bugs or feature requests to L<http://github.com/sni/Monitoring-Availability/issues>.
1666  
1667 =head1 DEBUGING
1668  
1669 You may enable the debug mode by setting MONITORING_AVAILABILITY_DEBUG environment variable.
1670 This will create a logfile: /tmp/Monitoring-Availability-Debug.log which gets overwritten with
1671 every calculation.
1672 You will need the Log4Perl module to create this logfile.
1673  
1674 =head1 SEE ALSO
1675  
1676 You can also look for information at:
1677  
1678 =over 4
1679  
1680 =item * Search CPAN
1681  
1682 L<http://search.cpan.org/dist/Monitoring-Availability/>
1683  
1684 =item * Github
1685  
1686 L<http://github.com/sni/Monitoring-Availability>
1687  
1688 =back
1689  
1690 =head1 AUTHOR
1691  
1692 Sven Nierlein, E<lt>nierlein@cpan.orgE<gt>
1693  
1694 =head1 COPYRIGHT AND LICENSE
1695  
1696 Copyright (C) 2010 by Sven Nierlein
1697  
1698 This library is free software; you can redistribute it and/or modify
1699 it under the same terms as Perl itself.
1700  
1701 =cut
1702  
1703 __END__
↑ Summary Availability/Logs.pm »