blib/lib/Nagios/Passive/BulkResult.pm

filter: Statementstmt 50.0% 19 / 38 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 2 totaltotal 40.6% 24 / 59 CC 7 SCAR 24.8
CC
7
Cov
40%
add
20.0
submit
20.0
__ANON__
2.0
CRAP 11.9
« blib/lib/Nagios/Passive/CommandFile.pm ↑ Summary blib/lib/Nagios/Passive/Gearman.pm »
1 package Nagios::Passive::BulkResult;
2 2 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 has 'checkresults_dir' => ( is => 'ro', isa => 'Str', required => 1);
9 has rpobjects => (
10 is => 'rw',
11 isa => 'ArrayRef[Nagios::Passive::ResultPath]',
12 traits => ['Array'],
13 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 # MouseX::NativeTraits is horribly slow currently
15 # so I provide my own sub add as a workaround
16 # handles => {
17 # add => 'push',
18 # },
19 2 );
20  
21 2 with 'Nagios::Passive::Role::Tempfile';
22  
23 sub add {
24 0 ▶ my $self = shift;
Subroutine add: not called CC=4 SCAR=30.0
Pod undocumented
25 0 my $error = "type constraint violated, not a Nagios::Passive::ResultPath";
26 0 for my $x (@_) {
27 0 ▶ croak($error) unless
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($x) and $x->isa("Nagios::Passive::ResultPath") 0 0 0
Condition0%0/3
defined builtin::blessed($x) and $x->isa("Nagios::Passive::ResultPath")
!ll&&!rl&&r
000
MC/DC0%0/2
defined builtin::blessed($x) and $x->isa("Nagios::Passive::ResultPath")
defined builtin::blessed($x) $x->isa("Nagios::Passive::ResultPath")
Truth table0%0/3
A and B
A= defined builtin::blessed($x)B= $x->isa("Nagios::Passive::ResultPath")
ABresult
0X0
100
111
28 defined blessed($x) and $x->isa('Nagios::Passive::ResultPath');
29 }
30 0 push @{ $self->rpobjects }, @_;
31 }
32  
33 sub submit {
34 0 ▶ my $self = shift;
Subroutine submit: not called CC=4 SCAR=30.0
Pod undocumented
35  
36 # nothing to do if empty
37 0 ▶ return unless @{$self->rpobjects};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->rpobjects;} 0 0 0
38  
39 0 my $fh = $self->tempfile;
40  
41 0 print $fh "### Active Check Result File ###\n";
42 0 print $fh sprintf("file_time=%d\n\n",time);
43  
44 0 for my $rp (@{ $self->rpobjects }) {
45 0 my $output = $rp->_to_string . "\n";
46 0 ▶ $fh->print($output) or croak($!);
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($output) or croak($!) 0 0 0
47 }
48  
49 0 $self->_touch_file;
50  
51 0 return $fh->filename;
52 }
53  
54 2 1;
55 __END__
56  
57 =head1 NAME
58  
59 Nagios::Passive::BulkResult - submit passive check results to nagios' checkresult_dir in one file
60  
61 =head1 SYNOPSIS
62  
63 my $bulk = Nagios::Passive::BulkResult->new(
64 checkresults_dir => '/var/lib/nagios/checkresults',
65 );
66 for my $check (@checkresults) {
67 my $nw = Nagios::Passive->create(
68 checkresults_dir => undef, # the checkresults_dir key is required here
69 service_description => $check->{service_description},
70 check_name => $check_name,
71 host_name => $check->{hostname},
72 return_code => $check->{code},
73 output => 'looks (good|bad|horrible) | performancedata'
74 );
75 $bulk->add($nw);
76 }
77 $bulk->submit;
78  
79 =head1 DESCRIPTION
80  
81 Submitting a huge amount of results with L<Nagios::Passive::ResultPath> has some
82 limits. Typically a checkresult has a size of 200 bytes or so. But the blocksize
83 of most filesystems is about 4K. Therefore a file takes at least 4K of disk space.
84  
85 Well, disk space is cheap and nagios deletes the file again after it has
86 processed it, but most of the time the checkresults_dir is a memory filesystem.
87 And suddenly you waste a lot of RAM. Also reading one large file is faster than
88 reading thousands of small files.
89  
90 Nagios can handle multiple check results within one file. This is what this
91 module provides. You just create Nagios::Passive objects with an undefined
92 checkresults_dir and add these to the BulkResult container. When you
93 are done, just call C<-E<gt>submit> on the container and one big file is created.
« blib/lib/Nagios/Passive/CommandFile.pm ↑ Summary blib/lib/Nagios/Passive/Gearman.pm »