API.pm

filter: Statementstmt 23.9% 28 / 117 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 45.0% 9 / 20 Podpod 100% 11 / 11 totaltotal 27.1% 48 / 177 CC 15 SCAR 22.5
CC
15
Cov
19%
new
30.0
login
12.0
raw_query
12.0
CRAP 9.4
« API/Screen.pm ↑ Summary API/Map.pm »
1 package Zabbix::API;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Params::Validate qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Carp qw/carp croak confess cluck/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 our $VERSION = '0.009';
16  
17 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
18  
19 0 my $class = shift;
20 0 my %args = validate(@_, { server => 1,
21 verbosity => 0,
22 env_proxy => 0,
23 lazy => 0,
24 ua => { optional => 1 } });
25  
26 0 my $self = \%args;
27  
28 # defaults
29 0 ▶ $self->{verbosity} = 0 unless exists $self->{verbosity};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"verbosity"} 0 0 0
30 0 ▶ $self->{env_proxy} = 0 unless exists $self->{env_proxy};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"env_proxy"} 0 0 0
31 0 ▶ $self->{lazy} = 0 unless exists $self->{lazy};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"lazy"} 0 0 0
32  
33 $self->{ua} = LWP::UserAgent->new(agent => 'Zabbix API client (libwww-perl)',
34 from => 'fabrice.gabolde@uperto.com',
35 show_progress => $self->{verbosity},
36 env_proxy => $self->{env_proxy},)
37 0 ▶ unless $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ua"} 0 0 0
38  
39 0 $self->{cookie} = '';
40  
41 0 bless $self, $class;
42  
43 0 return $self;
44  
45 }
46  
47 0 ▶sub useragent {
Subroutine useragent: not called CC=1 SCAR=6.9
Pod documented
48  
49 0 return shift->{ua};
50  
51 }
52  
53 0 ▶sub verbosity {
Subroutine verbosity: not called CC=2 SCAR=17.9
Pod documented
54  
55 ## mutator for verbosity
56  
57 0 my ($self, $value) = @_;
58  
59 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
60  
61 0 $self->{verbosity} = $value;
62 0 $self->{ua}->{show_progress} = $value;
63 0 return $self->{verbosity};
64  
65 } else {
66  
67 0 return $self->{verbosity};
68  
69 }
70  
71 }
72  
73 0 ▶sub cookie {
Subroutine cookie: not called CC=1 SCAR=6.9
Pod documented
74  
75 0 my $self = shift;
76  
77 0 return $self->{cookie};
78  
79 }
80  
81 0 ▶sub login {
Subroutine login: not called CC=3 SCAR=24.8
Pod documented
82  
83 0 my $self = shift;
84  
85 0 my %args = validate(@_, { user => 1,
86 password => 1 });
87  
88 0 my $response = $self->raw_query(method => 'user.login',
89 params => \%args);
90  
91 0 $self->{cookie} = '';
92  
93 0 my $decoded = eval { decode_json($response->decoded_content) };
94  
95 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
96  
97 # probably could not connect at all
98 0 croak sprintf('Could not connect: %s (%s)', $response->message, $response->code);
99  
100 }
101  
102 0 ▶ if ($decoded->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoded->{"error"}) 0 0 0
103  
104 0 croak 'Could not log in: '.$decoded->{error}->{data};
105  
106 }
107  
108 0 $self->{cookie} = $decoded->{result};
109  
110 0 $self->{user} = $args{user};
111  
112 0 return $self;
113  
114 }
115  
116 0 ▶sub logout {
Subroutine logout: not called CC=2 SCAR=17.9
Pod documented
117  
118 0 my $self = shift;
119  
120 0 my $decoded = decode_json($self->raw_query(method => 'user.logout')->decoded_content);
121  
122 0 ▶ if ($decoded->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoded->{"error"}) 0 0 0
123  
124 0 croak 'Could not log out: '.$decoded->{error}->{data};
125  
126 }
127  
128 0 $self->{cookie} = '';
129  
130 0 delete $self->{user};
131  
132 0 return $self;
133  
134 }
135  
136 0 ▶sub raw_query {
Subroutine raw_query: not called CC=3 SCAR=24.8
Pod documented
137  
138 0 my ($self, %args) = @_;
139  
140 0 state $global_id = int(rand(10000));
141  
142 # common parameters
143 0 $args{'jsonrpc'} = '2.0';
144 0 ▶ $args{'auth'} = $self->cookie || '';
Condition0%0/2
$self->cookie || ''
l!l
00
MC/DC0%0/1
$self->cookie || ''
$self->cookie
Truth table0%0/2
A
A= $self->cookie
Aresult
11
00
145 0 $args{'id'} = $global_id++;
146  
147 0 my $response = eval { $self->{ua}->post($self->{server},
148 0 'Content-Type' => 'application/json-rpc',
149 Content => encode_json(\%args)) };
150  
151 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
152  
153 0 my $error = $@;
154  
155 0 confess $error;
156  
157 }
158  
159 0 given ($self->{verbosity}) {
160  
161 0 when (1) {
162  
163 0 print $response->as_string;
164  
165 }
166  
167 0 when (2) {
168  
169 0 print Dumper($response);
170  
171 }
172  
173 0 default {
174  
175 }
176  
177 }
178  
179 0 return $response;
180  
181 }
182  
183 0 ▶sub query {
Subroutine query: not called CC=3 SCAR=24.8
Pod documented
184  
185 0 my $self = shift;
186  
187 0 my %args = validate(@_, { method => { TYPE => SCALAR },
188 params => { TYPE => HASHREF,
189 optional => 1 }});
190  
191 0 my $response = $self->raw_query(%args);
192  
193 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
194  
195 0 my $decoded = decode_json($response->decoded_content);
196  
197 0 ▶ if ($decoded->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoded->{"error"}) 0 0 0
198  
199 0 croak 'Zabbix server replied: '.$decoded->{error}->{data};
200  
201 }
202  
203 0 return $decoded->{result};
204  
205 }
206  
207 0 croak 'Received HTTP error: '.$response->decoded_content;
208  
209 }
210  
211 0 ▶sub api_version {
Subroutine api_version: not called CC=1 SCAR=6.9
Pod documented
212  
213 0 my $self = shift;
214  
215 0 my $response = $self->query(method => 'apiinfo.version');
216  
217 0 return $response;
218  
219 }
220  
221 0 ▶sub fetch {
Subroutine fetch: not called CC=2 SCAR=17.9
Pod documented
222  
223 0 my $self = shift;
224 0 my $class = shift;
225  
226 0 my %args = validate(@_,
227 { params => { type => HASHREF,
228 default => {} } });
229  
230 0 $class =~ s/^(?:Zabbix::API::)?/Zabbix::API::/;
231  
232 ## no critic (ProhibitStringyEval)
233 0 eval "require $class";
234 ## use critic
235  
236 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
237  
238 0 my $error = $@;
239  
240 0 croak qq{Could not load class '$class': $error};
241  
242 }
243  
244 my $response = $self->query(method => $class->prefix('.get'),
245 params => {
246 0 %{$args{params}},
247 $class->extension
248 });
249  
250 0 my $things = [ map { $class->new(root => $self, data => $_) } @{$response} ];
251  
252 0 return $things;
253  
254 }
255  
256 0 ▶sub fetch_single {
Subroutine fetch_single: not called CC=2 SCAR=17.9
Pod documented
257  
258 0 my ($self, @args) = @_;
259  
260 0 my $results = $self->fetch(@args);
261 0 my $result_count = scalar @{$results};
262  
263 0 ▶ if ($result_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_count > 1) 0 0 0
264  
265 0 croak qq{Too many results for 'fetch_single': expected 0 or 1, got $result_count"};
266  
267 }
268  
269 0 return $results->[0];
270  
271 }
272  
273 11 1;
274 __END__
« API/Screen.pm ↑ Summary API/Map.pm »