blib/lib/Zabbix/ServerScript/API.pm

filter: Statementstmt 100% 63 / 63 Branchbran 81.2% 13 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 92.3% 12 / 13 Podpod 50.0% 1 / 2 totaltotal 94.9% 94 / 99 CC 10 SCAR 11.0
CC
10
Cov
96%
init
6.0
_request
4.0
BEGIN
2.0
CRAP 3.0
« Build ↑ Summary _build/lib/MyBuilder.pm »
1 package Zabbix::ServerScript::API;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $AUTOLOAD;
12 1 our $logger;
13 1 our $ua;
14  
15 BEGIN {
16 1 ▶ eval {
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Zabbix::ServerScript::Config; 1 } } or die "Zabbix::ServerScript::Config must be present and filled with proper Zabbix credentials" 0 1 1
17 1 require Zabbix::ServerScript::Config;
18 1 1;
19 } or die q(Zabbix::ServerScript::Config must be present and filled with proper Zabbix credentials);
20 }
21  
22 sub AUTOLOAD {
23 3 ▶ my ($self, $params) = @_;
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
24 3 my $method_name = $AUTOLOAD;
25 3 $logger->debug(qq(Autoloading method $method_name));
26 3 $method_name =~ s/^.*:://;
27 3 $method_name =~ s/_/./;
28 3 return $self->_request($method_name, $params);
29 }
30  
31 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
32  
33 sub new {
34 3 ▶ my ($url) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
35 3 my $self = {
36 url => $url,
37 auth => undef,
38 };
39 3 bless $self;
40 3 return $self;
41 }
42  
43 sub init {
44 8 ▶ my ($api) = @_;
Subroutine init: called CC=6 SCAR=17.9
Pod documented
45 8 ▶ my $log_category = defined $ENV{LOG_CATEGORY} ? $ENV{LOG_CATEGORY} : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'LOG_CATEGORY'} ? : 8 0 8
46 8 $logger = Log::Log4perl::get_logger($log_category);
47  
48 8 my $api_config;
49 8 ▶ croak(q(Missing API configuration)) unless defined ($api_config = $Zabbix::ServerScript::Config->{api});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($api_config = $Zabbix::ServerScript::Config->{"api"}) 1 7 8
50 7 ▶ croak(q(API URL is not defined in config)) unless defined $api_config->{url};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_config->{"url"} 1 6 7
51 6 ▶ croak(qq(User credentials are not defined in config for API '$api')) unless (defined $api_config->{$api}->{login} and defined $api_config->{$api}->{password});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_config->{$api}{"login"} and defined $api_config->{$api}{"password"} 3 3 6
Condition100%3/3
defined $api_config->{$api}{"login"} and defined $api_config->{$api}{"password"}
!ll&&!rl&&r
213
MC/DC100%2/2
defined $api_config->{$api}{"login"} and defined $api_config->{$api}{"password"}
defined $api_config->{$api}{"login"} defined $api_config->{$api}{"password"}
Truth table100%3/3
A and B
A= defined $api_config->{$api}{"login"}B= defined $api_config->{$api}{"password"}
ABresult
0X0
100
111
52  
53 3 $ua = LWP::UserAgent->new;
54 3 $ua->timeout($api_config->{timeout});
55 3 my $self = new($api_config->{url});
56  
57 $self->{auth} = $self->user_login({
58 user => $api_config->{$api}->{login},
59 password => $api_config->{$api}->{password},
60 3 });
61 1 return $self;
62 }
63  
64 sub _request {
65 3 ▶ my ($self, $method_name, $params) = @_;
Subroutine _request: called CC=4 SCAR=13.9
66 3 ▶ $params = {} unless defined $params;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params 0 3 3
67 my $request_hashref = {
68 jsonrpc => q(2.0),
69 method => $method_name,
70 params => $params,
71 auth => $self->{auth},
72 3 id => 1,
73 };
74 3 my $request_json = encode_json($request_hashref);
75 3 $logger->debug(qq(API request: $request_json));
76 my $res = $ua->post(
77 $self->{url},
78 3 q(Content-Type) => q(application/json-rpc),
79 q(Content) => $request_json,
80 );
81 3 ▶ croak(qq(Cannot make request "$method_name": ) . $res->status_line) if $res->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_error 1 2 3
82 2 my $response_json = $res->content;
83 2 $logger->debug(qq(API response: $response_json));
84 2 my $response_hashref = decode_json($response_json);
85 2 ▶ if (defined $response_hashref->{error}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response_hashref->{"error"}) 1 1 2
86 1 croak(qq(Zabbix API error: $response_hashref->{error}->{message}. $response_hashref->{error}->{data}));
87 }
88 1 return $response_hashref->{result};
89 }
90  
91 1 1;
92  
93 __END__
94  
95 =encoding utf-8
96  
97 =head1 NAME
98  
99 Zabbix::ServerScript::API - Implementation of Zabbix JSON-RPC 2.0 API.
100  
101 =head1 SYNOPSIS
102  
103 #!/usr/bin/perl
104
105 use strict;
106 use warnings;
107 use utf8;
108 use Getopt::Long qw(:config bundling);
109 use Zabbix::ServerScript;
110
111 my $opt = {
112 unique => 1,
113 api => q(rw),
114 };
115
116 my @opt_specs = qw(
117 verbose|v+
118 debug
119 console
120 );
121
122 sub main {
123 GetOptions($opt, @opt_specs);
124 Zabbix::ServerScript::init($opt);
125 my $host = $zx_api->host_get({
126 filter => {
127 host => q(Zabbix server),
128 },
129 });
130 Zabbix::ServerScript::return_value($host->[0]->{hostid});
131 }
132  
133 main();
134  
135 =head1 DESCRIPTION
136  
137 Zabbix::ServerScript::API is a pure-perl implementation of Zabbix JSON-RPC 2.0 API. It is meant to be used as a part of Zabbix::ServerScript module.
138  
139 =head1 SUBROUTINES
140  
141 =head2 init($config_credential_section_name)
142  
143 Performs login to Zabbix with choosen credentials and returns an object of Zabbix::ServerScript::API.
144  
145 Any "foo_bar" method of this object will be transformed to "foo.bar" request to Zabbix API and return result of this request as a Perl structure.
146  
147 =head1 LICENSE
148  
149 Copyright (C) Anton Alekseyev.
150  
151 This library is free software; you can redistribute it and/or modify
152 it under the same terms as Perl itself.
153  
154 =head1 AUTHOR
155  
156 Anton Alekseyev E<lt>akint.wr+github@gmail.comE<gt>
157  
158 =cut
« Build ↑ Summary _build/lib/MyBuilder.pm »