blib/lib/Zabbix/Sender.pm

filter: Statementstmt 53.9% 115 / 213 Branchbran 23.7% 19 / 80 Conditioncond 51.6% 16 / 31 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 73.0% 19 / 26 Podpod n/a totaltotal 47.5% 174 / 366 CC 57 SCAR 40.4
CC
57
Cov
46%
_decode_answer
156.0
_send
156.0
bulk_send
42.0
CRAP 56.6
↑ Summary
1 package Zabbix::Sender;
2 # ABSTRACT: A pure-perl implementation of zabbix-sender.
3 3 $Zabbix::Sender::VERSION = '0.07';
4 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Net::Domain;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 3 has 'server' => (
16 'is' => 'rw',
17 'isa' => Str,
18 'required' => 1,
19 );
20  
21 3 has 'port' => (
22 'is' => 'rw',
23 'isa' => Int,
24 'default' => 10051,
25 );
26  
27 3 has 'timeout' => (
28 'is' => 'rw',
29 'isa' => Int,
30 'default' => 30,
31 );
32  
33 3 has 'hostname' => (
34 'is' => 'rw',
35 'isa' => Str,
36 'lazy' => 1,
37 'builder' => '_init_hostname',
38 );
39  
40 3 has 'interval' => (
41 'is' => 'rw',
42 'isa' => Int,
43 'default' => 1,
44 );
45  
46 3 has 'retries' => (
47 'is' => 'rw',
48 'isa' => Int,
49 'default' => 3,
50 );
51  
52 3 has 'keepalive' => (
53 'is' => 'rw',
54 'isa' => Bool,
55 'default' => 0,
56 );
57  
58 3 has '_json' => (
59 'is' => 'rw',
60 'isa' => InstanceOf['JSON'],
61 'lazy' => 1,
62 'builder' => '_init_json',
63 );
64  
65 3 has '_last_sent' => (
66 'is' => 'rw',
67 'isa' => Int,
68 'default' => 0,
69 );
70  
71 3 has '_socket' => (
72 'is' => 'rw',
73 'isa' => Maybe[ InstanceOf['IO::Socket'] ],
74 );
75  
76 has 'response' => (
77 'is' => 'rw',
78 'isa' => HashRef,
79 2 ▶ 'default' => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
80 3 );
81  
82 has 'bulk_buf' => (
83 'is' => 'rw',
84 'isa' => ArrayRef,
85 2 ▶ 'default' => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
86 3 );
87  
88 3 has '_info' => (
89 'is' => 'rw',
90 'isa' => Str,
91 );
92  
93 3 has 'strict' => (
94 'is' => 'rw',
95 'isa' => Bool,
96 'default' => 0,
97 );
98  
99  
100  
101 1 ▶sub _init_json {
Subroutine _init_json: called CC=1 SCAR=0.0
102 1 my $self = shift;
103  
104 1 my $JSON = JSON::->new->utf8();
105  
106 1 return $JSON;
107 }
108  
109  
110 0 ▶sub _init_hostname {
Subroutine _init_hostname: not called CC=1 SCAR=6.9
111 0 my $self = shift;
112  
113 0 return Net::Domain::hostname() . '.' . Net::Domain::hostdomain();
114 }
115  
116  
117 3 has 'zabbix_template_1_8' => (
118 'is' => 'ro',
119 'isa' => Str,
120 'default' => "a4 b V V a*",
121 );
122  
123  
124 2 ▶sub _encode_request {
Subroutine _encode_request: called CC=3 SCAR=11.0
125 2 my $self = shift;
126 2 my $values = shift;
127  
128 2 my @data;
129 2 for my $ref (@{$values}) {
130 2 my %data = (
131 'host' => $ref->[0],
132 'key' => $ref->[1],
133 'value' => $ref->[2],
134 );
135 2 ▶ $data{'clock'} = $ref->[3]
Branch: 1 branch
BranchTrueFalseTotal
if $ref->[3] 0 2 2
136 if $ref->[3];
137 2 push @data, \%data;
138 }
139  
140 2 my $data = {
141 'request' => 'sender data',
142 'data' => \@data,
143 };
144  
145 2 my $output = '';
146 2 my $json = $self->_json()->encode($data);
147  
148 # turn on byte semantics to get the real length of the string
149 3 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 2 my $length = length($json);
151 3 ▶ no bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
152  
153 ## no critic (ProhibitBitwiseOperators)
154 2 $output = pack(
155 $self->zabbix_template_1_8(),
156 "ZBXD", 0x01,
157 $length, 0x00,
158 $json
159 );
160 ## use critic
161  
162 2 return $output;
163 }
164  
165  
166 0 ▶sub _check_info {
Subroutine _check_info: not called CC=4 SCAR=30.0
167 0 my $self = shift;
168 0 ▶ if($self->_info() !~ /^Processed:?\s+(\d+);?\s+Failed:?\s+(\d+);?\s+Total:?\s+(\d+);?\s+Seconds\s+spent:?\s+\d+.\d+$/i )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_info =~ /^Processed:?\s+(\d+);?\s+Failed:?\s+(\d+);?\s+Total:?\s+(\d+);?\s+Seconds\s+spent:?\s+\d+.\d+$/i) 0 0 0
169 {
170 0 return "Failed to parse info from zabbix server: ", $self->_info();
171 }
172 0 my($processed, $failed, $total) = (int($1), int($2), int($3));
173 0 ▶ return if $processed eq $total and $failed eq 0;
Branch: 1 branch
BranchTrueFalseTotal
if $processed eq $total and $failed eq 0 0 0 0
Condition0%0/3
$processed eq $total and $failed eq 0
!ll&&!rl&&r
000
MC/DC0%0/2
$processed eq $total and $failed eq 0
$processed eq $total $failed eq 0
Truth table0%0/3
A and B
A= $processed eq $totalB= $failed eq 0
ABresult
0X0
100
111
174 0 return "(Processed, failed, total) != (x, 0, x) in info from zabbix server: ", $self->_info()
175 }
176  
177  
178 0 ▶sub _decode_answer {
Subroutine _decode_answer: not called CC=12 SCAR=50.5
179 0 my $self = shift;
180 0 my $data = shift;
181  
182 0 $self->_info('');
183 0 my ( $ident, $answer );
184 0 ▶ $ident = substr( $data, 0, 4 ) if length($data) > 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $data > 3 0 0 0
185 0 ▶ if ($ident and $ident eq 'ZBXD') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident and $ident eq 'ZBXD') { } 0 0 0
Condition0%0/3
$ident and $ident eq "ZBXD"
!ll&&!rl&&r
000
MC/DC0%0/2
$ident and $ident eq "ZBXD"
$ident $ident eq "ZBXD"
Truth table0%0/3
A and B
A= $identB= $ident eq "ZBXD"
ABresult
0X0
100
111
186 # Headers are optional since Zabbix 2.0.8 and 2.1.7
187 0 ▶ if (length($data) > 12) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > 12) { } 0 0 0
188 0 $answer = substr( $data, 13 );
189 } else {
190 0 carp "Invalid response header received: '$data' (length: ", length($data), ")";
191 0 return;
192 }
193 } else {
194 0 $answer = $data;
195 }
196  
197 0 ▶ if ( $answer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer) 0 0 0
198 0 my $ref = $self->_json()->decode($answer);
199 0 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 0 0 0
200 0 $self->response($ref);
201 0 ▶ if ( $ref->{'response'} eq 'success' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"response"} eq "success") 0 0 0
202 0 $self->_info($ref->{'info'});
203 0 ▶ if($self->strict())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strict) 0 0 0
204 {
205 0 my $msg = $self->_check_info();
206 0 ▶ carp $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 0 0
207 0 ▶ return 0 if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 0 0
208 }
209 0 return 1;
210 }
211 0 ▶ return $ref->{'response'} eq 'success' ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$ref->{'response'} eq 'success' ? : 0 0 0
212 } else {
213 0 $self->response(undef);
214 }
215 }
216 0 return;
217 }
218  
219  
220 # DGR: Anything but send just doesn't makes sense here. And since this is a pure-OO module
221 # and if the implementor avoids indirect object notation you should be fine.
222 ## no critic (ProhibitBuiltinHomonyms)
223 0 ▶sub send {
Subroutine send: not called CC=5 SCAR=34.0
224 ## use critic
225 0 my $self = shift;
226 0 my $item = shift;
227 0 my $value = shift;
228 0 ▶ my $clock = shift || time;
Condition0%0/2
shift() || time
l!l
00
MC/DC0%0/1
shift() || time
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
229  
230 0 my $data = $self->_encode_request( [ [ $self->hostname(), $item, $value, $clock ] ] );
231 0 my $status = 0;
232 0 foreach my $i ( 1 .. $self->retries() ) {
233 0 ▶ if ( $self->_send( $data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_send($data)) 0 0 0
234 0 $status = 1;
235 0 last;
236 }
237 }
238  
239 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 0 0 0
240 0 return $self->response;
241 }
242 else { ## Should this die/croak/warn? Is this for timeout?
243 0 return;
244 }
245  
246 }
247  
248 0 ▶sub _send {
Subroutine _send: not called CC=12 SCAR=50.5
249 0 my $self = shift;
250 0 my $data = shift;
251  
252 0 ▶ if ( time() - $self->_last_sent() < $self->interval() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $self->_last_sent < $self->interval) 0 0 0
253 0 my $sleep = $self->interval() - ( time() - $self->_last_sent() );
254 0 ▶ $sleep ||= 0;
Condition0%0/2
$sleep ||= 0
l!l
00
MC/DC0%0/1
$sleep ||= 0
$sleep
Truth table0%0/2
A
A= $sleep
Aresult
11
00
255 0 sleep $sleep;
256 }
257  
258 0 ▶ unless ($self->_socket()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_socket) 0 0 0
259 return
260 0 ▶ unless $self->_connect();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_connect 0 0 0
261 }
262 0 $self->_socket()->send( $data );
263 0 my $Select = IO::Select::->new($self->_socket());
264 0 my $status = 0;
265 0 my $recvstarttime = time;
266 0 my $reply = '';
267 0 ▶ while($recvstarttime + $self->timeout() > time)
Branch: 1 branch
BranchTrueFalseTotal
if ($recvstarttime + $self->timeout > time) 0 0 0
268 {
269 0 my @Handles = $Select->can_read( $self->timeout() );
270  
271 0 ▶ if ( scalar(@Handles) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @Handles > 0) 0 0 0
272 0 my $result;
273 0 $self->_socket()->recv( $result, 1024 );
274 0 $reply .= $result;
275 0 ▶ next if length($reply) < 13;
Branch: 1 branch
BranchTrueFalseTotal
if length $reply < 13 0 0 0
276 # we need to recv until we have read either as much data as indicated
277 # in the header or there is an error. so we have to decode the header
278 # here, before calling _decode_answer.
279 0 my($ZBXD, $one, $len1, $len2, $json) = unpack 'A4 C V2 A*', $reply;
280 0 my $expected_length = 13 + $len1 + ($len2 << 32);
281 0 ▶ next if length($reply) < $expected_length;
Branch: 1 branch
BranchTrueFalseTotal
if length $reply < $expected_length 0 0 0
282 0 ▶ if ( $self->_decode_answer($reply) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_decode_answer($reply)) 0 0 0
283 0 $status = 1;
284 }
285 0 last;
286 }
287 }
288 0 ▶ $self->_disconnect() unless $self->keepalive();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keepalive 0 0 0
289 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 0 0 0
290 0 return $status;
291 }
292 else {
293 0 return;
294 }
295 }
296  
297 0 ▶sub _connect {
Subroutine _connect: not called CC=2 SCAR=17.9
298 0 my $self = shift;
299  
300 0 ▶ my $Socket = IO::Socket::INET::->new(
Branch: 1 branch
BranchTrueFalseTotal
my $Socket = "IO::Socket::INET"->new("PeerAddr", $self->server, "PeerPort", $self->port, "Proto", "tcp", "Timeout", $self->timeout) or return 0 0 0
301 PeerAddr => $self->server(),
302 PeerPort => $self->port(),
303 Proto => 'tcp',
304 Timeout => $self->timeout(),
305 ) or return;
306  
307 0 $self->_socket($Socket);
308  
309 0 return 1;
310 }
311  
312 2 ▶sub _disconnect {
Subroutine _disconnect: called CC=2 SCAR=9.2
313 2 my $self = shift;
314  
315 2 ▶ if(!$self->_socket()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_socket) 2 0 2
316 2 return;
317 }
318  
319 0 $self->_socket()->close();
320 0 $self->_socket(undef);
321  
322 0 return 1;
323 }
324  
325  
326 9 ▶sub bulk_buf_add {
Subroutine bulk_buf_add: called CC=19 SCAR=29.6
327 9 my $self = shift;
328  
329 9 my @values;
330 9 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 12 3 15
331 12 my $arg = shift;
332 12 ▶ if ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) { } 12 0 12
333 12 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 5 7 12
334 5 ▶ if (ref $arg eq 'ARRAY' and (@{$arg} == 2 or @{$arg} == 3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY' and @{$arg;} == 2 || @{$arg;} == 3) { } 2 3 5
Condition83%5/6
ref $arg eq "ARRAY" and @{$arg;} == 2 || @{$arg;} == 3
!ll&&!rl&&r
122
@{$arg;} == 2 or @{$arg;} == 3
l!l&&r!l&&!r
022
MC/DC66%2/3
ref $arg eq "ARRAY" and @{$arg;} == 2 || @{$arg;} == 3
ref $arg eq "ARRAY" @{$arg;} == 2 @{$arg;} == 3
Truth table75%3/4
A and B or C
A= ref $arg eq "ARRAY"B= @{$arg;} == 2C= @{$arg;} == 3
ABCresult
0XX0
1000
11X1
1011
335 # Array of (key, value[, clock])
336 2 ▶ push @values, [ $self->hostname(),
Condition100%2/2
$arg->[2] || time
l!l
11
MC/DC100%1/1
$arg->[2] || time
$arg->[2]
Truth table100%2/2
A
A= $arg->[2]
Aresult
11
00
337 $arg->[0], $arg->[1], $arg->[2] || time ];
338 } else {
339 3 carp "Invalid argument: Expected ARRAY with 2 or 3 elements";
340 3 return;
341 }
342 } else {
343 7 my $arg2 = shift;
344 7 ▶ if ($arg2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg2) { } 6 1 7
345 6 ▶ if (ref $arg2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg2) { } 4 2 6
346 4 ▶ unless (ref $arg2 eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $arg2 eq "ARRAY") 1 3 4
347 1 carp "Invalid argument: Expected ARRAY";
348 1 return;
349 }
350 3 my $hostname = $arg;
351 3 for my $ref (@{$arg2}) {
352 5 ▶ if (ref $ref and ref $ref eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref and ref $ref eq 'ARRAY' and @{$ref;} == 2 || @{$ref;} == 3) { } 4 1 5
Condition66%6/9
ref $ref and ref $ref eq "ARRAY" and @{$ref;} == 2 || @{$ref;} == 3
!ll&&!rl&&r
104
ref $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
104
@{$ref;} == 2 or @{$ref;} == 3
l!l&&r!l&&!r
130
MC/DC25%1/4
ref $ref and ref $ref eq "ARRAY" and @{$ref;} == 2 || @{$ref;} == 3
ref $ref ref $ref eq "ARRAY" @{$ref;} == 2 @{$ref;} == 3
Truth table60%3/5
A and B and C or D
A= ref $refB= ref $ref eq "ARRAY"C= @{$ref;} == 2D= @{$ref;} == 3
ABCDresult
0XXX0
10XX0
11000
111X1
11011
353 4 and (@{$ref} == 2 or @{$ref} == 3)) {
354 # (key, value[, clock])
355 4 ▶ $ref->[2] = time
Branch: 1 branch
BranchTrueFalseTotal
unless $ref->[2] 2 2 4
356 unless $ref->[2];
357 4 ▶ push @values, [ $hostname, $ref->[0],
Condition50%1/2
$ref->[2] || time
l!l
40
MC/DC0%0/1
$ref->[2] || time
$ref->[2]
Truth table50%1/2
A
A= $ref->[2]
Aresult
11
00
358 $ref->[1], $ref->[2] || time ];
359 } else {
360 1 carp "Invalid argument: ARRAY had not 2 or 3 elements";
361 1 return;
362 }
363 }
364 } else {
365 # (key, value[, clock])
366 2 my $key = $arg;
367 2 my $value = $arg2;
368 2 ▶ my $clock = shift || time;
Condition100%2/2
shift() || time
l!l
11
MC/DC100%1/1
shift() || time
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
369 2 push @values, [ $self->hostname(), $key, $value, $clock ];
370 }
371 } else {
372 1 carp "Insufficient number of arguments";
373 1 return;
374 }
375 }
376 } else {
377 0 carp "Insufficient number of arguments";
378 0 return;
379 }
380 }
381  
382 3 push @{$self->bulk_buf()}, @values;
383 3 return 1;
384 }
385  
386  
387 1 ▶sub bulk_buf_clear {
Subroutine bulk_buf_clear: called CC=1 SCAR=0.0
388 1 my $self = shift;
389  
390 1 $self->bulk_buf([]);
391 }
392  
393  
394 0 ▶sub bulk_send {
Subroutine bulk_send: not called CC=6 SCAR=37.4
395 0 my $self = shift;
396  
397 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
398 0 ▶ $self->bulk_buf_add(@_)
Branch: 1 branch
BranchTrueFalseTotal
$self->bulk_buf_add(@_) or return 0 0 0
399 or return;
400 }
401  
402 0 my $data = $self->_encode_request( $self->bulk_buf() );
403 0 my $status = 0;
404 0 foreach my $i ( 1 .. $self->retries() ) {
405 0 ▶ if ( $self->_send( $data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_send($data)) 0 0 0
406 0 $status = 1;
407 0 last;
408 }
409 }
410  
411 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 0 0 0
412 0 $self->bulk_buf_clear();
413 0 return 1;
414 }
415 else {
416 0 return;
417 }
418  
419 }
420  
421  
422 2 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=1 SCAR=0.0
423 2 my $self = shift;
424  
425 2 $self->_disconnect();
426  
427 2 return 1;
428 }
429  
430 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
431 3 __PACKAGE__->meta->make_immutable;
432  
433  
434 3 1; # End of Zabbix::Sender
435  
436 __END__
↑ Summary