blib/lib/Zabbix/Tiny.pm

filter: Statementstmt 94.7% 144 / 152 Branchbran 82.5% 33 / 40 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 94.7% 18 / 19 Podpod 33.3% 2 / 6 totaltotal 88.4% 199 / 225 CC 23 SCAR 13.7
CC
23
Cov
91%
prepare
8.0
do
6.1
login
4.0
CRAP 3.9
↑ Summary
1 package Zabbix::Tiny;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our $VERSION = "2.0.1";
11  
12 8 has 'server' => (
13 is => 'rw',
14 required => 1,
15 );
16 8 has 'user' => (
17 is => 'rw',
18 required => 1,
19 );
20 8 has 'password' => (
21 is => 'rw',
22 required => 1,
23 );
24 8 has 'zabbix_method' => ( is => 'ro' );
25 8 has 'zabbix_params' => ( is => 'ro' );
26 8 has 'auth' => ( is => 'ro' );
27 has 'ua' => (
28 is => 'ro',
29 lazy => 1,
30 0 ▶ default => sub { LWP::UserAgent->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 8 );
32 8 ▶has 'id' => ( is => 'ro', default => sub { 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
33 8 has 'post_response' => ( is => 'ro' );
34 8 has 'last_response' => ( is => 'ro' );
35 8 has 'json_request' => ( is => 'ro' );
36 8 has 'json_response' => ( is => 'ro' );
37 8 ▶has 'verify_hostname' => ( is => 'rw', default => sub { 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
38 8 has 'ssl_opts' => ( is => 'rw' );
39 8 has 'delay' => ( is => 'rw' );
40 8 has 'request' => ( is => 'ro' );
41 8 has 'json_prepared' => ( is => 'ro' );
42 8 ▶has 'json_executed' => ( is => 'ro', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
43 8 has 'redo' => ( is => 'ro' );
44 8 has 'version' => ( is => 'lazy' );
45  
46 8 my @content_type = ( 'content-type', 'application/json', );
47  
48 8 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=12.6
Pod undocumented
49 8 my $self = shift;
50 8 my $ua = $self->ua;
51 8 my $url = $self->server;
52  
53 8 my $json_data = {
54 jsonrpc => '2.0',
55 id => $self->id,
56 method => 'user.login',
57 params => {
58 user => $self->user,
59 password => $self->password,
60 },
61 };
62  
63 8 ▶ if ( $self->verify_hostname == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verify_hostname == 0) 0 8 8
64 0 $ua->ssl_opts( verify_hostname => 0 );
65 }
66  
67 8 ▶ if ( $self->ssl_opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ssl_opts) 0 8 8
68 0 $ua->ssl_opts( %{ $self->{ssl_opts} } );
69 }
70 }
71  
72  
73 7 ▶sub _build_version {
Subroutine _build_version: called CC=2 SCAR=7.1
74 7 my $self = shift;
75 7 my $id = $self->id;
76 7 my $ua = $self->ua;
77 7 my $url = $self->server;
78  
79 7 my $json_data = {
80 jsonrpc => '2.0',
81 id => $self->id,
82 method => 'apiinfo.version',
83 params => {
84 },
85 };
86  
87 7 my $encoded_json = encode_json ($json_data);
88  
89 7 my $post_response = $ua->post(
90 $self->server,
91 @content_type,
92 Content => $encoded_json
93 );
94  
95 7 _validate_http_response($post_response);
96  
97 6 my $response_content = decode_json( $post_response->{_content} );
98  
99 6 ▶ if ( $response_content->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response_content->{'error'}) { } 0 6 6
100 0 my $error = $response_content->{error}->{data};
101 0 croak("Error: $error");
102 }
103 else {
104 6 my $version = version->new($response_content->{'result'});
105 6 return($version);
106 }
107 }
108  
109  
110 8 ▶sub login {
Subroutine login: called CC=4 SCAR=13.9
Pod undocumented
111 8 my $self = shift;
112 8 my $id = $self->id;
113 8 my $ua = $self->ua;
114 8 my $url = $self->server;
115  
116 8 my $json_data = {
117 jsonrpc => '2.0',
118 id => $id,
119 method => 'user.login',
120 };
121  
122 8 ▶ if ( $self->version lt "6.0" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version lt '6.0') { } 1 6 7
123 $json_data->{params} = {
124 1 user => $self->user,
125 password => $self->password,
126 };
127 } else {
128 $json_data->{params} = {
129 6 username => $self->user,
130 password => $self->password,
131 };
132 }
133  
134 7 my $json = encode_json($json_data);
135 7 my $response = $ua->post( $url, @content_type, Content => $json );
136  
137 7 _validate_http_response($response);
138  
139 7 ▶ my $content = decode_json( $response->{_content} ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
my $content = &decode_json($response->{"_content"}) or die $! 7 0 7
140  
141 7 ▶ if ( $content->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{"error"}) 1 6 7
142 1 my $error_data = $content->{error}->{data};
143 1 my $error_msg = $content->{error}->{message};
144 1 my $error_code = $content->{error}->{code};
145 1 my $error = "Error from Zabbix (code $error_code): $error_msg $error_data";
146 1 croak($error);
147 }
148  
149 6 $self->{auth} = $content->{'result'};
150 }
151  
152  
153 14 ▶sub _validate_http_response {
Subroutine _validate_http_response: called CC=3 SCAR=11.0
154 14 my $response = shift;
155  
156 14 ▶ if ( $response->{_rc} !~ /2\d\d/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->{"_rc"} =~ /2\d\d/) 1 13 14
157 1 my $error_message = "HTTP error ";
158 1 $error_message .= "(code $response->{_rc}) ";
159 1 ▶ $error_message .= $response->{_msg} // q{};
Condition50%1/2
$response->{"_msg"} // ""
l!l
10
MC/DC0%0/1
$response->{"_msg"} // ""
$response->{"_msg"}
Truth table50%1/2
A
A= $response->{"_msg"}
Aresult
11
00
160 1 croak($error_message);
161 }
162 }
163  
164 13 ▶sub prepare {
Subroutine prepare: called CC=8 SCAR=20.9
Pod documented
165 13 my $self = shift;
166 13 my $method = shift;
167 13 $self->{ id }++;
168 13 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) 6 7 13
169 6 $self->{zabbix_method} = $method;
170 6 undef $self->{zabbix_params};
171 6 my @args = @_;
172 6 ▶ if ( scalar @args == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == 1) { } 3 3 6
173 3 $self->{zabbix_params} = $args[0];
174 }
175 else {
176 3 my %params = @args;
177 3 $self->{zabbix_params} = \%params;
178 }
179 }
180 13 ▶ unless ($self->{zabbix_method} eq 'apiinfo.version') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"zabbix_method"} eq "apiinfo.version") 9 4 13
181 9 ▶ login($self) if ( !$self->auth );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auth 1 8 9
182 }
183 13 ▶ if ( !$self->zabbix_method ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->zabbix_method) 0 13 13
184 0 croak("No Zabbix API method defined");
185 }
186 $self->{request} = {
187 13 jsonrpc => '2.0',
188 id => $self->id,
189 method => $self->zabbix_method,
190 params => $self->zabbix_params,
191 };
192 13 ▶ unless ($self->{zabbix_method} eq 'apiinfo.version') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"zabbix_method"} eq "apiinfo.version") 9 4 13
193 9 $self->{request}->{auth} = $self->auth;
194 }
195 13 ▶ $self->{json_prepared} = encode_json( $self->request ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"json_prepared"} = &encode_json(scalar $self->request) or die $! 13 0 13
196 }
197  
198 7 ▶sub execute {
Subroutine execute: called CC=2 SCAR=6.9
Pod undocumented
199 7 my $self = shift;
200 7 my $ua = $self->ua;
201 7 $self->{post_response} = $ua->post( $self->server, @content_type,
202 Content => $self->json_prepared );
203 7 $self->{json_request} = $self->{post_response}->{'_request'}->{_content};
204 7 $self->{json_response} = $self->post_response->{_content};
205 7 ▶ if ( $self->post_response->{_content} eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->post_response->{"_content"} eq "") 1 6 7
206 1 croak( "Empty response received from the Zabbix API. This can indicate an error on the API side like running out of memory." );
207 }
208 6 $self->{last_response} = decode_json( $self->{post_response}->{_content} );
209 6 my $method = $self->zabbix_method;
210 6 my $params = $self->zabbix_params;
211 6 prepare($self); ## Rerun prepare to get the new request id.
212 }
213  
214 7 ▶sub do {
Subroutine do: called CC=6 SCAR=18.1
Pod documented
215 7 my $self = shift;
216 7 my $method = shift;
217 7 my @args = @_;
218 7 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) 5 2 7
219 5 prepare( $self, $method, @args );
220 }
221 7 execute($self);
222 6 ▶ if ( $self->{last_response}->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'last_response'}{'error'}) { } 1 5 6
223 1 my $error = $self->{last_response}->{error}->{data};
224 1 ▶ if ( ( !$self->{redo} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'redo'} and $error eq 'Session terminated, re-login, please.') { } 1 0 1
Condition33%1/3
not $self->{"redo"} and $error eq "Session terminated, re-login, please."
!ll&&!rl&&r
001
MC/DC0%0/2
not $self->{"redo"} and $error eq "Session terminated, re-login, please."
not $self->{"redo"} $error eq "Session terminated, re-login, please."
Truth table33%1/3
not(A) and B
A= not $self->{"redo"}B= $error eq "Session terminated, re-login, please."
ABresult
0X0
100
111
225 && ( $error eq 'Session terminated, re-login, please.' ) )
226 {
227 1 $self->{redo}++;
228 1 delete( $self->{auth} );
229 1 prepare($self);
230 1 &do($self); ## Need to use "&" because "do" is a perl keyword.
231 }
232 else {
233 0 croak("Error: $error");
234 }
235 }
236 else {
237 5 ▶ delete( $self->{redo} ) if $self->redo;
Branch: 1 branch
BranchTrueFalseTotal
if $self->redo 1 4 5
238 5 $self->{json_executed} = 1;
239 5 return $self->{last_response}->{'result'};
240 }
241 }
242  
243 9 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=6.9
Pod undocumented
244 9 my $self = shift;
245 9 my $method = shift;
246 9 my $ua = $self->ua;
247 9 my $auth = $self->auth;
248 9 my $url = $self->server;
249
250 9 ▶ return unless ($ua);
Branch: 1 branch
BranchTrueFalseTotal
unless $ua 2 7 9
251
252 my $json_data = {
253 jsonrpc => '2.0',
254 id => ++$self->{ id },
255 7 method => 'user.logout',
256 auth => $auth,
257 };
258 7 my $json = encode_json($json_data);
259 7 $self->{post_response} = $ua->post( $url, @content_type, Content => $json );
260 }
261  
262 8 1;
263  
264 __END__
↑ Summary