line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Zabbix; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
70856
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
650
|
use Mojo::UserAgent; |
|
1
|
|
|
|
|
477047
|
|
|
1
|
|
|
|
|
13
|
|
7
|
1
|
|
|
1
|
|
58
|
use Scalar::Util qw(reftype refaddr); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
79
|
|
8
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
55
|
|
9
|
1
|
|
|
1
|
|
6
|
use Time::HiRes qw(gettimeofday tv_interval); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
10
|
1
|
|
|
1
|
|
159
|
use POSIX qw(strftime); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
17
|
0
|
|
|
|
|
|
my $required = 1; |
18
|
0
|
|
|
|
|
|
my $args = { |
19
|
|
|
|
|
|
|
url => \$required, |
20
|
|
|
|
|
|
|
trace => 0, |
21
|
|
|
|
|
|
|
debug => 0, |
22
|
|
|
|
|
|
|
username => \$required, |
23
|
|
|
|
|
|
|
password => \$required, |
24
|
|
|
|
|
|
|
@_, |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
for my $k ( keys %$args ) { |
28
|
0
|
0
|
0
|
|
|
|
if ( ref $args->{$k} |
29
|
|
|
|
|
|
|
&& refaddr( $args->{$k} ) == refaddr( \$required ) ) |
30
|
|
|
|
|
|
|
{ |
31
|
0
|
|
|
|
|
|
die "Missing value for $k"; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
$args->{url} =~ s,/+$,,g; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $self = bless { |
38
|
|
|
|
|
|
|
UserAgent => undef, |
39
|
|
|
|
|
|
|
Request => undef, |
40
|
|
|
|
|
|
|
Count => 1, |
41
|
|
|
|
|
|
|
Auth => undef, |
42
|
|
|
|
|
|
|
API_URL => $args->{url}, |
43
|
|
|
|
|
|
|
Output => "extend", |
44
|
|
|
|
|
|
|
Debug => $args->{debug} ? 1 : 0, |
45
|
|
|
|
|
|
|
Trace => $args->{trace} ? 1 : 0, |
46
|
|
|
|
|
|
|
User => $args->{username}, |
47
|
|
|
|
|
|
|
Password => $args->{password}, |
48
|
0
|
0
|
|
|
|
|
_call_start => 0, |
|
|
0
|
|
|
|
|
|
49
|
|
|
|
|
|
|
}, $class; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# init json object |
52
|
0
|
|
|
|
|
|
$self->_json; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# init useragent |
55
|
0
|
|
|
|
|
|
$self->ua; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# authenticate |
58
|
0
|
|
|
|
|
|
$self->auth; |
59
|
0
|
|
|
|
|
|
return $self; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub output { |
63
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
|
|
|
|
$self->{Output} = $_[0] |
66
|
|
|
|
|
|
|
if (@_); |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
return $self->{Output}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub ua { |
72
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
unless ( $self->{UserAgent} ) { |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
$self->{UserAgent} = Mojo::UserAgent->new; |
77
|
0
|
|
|
|
|
|
$self->{UserAgent}->transactor->name("Mojo::Zabbix"); |
78
|
0
|
|
|
|
|
|
$self->{UserAgent}->inactivity_timeout(10); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
return $self->{UserAgent}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _json { |
85
|
0
|
|
|
0
|
|
|
my $self = shift; |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
|
unless ( defined $self->{JSON} ) { |
88
|
0
|
|
|
|
|
|
$self->{JSON} = JSON::PP->new; |
89
|
|
|
|
|
|
|
$self->{JSON}->ascii->pretty->allow_nonref->allow_blessed |
90
|
0
|
|
|
|
|
|
->allow_bignum; |
91
|
|
|
|
|
|
|
} |
92
|
0
|
|
|
|
|
|
return $self->{JSON}; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub trace { |
96
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
97
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
$self->{Trace} = $_[0] |
99
|
|
|
|
|
|
|
if (@_); |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
return $self->{Trace}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub debug { |
105
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
$self->{Debug} = $_[0] |
108
|
|
|
|
|
|
|
if (@_); |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
return $self->{Debug}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub auth { |
114
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
115
|
0
|
0
|
|
|
|
|
if ( not defined $self->{Auth} ) { |
|
|
0
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
$self->{Auth} = ''; |
117
|
|
|
|
|
|
|
my $res = $self->http_request( |
118
|
|
|
|
|
|
|
'user', 'login', |
119
|
|
|
|
|
|
|
{ |
120
|
|
|
|
|
|
|
user => $self->{User}, |
121
|
|
|
|
|
|
|
password => $self->{Password}, |
122
|
|
|
|
|
|
|
} |
123
|
0
|
|
|
|
|
|
); |
124
|
|
|
|
|
|
|
#confess $res->{error}->{data} |
125
|
|
|
|
|
|
|
warn "$res->{error}->{data}\nError Code: $res->{error}->{code} |
126
|
|
|
|
|
|
|
\nResponse: $res->{error}->{message}" |
127
|
0
|
0
|
|
|
|
|
if defined $res->{error}; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
#print "$res->{error}"; |
130
|
0
|
|
|
|
|
|
$self->{Password} = '***'; |
131
|
0
|
|
|
|
|
|
$self->{Auth} = $res->{result}; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
elsif ( $self->{Auth} eq '' ) { |
134
|
0
|
|
|
|
|
|
return (); # empty for first auth call |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
0
|
0
|
|
|
|
|
return $self->{Auth} unless defined wantarray; |
138
|
0
|
|
|
|
|
|
return ( auth => $self->{Auth} ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub next_id { |
142
|
0
|
|
|
0
|
0
|
|
return ++shift->{'Count'}; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub int_debug { |
146
|
0
|
|
|
0
|
0
|
|
my ( $self, $data ) = @_; |
147
|
0
|
|
|
|
|
|
my $tempass; |
148
|
0
|
0
|
0
|
|
|
|
if ( defined $data->{'params'} |
|
|
|
0
|
|
|
|
|
149
|
|
|
|
|
|
|
and ref( $data->{'params'} ) eq 'HASH' |
150
|
|
|
|
|
|
|
and exists $data->{'params'}->{'password'} ) |
151
|
|
|
|
|
|
|
{ |
152
|
0
|
|
|
|
|
|
$tempass = $data->{'params'}->{'password'}; |
153
|
0
|
|
|
|
|
|
$data->{'params'}->{'password'} = '******'; |
154
|
|
|
|
|
|
|
} |
155
|
0
|
|
|
|
|
|
my $json = $self->{JSON}->encode($data); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
$self->_dbgmsg( "TX: " . $json ) |
158
|
0
|
0
|
|
|
|
|
if $self->{Debug}; |
159
|
|
|
|
|
|
|
$data->{'params'}->{'password'} = $tempass |
160
|
|
|
|
|
|
|
if ( ref( $data->{'params'} ) eq 'HASH' |
161
|
0
|
0
|
0
|
|
|
|
and exists $data->{'params'}->{'password'} ); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub out_debug { |
166
|
0
|
|
|
0
|
0
|
|
my ( $self, $data ) = @_; |
167
|
0
|
|
|
|
|
|
my $json = $self->{JSON}->encode($data); |
168
|
|
|
|
|
|
|
$self->_dbgmsg( "RX: " . $json ) |
169
|
0
|
0
|
|
|
|
|
if $self->{Debug}; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub get { |
174
|
0
|
|
|
0
|
0
|
|
my ( $self, $object, $params ) = @_; |
175
|
0
|
|
|
|
|
|
return $self->http_request( $object, "get", $params ); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub update { |
179
|
0
|
|
|
0
|
0
|
|
my ( $self, $object, $params ) = @_; |
180
|
0
|
|
|
|
|
|
return $self->http_request( $object, "update", $params ); |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub delete { |
184
|
0
|
|
|
0
|
0
|
|
my ( $self, $object, $params ) = @_; |
185
|
0
|
|
|
|
|
|
return $self->http_request( $object, "delete", $params ); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub create { |
189
|
0
|
|
|
0
|
0
|
|
my ( $self, $object, $params ) = @_; |
190
|
0
|
|
|
|
|
|
return $self->http_request( $object, "create", $params ); |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub exists { |
194
|
0
|
|
|
0
|
0
|
|
my ( $self, $object, $params ) = @_; |
195
|
0
|
|
|
|
|
|
return $self->http_request( $object, "exists", $params ); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub http_request { |
199
|
0
|
|
|
0
|
0
|
|
my ( $self, $object, $op, $params ) = @_; |
200
|
|
|
|
|
|
|
|
201
|
0
|
0
|
|
|
|
|
if ( $self->{Trace} ) { |
202
|
0
|
|
|
|
|
|
$self->{_call_start} = [gettimeofday]; |
203
|
0
|
|
|
|
|
|
$self->_dbgmsg("Starting method $object.$op"); |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
0
|
0
|
|
|
|
|
if ($params) { |
207
|
|
|
|
|
|
|
$params->{output} = $self->{Output} |
208
|
0
|
0
|
0
|
|
|
|
if ( reftype($params) eq 'HASH' and not defined $params->{output} and $object.$op ne "userlogin" ); |
|
|
|
0
|
|
|
|
|
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
else { |
211
|
0
|
|
|
|
|
|
$params = []; |
212
|
|
|
|
|
|
|
} |
213
|
0
|
|
|
|
|
|
my $zrurl = "$self->{API_URL}/api_jsonrpc.php"; |
214
|
0
|
|
|
|
|
|
my $myjson = { |
215
|
|
|
|
|
|
|
jsonrpc => "2.0", |
216
|
|
|
|
|
|
|
method => "$object.$op", |
217
|
|
|
|
|
|
|
params => $params, |
218
|
|
|
|
|
|
|
id => $self->next_id, |
219
|
|
|
|
|
|
|
( $self->auth ), |
220
|
|
|
|
|
|
|
}; |
221
|
0
|
0
|
|
|
|
|
$self->int_debug($myjson) if $self->{Debug}; |
222
|
|
|
|
|
|
|
|
223
|
0
|
|
|
|
|
|
my $res = $self->ua->post( $zrurl, json => $myjson ); |
224
|
|
|
|
|
|
|
|
225
|
0
|
0
|
|
|
|
|
unless ( $res->success ) { |
226
|
0
|
|
|
|
|
|
my $err = $res->error; |
227
|
0
|
0
|
|
|
|
|
warn "$err->{code} response: $err->{message}" if $err->{code}; |
228
|
0
|
|
|
|
|
|
warn "Connection error: $err->{message}"; |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
0
|
0
|
|
|
|
|
if ( $self->{Trace} ) { |
233
|
0
|
|
|
|
|
|
$self->_dbgmsg("Finished method $object.$op"); |
234
|
|
|
|
|
|
|
$self->_dbgmsg( "Spent " |
235
|
|
|
|
|
|
|
. tv_interval( $self->{_call_start} ) |
236
|
0
|
|
|
|
|
|
. "s on $object.$op" ); |
237
|
|
|
|
|
|
|
} |
238
|
0
|
0
|
|
|
|
|
$self->out_debug( $res->res->json ) if $self->{Debug}; |
239
|
0
|
|
|
|
|
|
return $res->res->json; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
sub _dbgmsg { |
243
|
0
|
|
|
0
|
|
|
my $self = shift; |
244
|
|
|
|
|
|
|
warn strftime( '[%F %T]', localtime ) . ' ' |
245
|
|
|
|
|
|
|
. __PACKAGE__ . ' @ #' |
246
|
0
|
|
|
|
|
|
. $self->{Count} . ' ' |
247
|
|
|
|
|
|
|
. join( ', ', @_ ) . "\n"; |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
1; |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
__END__ |