line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Zabbix::ServerScript::API; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1230
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
5
|
1
|
|
|
1
|
|
3
|
use Data::Dumper; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
6
|
1
|
|
|
1
|
|
3
|
use JSON; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
7
|
1
|
|
|
1
|
|
633
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
27179
|
|
|
1
|
|
|
|
|
31
|
|
8
|
1
|
|
|
1
|
|
8
|
use Log::Log4perl; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
11
|
|
9
|
1
|
|
|
1
|
|
40
|
use Carp; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
77
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $AUTOLOAD; |
12
|
|
|
|
|
|
|
our $logger; |
13
|
|
|
|
|
|
|
our $ua; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
BEGIN { |
16
|
1
|
50
|
|
1
|
|
1
|
eval { |
17
|
1
|
|
|
|
|
541
|
require Zabbix::ServerScript::Config; |
18
|
1
|
|
|
|
|
438
|
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
|
|
3
|
my ($self, $params) = @_; |
24
|
3
|
|
|
|
|
3
|
my $method_name = $AUTOLOAD; |
25
|
3
|
|
|
|
|
11
|
$logger->debug(qq(Autoloading method $method_name)); |
26
|
3
|
|
|
|
|
27
|
$method_name =~ s/^.*:://; |
27
|
3
|
|
|
|
|
5
|
$method_name =~ s/_/./; |
28
|
3
|
|
|
|
|
6
|
return $self->_request($method_name, $params); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
0
|
|
|
sub DESTROY {} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub new { |
34
|
3
|
|
|
3
|
0
|
3
|
my ($url) = @_; |
35
|
3
|
|
|
|
|
6
|
my $self = { |
36
|
|
|
|
|
|
|
url => $url, |
37
|
|
|
|
|
|
|
auth => undef, |
38
|
|
|
|
|
|
|
}; |
39
|
3
|
|
|
|
|
2
|
bless $self; |
40
|
3
|
|
|
|
|
3
|
return $self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub init { |
44
|
8
|
|
|
8
|
1
|
8
|
my ($api) = @_; |
45
|
8
|
50
|
|
|
|
18
|
my $log_category = defined $ENV{LOG_CATEGORY} ? $ENV{LOG_CATEGORY} : __PACKAGE__; |
46
|
8
|
|
|
|
|
17
|
$logger = Log::Log4perl::get_logger($log_category); |
47
|
|
|
|
|
|
|
|
48
|
8
|
|
|
|
|
155
|
my $api_config; |
49
|
8
|
100
|
|
|
|
174
|
croak(q(Missing API configuration)) unless defined ($api_config = $Zabbix::ServerScript::Config->{api}); |
50
|
7
|
100
|
|
|
|
106
|
croak(q(API URL is not defined in config)) unless defined $api_config->{url}; |
51
|
6
|
100
|
100
|
|
|
320
|
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
|
|
|
|
|
8
|
$ua = LWP::UserAgent->new; |
54
|
3
|
|
|
|
|
27
|
$ua->timeout($api_config->{timeout}); |
55
|
3
|
|
|
|
|
156
|
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
|
|
|
|
|
18
|
}); |
61
|
1
|
|
|
|
|
5
|
return $self; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _request { |
65
|
3
|
|
|
3
|
|
3
|
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
|
|
|
|
|
13
|
id => 1, |
73
|
|
|
|
|
|
|
}; |
74
|
3
|
|
|
|
|
28
|
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
|
|
|
|
|
21
|
q(Content-Type) => q(application/json-rpc), |
79
|
|
|
|
|
|
|
q(Content) => $request_json, |
80
|
|
|
|
|
|
|
); |
81
|
3
|
100
|
|
|
|
120
|
croak(qq(Cannot make request "$method_name": ) . $res->status_line) if $res->is_error; |
82
|
2
|
|
|
|
|
75
|
my $response_json = $res->content; |
83
|
2
|
|
|
|
|
59
|
$logger->debug(qq(API response: $response_json)); |
84
|
2
|
|
|
|
|
18
|
my $response_hashref = decode_json($response_json); |
85
|
2
|
100
|
|
|
|
5
|
if (defined $response_hashref->{error}){ |
86
|
1
|
|
|
|
|
92
|
croak(qq(Zabbix API error: $response_hashref->{error}->{message}. $response_hashref->{error}->{data})); |
87
|
|
|
|
|
|
|
} |
88
|
1
|
|
|
|
|
5
|
return $response_hashref->{result}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |