line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Zabbix::ServerScript::API; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1302
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
18
|
|
5
|
1
|
|
|
1
|
|
3
|
use Data::Dumper; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
6
|
1
|
|
|
1
|
|
3
|
use JSON; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
7
|
1
|
|
|
1
|
|
628
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
26619
|
|
|
1
|
|
|
|
|
29
|
|
8
|
1
|
|
|
1
|
|
6
|
use Log::Log4perl; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
9
|
1
|
|
|
1
|
|
34
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
75
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $AUTOLOAD; |
12
|
|
|
|
|
|
|
our $logger; |
13
|
|
|
|
|
|
|
our $ua; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
BEGIN { |
16
|
1
|
50
|
|
1
|
|
2
|
eval { |
17
|
1
|
|
|
|
|
474
|
require Zabbix::ServerScript::Config; |
18
|
1
|
|
|
|
|
401
|
1; |
19
|
|
|
|
|
|
|
} or die q(Zabbix::ServerScript::Config must be present and filled with proper Zabbix credentials); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub AUTOLOAD { |
23
|
3
|
|
|
3
|
|
4
|
my ($self, $params) = @_; |
24
|
3
|
|
|
|
|
3
|
my $method_name = $AUTOLOAD; |
25
|
3
|
|
|
|
|
9
|
$logger->debug(qq(Autoloading method $method_name)); |
26
|
3
|
|
|
|
|
25
|
$method_name =~ s/^.*:://; |
27
|
3
|
|
|
|
|
4
|
$method_name =~ s/_/./; |
28
|
3
|
|
|
|
|
4
|
return $self->_request($method_name, $params); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
0
|
|
|
sub DESTROY {} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub new { |
34
|
3
|
|
|
3
|
0
|
4
|
my ($url) = @_; |
35
|
3
|
|
|
|
|
5
|
my $self = { |
36
|
|
|
|
|
|
|
url => $url, |
37
|
|
|
|
|
|
|
auth => undef, |
38
|
|
|
|
|
|
|
}; |
39
|
3
|
|
|
|
|
3
|
bless $self; |
40
|
3
|
|
|
|
|
2
|
return $self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub init { |
44
|
8
|
|
|
8
|
1
|
10
|
my ($api) = @_; |
45
|
8
|
50
|
|
|
|
20
|
my $log_category = defined $ENV{LOG_CATEGORY} ? $ENV{LOG_CATEGORY} : __PACKAGE__; |
46
|
8
|
|
|
|
|
20
|
$logger = Log::Log4perl::get_logger($log_category); |
47
|
|
|
|
|
|
|
|
48
|
8
|
|
|
|
|
153
|
my $api_config; |
49
|
8
|
100
|
|
|
|
223
|
croak(q(Missing API configuration)) unless defined ($api_config = $Zabbix::ServerScript::Config->{api}); |
50
|
7
|
100
|
|
|
|
145
|
croak(q(API URL is not defined in config)) unless defined $api_config->{url}; |
51
|
6
|
100
|
100
|
|
|
319
|
croak(qq(User credentials are not defined in config for API '$api')) unless (defined $api_config->{$api}->{login} and defined $api_config->{$api}->{password}); |
52
|
|
|
|
|
|
|
|
53
|
3
|
|
|
|
|
9
|
$ua = LWP::UserAgent->new; |
54
|
3
|
|
|
|
|
25
|
$ua->timeout($api_config->{timeout}); |
55
|
3
|
|
|
|
|
128
|
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
|
|
|
|
|
15
|
}); |
61
|
1
|
|
|
|
|
5
|
return $self; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _request { |
65
|
3
|
|
|
3
|
|
4
|
my ($self, $method_name, $params) = @_; |
66
|
3
|
50
|
|
|
|
6
|
$params = {} unless defined $params; |
67
|
|
|
|
|
|
|
my $request_hashref = { |
68
|
|
|
|
|
|
|
jsonrpc => q(2.0), |
69
|
|
|
|
|
|
|
method => $method_name, |
70
|
|
|
|
|
|
|
params => $params, |
71
|
|
|
|
|
|
|
auth => $self->{auth}, |
72
|
3
|
|
|
|
|
12
|
id => 1, |
73
|
|
|
|
|
|
|
}; |
74
|
3
|
|
|
|
|
26
|
my $request_json = encode_json($request_hashref); |
75
|
3
|
|
|
|
|
8
|
$logger->debug(qq(API request: $request_json)); |
76
|
|
|
|
|
|
|
my $res = $ua->post( |
77
|
|
|
|
|
|
|
$self->{url}, |
78
|
3
|
|
|
|
|
20
|
q(Content-Type) => q(application/json-rpc), |
79
|
|
|
|
|
|
|
q(Content) => $request_json, |
80
|
|
|
|
|
|
|
); |
81
|
3
|
100
|
|
|
|
115
|
croak(qq(Cannot make request "$method_name": ) . $res->status_line) if $res->is_error; |
82
|
2
|
|
|
|
|
58
|
my $response_json = $res->content; |
83
|
2
|
|
|
|
|
56
|
$logger->debug(qq(API response: $response_json)); |
84
|
2
|
|
|
|
|
17
|
my $response_hashref = decode_json($response_json); |
85
|
2
|
100
|
|
|
|
4
|
if (defined $response_hashref->{error}){ |
86
|
1
|
|
|
|
|
92
|
croak(qq(Zabbix API error: $response_hashref->{error}->{message}. $response_hashref->{error}->{data})); |
87
|
|
|
|
|
|
|
} |
88
|
1
|
|
|
|
|
4
|
return $response_hashref->{result}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |