blib/lib/Nagios/Passive/Gearman.pm

filter: Statementstmt 41.6% 15 / 36 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 55.5% 5 / 9 Podpod n/a totaltotal 35.0% 20 / 57 CC 7 SCAR 22.0
CC
7
Cov
31%
_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 $key = $self->key;
61 0 $key = substr($key,0,32) . chr(0) x ( 32 - length( $key ) );
62 0 my $crypt = Crypt::Rijndael->new(
63 $key,
64 Crypt::Rijndael::MODE_ECB() # :-(
65 );
66 0 $payload = _null_padding($payload,32,'e');
67 0 encode_base64($crypt->encrypt($payload));
68 }
69  
70 sub submit {
71 0 ▶ my $self = shift;
Subroutine submit: not called CC=3 SCAR=24.8
72 0 ▶ my $payload = $self->has_key
Branch: 1 branch
BranchTrueFalseTotal
$self->has_key ? : 0 0 0
73 ? $self->encrypted_string
74 : $self->to_string;
75 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
76 or croak("submitting job failed");
77 }
78  
79 # Thanks to Crypt::CBC
80 sub _null_padding {
81 0 ▶ my ($b,$bs,$decrypt) = @_;
Subroutine _null_padding: not called CC=4 SCAR=30.0
82 0 ▶ return unless length $b;
Branch: 1 branch
BranchTrueFalseTotal
unless length $b 0 0 0
83 0 ▶ $b = length $b ? $b : '';
Branch: 1 branch
BranchTrueFalseTotal
length $b ? : 0 0 0
84 0 ▶ if ($decrypt eq 'd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($decrypt eq "d") 0 0 0
85 0 $b=~ s/\0*$//s;
86 0 return $b;
87 }
88 0 return $b . pack("C*", (0) x ($bs - length($b) % $bs));
89 }
90  
91 1;
92  
93 __END__
94 =head1 NAME
95  
96 Nagios::Passive::Gearman - drop check results into mod_gearman's check_result queue
97  
98 =head1 SYNOPSIS
99  
100 my $gearman = Gearman::Client->new;
101 $gearman->job_servers([@job_servers]);
102  
103 my $nw = Nagios::Passive->create(
104 gearman => $gearman,
105 key => "...", # if using encryption
106 service_description => $service_description,
107 check_name => $check_name,
108 host_name => $hostname,
109 return_code => 0, # 1 2 3
110 output => 'looks (good|bad|horrible) | performancedata'
111 );
112  
113 $nw->submit;
114  
115 =head1 DESCRIPTION
116  
117 This module gives you the ability to drop checkresults into
118 mod_gearman's check_result queue.
119  
120 The usage is described in L<Nagios::Passive>
121  
122 =cut
« blib/lib/Nagios/Passive/BulkResult.pm ↑ Summary blib/lib/Nagios/Passive/ResultPath.pm »