blib/lib/Nagios/Passive/Gearman.pm

filter: Statementstmt 44.1% 15 / 34 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 55.5% 5 / 9 Podpod n/a totaltotal 36.3% 20 / 55 CC 7 SCAR 22.0
CC
7
Cov
33%
_null_padding
20.0
submit
12.0
to_string
6.0
CRAP 9.0
« blib/lib/Nagios/Passive/BulkResult.pm ↑ Summary blib/lib/Nagios/Passive/ResultPath.pm »
1 package Nagios::Passive::Gearman;
2 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Gearman::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Crypt::Rijndael;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 extends 'Nagios::Passive::Base';
9  
10 has 'key' => (
11 is => 'rw',
12 isa => 'Str',
13 required => 0,
14 predicate => 'has_key',
15 );
16  
17 has queue => (
18 is => 'rw',
19 isa => 'Str',
20 required => 1,
21 default => sub { "check_results" },
22 );
23  
24 has gearman => (
25 is => 'rw',
26 isa => 'Gearman::Client',
27 required => 1,
28 );
29  
30 sub to_string {
31 0 ▶ my $self = shift;
Subroutine to_string: not called CC=2 SCAR=17.9
32 0 my $template = << 'EOT';
33 type=%s
34 host_name=%s%s
35 start_time=%i.%i
36 finish_time=%i.%i
37 latency=%i.%i
38 return_code=%i
39 output=%s %s - %s
40 EOT
41 0 ▶ my $result = sprintf $template,
Branch: 1 branch
BranchTrueFalseTotal
defined $self->service_description ? : 0 0 0
42 'passive',
43 $self->host_name,
44 (defined $self->service_description
45 ? sprintf "\nservice_description=%s", $self->service_description
46 : '' ),
47 time,0,
48 time,0,
49 0,0,
50 $self->return_code,
51 $self->check_name,
52 $self->_status_code,
53 $self->_quoted_output;
54 0 return $result;
55 }
56  
57 sub encrypted_string {
58 0 ▶ my $self = shift;
Subroutine encrypted_string: not called CC=1 SCAR=6.9
59 0 my $payload = $self->to_string;
60 0 my $crypt = Crypt::Rijndael->new(
61 _null_padding($self->key,32,'e'),
62 Crypt::Rijndael::MODE_ECB() # :-(
63 );
64 0 $payload = _null_padding($payload,32,'e');
65 0 encode_base64($crypt->encrypt($payload));
66 }
67  
68 sub submit {
69 0 ▶ my $self = shift;
Subroutine submit: not called CC=3 SCAR=24.8
70 0 ▶ my $payload = $self->has_key
Branch: 1 branch
BranchTrueFalseTotal
$self->has_key ? : 0 0 0
71 ? $self->encrypted_string
72 : $self->to_string;
73 0 ▶ $self->gearman->dispatch_background($self->queue, $payload)
Branch: 1 branch
BranchTrueFalseTotal
$self->gearman->dispatch_background($self->queue, $payload) or croak("submitting job failed") 0 0 0
74 or croak("submitting job failed");
75 }
76  
77 # Thanks to Crypt::CBC
78 sub _null_padding {
79 0 ▶ my ($b,$bs,$decrypt) = @_;
Subroutine _null_padding: not called CC=4 SCAR=30.0
80 0 ▶ return unless length $b;
Branch: 1 branch
BranchTrueFalseTotal
unless length $b 0 0 0
81 0 ▶ $b = length $b ? $b : '';
Branch: 1 branch
BranchTrueFalseTotal
length $b ? : 0 0 0
82 0 ▶ if ($decrypt eq 'd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($decrypt eq "d") 0 0 0
83 0 $b=~ s/\0*$//s;
84 0 return $b;
85 }
86 0 return $b . pack("C*", (0) x ($bs - length($b) % $bs));
87 }
88  
89 1;
90  
91 __END__
92 =head1 NAME
93  
94 Nagios::Passive::Gearman - drop check results into mod_gearman's check_result queue
95  
96 =head1 SYNOPSIS
97  
98 my $gearman = Gearman::Client->new;
99 $gearman->job_servers([@job_servers]);
100  
101 my $nw = Nagios::Passive->create(
102 gearman => $gearman,
103 key => "...", # if using encryption
104 service_description => $service_description,
105 check_name => $check_name,
106 host_name => $hostname,
107 return_code => 0, # 1 2 3
108 output => 'looks (good|bad|horrible) | performancedata'
109 );
110  
111 $nw->submit;
112  
113 =head1 DESCRIPTION
114  
115 This module gives you the ability to drop checkresults into
116 mod_gearman's check_result queue.
117  
118 The usage is described in L<Nagios::Passive>
119  
120 =cut
« blib/lib/Nagios/Passive/BulkResult.pm ↑ Summary blib/lib/Nagios/Passive/ResultPath.pm »