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