line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Zabbix::APP; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
14780
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
5
|
1
|
|
|
1
|
|
457
|
use Mojo::Zabbix; |
|
1
|
|
|
|
|
293921
|
|
|
1
|
|
|
|
|
41
|
|
6
|
1
|
|
|
1
|
|
11
|
use utf8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
25
|
use POSIX qw(strftime); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
3
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Exporter; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
13
|
|
|
|
|
|
|
our @EXPORT = qw(initZ pVersion getAllhost getname getItem getAlert getEvents pHitems pTriggers); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=encoding utf8 |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 NAME |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Mojo::Zabbix::APP - The application module of Mojo-Zabbix .Using to get |
20
|
|
|
|
|
|
|
data from zabbix data include host,items, Triggers and warns and so on. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 VERSION |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Version 0.05 |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 SYNOPSIS |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
use Mojo::Zabbix::APP; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my @myzinfo = ; ##(get zabbix info from __DATA__ ) |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Define for debug and traceing processe infomaition。(打开调试和跟踪) |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $DEBUG=0; |
40
|
|
|
|
|
|
|
my $TRACE=0; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
#my @myzinfo = ('test1 http://test1/zabbix testuser pass'); |
43
|
|
|
|
|
|
|
# @可以定义为多行数据,格式按照这种,一个zabbix 服务地址一个 |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
for (@myzinfo) { |
46
|
|
|
|
|
|
|
next if /^#/; |
47
|
|
|
|
|
|
|
next if /^\s*$/; |
48
|
|
|
|
|
|
|
my ( $name, $url,$user, $pass ) = split; |
49
|
|
|
|
|
|
|
print "\n$name\n"; |
50
|
|
|
|
|
|
|
my $z; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
eval { $z = initZ( $url,$user,$pss ); }; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
if ($@) { |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
print "Error $@!\n"; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
} else { |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
## Print the version of zabbix api. 打印zabbix 版本 |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
pVersion($z); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
## Print all host lists。 获取所有的主机列表 |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
print getAllhost($z); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
## Print warning info of Triggers。打印取得的所有触发器告警信息 |
69
|
|
|
|
|
|
|
pTriggers($z); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
## Print the history data of given items, default for past 24 hours. |
72
|
|
|
|
|
|
|
## 打印给定时间段的item历史数据,如果默认不给时间默认为过去24小时内的 |
73
|
|
|
|
|
|
|
pHitems($z); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
#### 初始化的Mojo::zabbix ,必须安装Mojo::zabbix模块,可用cpanm Mojo::zabbix 安装 |
81
|
|
|
|
|
|
|
my %EVcache; |
82
|
|
|
|
|
|
|
my %HTcache; |
83
|
|
|
|
|
|
|
my %Vcache; |
84
|
|
|
|
|
|
|
## 缓存hash 对事件id进行缓存,防止重复调用 |
85
|
|
|
|
|
|
|
my $DEBUG = 0; |
86
|
|
|
|
|
|
|
my $TRACE = 0; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub initZ { |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
0
|
0
|
|
my ( $url, $user, $pass ) = @_; |
91
|
0
|
0
|
|
|
|
|
print "debug-initZ { parameter } : $url, $user, $pass \n" if $DEBUG; |
92
|
0
|
|
|
|
|
|
my $zbbix = Mojo::Zabbix->new( |
93
|
|
|
|
|
|
|
url => $url, |
94
|
|
|
|
|
|
|
username => $user, |
95
|
|
|
|
|
|
|
password => $pass, |
96
|
|
|
|
|
|
|
debug => $DEBUG, |
97
|
|
|
|
|
|
|
trace => $TRACE, |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
### 打印zabbix 版本 |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub pVersion { |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
0
|
0
|
|
my $z = shift; |
106
|
0
|
|
|
|
|
|
my $ckey=$z->{'API_URL'}; |
107
|
0
|
0
|
|
|
|
|
unless ( exists $Vcache{$ckey} ) { |
108
|
0
|
|
|
|
|
|
my $result; |
109
|
0
|
|
|
|
|
|
my $auth=$z->{'Auth'}; |
110
|
0
|
|
|
|
|
|
$z->{'Auth'}=""; |
111
|
0
|
|
|
|
|
|
my $r = $z->get( "apiinfo.version", ); |
112
|
0
|
|
|
|
|
|
$z->{'Auth'}=$auth; |
113
|
0
|
0
|
|
|
|
|
$result = $r->{'result'} if $r->{'result'}; |
114
|
0
|
0
|
|
|
|
|
$result = pVersion2($z) unless $r->{'result'}; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
$Vcache{$ckey} = $result; |
117
|
|
|
|
|
|
|
} |
118
|
0
|
|
|
|
|
|
return $Vcache{$ckey}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub pVersion2 { |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
0
|
0
|
|
my $z = shift; |
124
|
0
|
|
|
|
|
|
my $r = $z->get( "apiinfo.version", ); |
125
|
0
|
|
|
|
|
|
my $result = $r->{'result'}; |
126
|
0
|
|
|
|
|
|
return $result; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
### 打印给定时间段的item历史数据,如果默认不给时间默认为过去24小时内的 |
130
|
|
|
|
|
|
|
sub pHitems { |
131
|
0
|
|
|
0
|
0
|
|
my ( $z, $host, $key, $btime, $ltime ) = @_; |
132
|
0
|
|
0
|
|
|
|
$host //= '192.168.1.1'; |
133
|
0
|
|
0
|
|
|
|
$key //= 'net.if.in[bond0]'; |
134
|
0
|
|
0
|
|
|
|
$btime //= time() - 1 * 3600; |
135
|
0
|
|
0
|
|
|
|
$ltime //= time(); |
136
|
0
|
|
|
|
|
|
my $info; |
137
|
0
|
|
|
|
|
|
my $hostid = gethostID( $z, $host ); |
138
|
0
|
|
|
|
|
|
my ( $name, $itemid ) = getItemID( $z, $hostid, $key ); |
139
|
0
|
|
|
|
|
|
$info = "The Item of $name-$key \n\n"; |
140
|
0
|
0
|
|
|
|
|
print "debug-PHitems { parameter } : $host $key $btime $ltime \n" if $DEBUG; |
141
|
0
|
|
|
|
|
|
my $v = getHisv( $z, $hostid, $itemid, $btime, $ltime ); |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
for ( sort { $b <=> $a } @{$v} ) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
#print Dumper($_); |
146
|
0
|
|
|
|
|
|
my $stime = strftime( "%Y-%m-%d %H:%M:%S", localtime( $_->[0] ) ); |
147
|
0
|
|
|
|
|
|
$info .= "$stime $_->[1] \n"; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
} |
150
|
0
|
|
|
|
|
|
return $info; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
### 打印取得的所有触发器告警信息 |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub pTriggers { |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
0
|
0
|
|
my $z = shift; |
158
|
0
|
|
|
|
|
|
my $info; |
159
|
0
|
|
|
|
|
|
my $reslut = getTriggers($z); |
160
|
0
|
|
|
|
|
|
$info = "\n\nWarning info of Triggers \n\n"; |
161
|
|
|
|
|
|
|
|
162
|
0
|
|
|
|
|
|
for ( sort { $b <=> $a } keys %{$reslut} ) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
$info .= "$reslut->{$_}"; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
} |
167
|
0
|
|
|
|
|
|
return $info; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
##### 获取给定主机和key值的所有监控项以及当前值 |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub getItem { |
172
|
|
|
|
|
|
|
|
173
|
0
|
|
|
0
|
0
|
|
my ( $z, $host, $key ) = @_; |
174
|
|
|
|
|
|
|
|
175
|
0
|
0
|
|
|
|
|
return unless gethostID( $z, $host ); |
176
|
0
|
|
|
|
|
|
my $hostid = gethostID( $z, $host ); |
177
|
0
|
|
|
|
|
|
my $r = $z->get( |
178
|
|
|
|
|
|
|
"item", |
179
|
|
|
|
|
|
|
{ |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
output => [ "name", "key_", "prevvalue" ], |
182
|
|
|
|
|
|
|
search => { "key_" => $key }, |
183
|
|
|
|
|
|
|
hostids => $hostid, |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# limit => 10, |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
}, |
188
|
|
|
|
|
|
|
); |
189
|
|
|
|
|
|
|
|
190
|
0
|
|
|
|
|
|
my $result = $r->{'result'}; |
191
|
0
|
|
|
|
|
|
my $sresult; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
$sresult .= "$_->{'name'} - { $_->{'key_'} } : $_->{'prevvalue'} \n" |
194
|
0
|
|
|
|
|
|
for ( @{$result} ); |
|
0
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
return $sresult; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub getItemID { |
200
|
|
|
|
|
|
|
|
201
|
0
|
|
|
0
|
0
|
|
my ( $z, $hostid, $key ) = @_; |
202
|
0
|
0
|
|
|
|
|
print "DEBUG-function(getItemID): $z, $key \n" if $DEBUG; |
203
|
0
|
|
|
|
|
|
my $r = $z->get( |
204
|
|
|
|
|
|
|
"item", |
205
|
|
|
|
|
|
|
{ |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
output => [ "itemids", "name" ], |
208
|
|
|
|
|
|
|
search => { "key_" => $key }, |
209
|
|
|
|
|
|
|
hostids => $hostid, |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
}, |
212
|
|
|
|
|
|
|
); |
213
|
|
|
|
|
|
|
|
214
|
0
|
|
|
|
|
|
my $result = $r->{'result'}; |
215
|
0
|
|
|
|
|
|
my $sresult; |
216
|
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
$sresult = $result->[0]->{'itemid'}; |
218
|
|
|
|
|
|
|
|
219
|
0
|
|
|
|
|
|
return ( $result->[0]->{'name'}, $result->[0]->{'itemid'} ); |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub getHost { |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
0
|
0
|
|
my ( $z, $hostid) = @_; |
225
|
0
|
|
|
|
|
|
my $r = $z->get( |
226
|
|
|
|
|
|
|
"host", |
227
|
|
|
|
|
|
|
{ |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
hostids => $hostid, |
230
|
|
|
|
|
|
|
output=> ["host"], |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
}, |
233
|
|
|
|
|
|
|
); |
234
|
|
|
|
|
|
|
|
235
|
0
|
0
|
|
|
|
|
return $r->{'result'}->[0]->{host} if $r->{'result'}; |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub getHisv { |
241
|
|
|
|
|
|
|
|
242
|
0
|
|
|
0
|
0
|
|
my ( $z, $hostid, $itemid, $btime, $ltime ) = @_; |
243
|
0
|
0
|
|
|
|
|
print "DEBUG-function(-getHist): $z, $hostid, $itemid, $btime, $ltime \n" |
244
|
|
|
|
|
|
|
if $DEBUG; |
245
|
0
|
|
|
|
|
|
my $r = $z->get( |
246
|
|
|
|
|
|
|
"history", |
247
|
|
|
|
|
|
|
{ |
248
|
|
|
|
|
|
|
# history => 0, |
249
|
|
|
|
|
|
|
itemids => $itemid, |
250
|
|
|
|
|
|
|
time_from => $btime, |
251
|
|
|
|
|
|
|
time_till => $ltime, |
252
|
|
|
|
|
|
|
output => "extend", |
253
|
|
|
|
|
|
|
hostids => $hostid, |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
}, |
256
|
|
|
|
|
|
|
); |
257
|
|
|
|
|
|
|
|
258
|
0
|
|
|
|
|
|
my $result = $r->{'result'}; |
259
|
0
|
|
|
|
|
|
my $sresult; |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
#$sresult.="$_->{'name'} - $_->{'_key'} : $_->{'prevvalue'} \n" for(@{$result}); |
262
|
|
|
|
|
|
|
#print Dumper($result); |
263
|
0
|
|
|
|
|
|
for ( @{$result} ) { |
|
0
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
|
265
|
0
|
|
|
|
|
|
push @{$sresult}, [ $_->{'clock'}, $_->{'value'} ]; |
|
0
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
} |
268
|
0
|
|
|
|
|
|
return $sresult; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
#### 获取给定主机(ip)的主机号 |
272
|
|
|
|
|
|
|
sub gethostID { |
273
|
0
|
|
|
0
|
0
|
|
my ( $z, $host ) = @_; |
274
|
0
|
|
|
|
|
|
my $ckey = $host; |
275
|
0
|
0
|
|
|
|
|
unless ( exists $HTcache{$ckey} ) { |
276
|
0
|
0
|
|
|
|
|
print "DEBUG-function(gethostID): $z, $host \n" if $DEBUG; |
277
|
0
|
|
|
|
|
|
my $r = $z->get( |
278
|
|
|
|
|
|
|
"host", |
279
|
|
|
|
|
|
|
{ |
280
|
|
|
|
|
|
|
filter => { |
281
|
|
|
|
|
|
|
host => $host, |
282
|
|
|
|
|
|
|
}, |
283
|
|
|
|
|
|
|
output => ["hostid"], |
284
|
|
|
|
|
|
|
}, |
285
|
|
|
|
|
|
|
); |
286
|
0
|
0
|
|
|
|
|
$HTcache{$ckey} = $r->{'result'}->[0]->{'hostid'} if $r->{'result'}; |
287
|
|
|
|
|
|
|
} |
288
|
0
|
|
|
|
|
|
return $HTcache{$ckey}; |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
sub getname { |
292
|
|
|
|
|
|
|
|
293
|
0
|
|
|
0
|
0
|
|
my ( $z, $host ) = @_; |
294
|
0
|
0
|
|
|
|
|
print "DEBUG-function(gethostname): $z, $host \n" if $DEBUG; |
295
|
0
|
|
|
|
|
|
my $r = $z->get( |
296
|
|
|
|
|
|
|
"host", |
297
|
|
|
|
|
|
|
{ |
298
|
|
|
|
|
|
|
filter => { |
299
|
|
|
|
|
|
|
host => $host, |
300
|
|
|
|
|
|
|
}, |
301
|
|
|
|
|
|
|
output => ["name"], |
302
|
|
|
|
|
|
|
}, |
303
|
|
|
|
|
|
|
); |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
#use Data::Dumper; |
306
|
0
|
0
|
|
|
|
|
return $r->{'result'}->[0]->{'name'} if $r->{'result'}; |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
#### 获取所有的主机列表 |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
sub getAllhost { |
311
|
|
|
|
|
|
|
|
312
|
0
|
|
|
0
|
0
|
|
my $z = shift; |
313
|
0
|
|
|
|
|
|
my $r = $z->get( |
314
|
|
|
|
|
|
|
"host", |
315
|
|
|
|
|
|
|
{ |
316
|
|
|
|
|
|
|
filter => undef, |
317
|
|
|
|
|
|
|
search => undef, |
318
|
|
|
|
|
|
|
output => [ "host", "name" ], |
319
|
|
|
|
|
|
|
}, |
320
|
|
|
|
|
|
|
); |
321
|
|
|
|
|
|
|
|
322
|
0
|
|
|
|
|
|
my $hresult; |
323
|
0
|
|
|
|
|
|
my $host = $r->{'result'}; |
324
|
0
|
|
|
|
|
|
for (@$host) { |
325
|
0
|
|
|
|
|
|
$hresult .= $_->{'host'} . ": $_->{'name'}" . "\n"; |
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
|
328
|
0
|
|
|
|
|
|
return $hresult; |
329
|
|
|
|
|
|
|
} |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
sub getAllhostid { |
332
|
|
|
|
|
|
|
|
333
|
0
|
|
|
0
|
0
|
|
my $z = shift; |
334
|
0
|
|
|
|
|
|
my $hostsids; |
335
|
0
|
|
|
|
|
|
my $r = $z->get( |
336
|
|
|
|
|
|
|
"host", |
337
|
|
|
|
|
|
|
{ |
338
|
|
|
|
|
|
|
filter => undef, |
339
|
|
|
|
|
|
|
search => undef, |
340
|
|
|
|
|
|
|
output => [ "hostid" ], |
341
|
|
|
|
|
|
|
}, |
342
|
|
|
|
|
|
|
); |
343
|
|
|
|
|
|
|
|
344
|
0
|
|
|
|
|
|
my $hresult; |
345
|
0
|
|
|
|
|
|
my $host = $r->{'result'}; |
346
|
0
|
|
|
|
|
|
for (@$host) { |
347
|
0
|
|
|
|
|
|
push(@{$hostsids},$_->{'hostid'}); |
|
0
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
|
350
|
0
|
|
|
|
|
|
return $hostsids;; |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
####获取所有的有问题触发警告信息,返回一个包含时间、主机ip和描述的哈希引用 |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
sub getTriggers { |
356
|
0
|
|
|
0
|
0
|
|
my $z = shift; |
357
|
0
|
|
|
|
|
|
my $V=pVersion($z); |
358
|
0
|
|
|
|
|
|
$V=~s/(\d).*/$1/; |
359
|
0
|
|
|
|
|
|
my $getv3={ |
360
|
|
|
|
|
|
|
filter => { |
361
|
|
|
|
|
|
|
value => 1, |
362
|
|
|
|
|
|
|
only_true => 1, |
363
|
|
|
|
|
|
|
withUnacknowledgedEvents => 1, |
364
|
|
|
|
|
|
|
}, |
365
|
|
|
|
|
|
|
output => ["hostid","triggerid", "description", "priority" ], |
366
|
|
|
|
|
|
|
sortfield => "triggerid", |
367
|
|
|
|
|
|
|
sortorder => "DESC", |
368
|
|
|
|
|
|
|
selectHosts => "host", |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
}; |
371
|
|
|
|
|
|
|
|
372
|
0
|
|
|
|
|
|
my $getv2={ |
373
|
|
|
|
|
|
|
filter => { |
374
|
|
|
|
|
|
|
value => 1, |
375
|
|
|
|
|
|
|
only_true => 1, |
376
|
|
|
|
|
|
|
withUnacknowledgedEvents => 1, |
377
|
|
|
|
|
|
|
}, |
378
|
|
|
|
|
|
|
output => ["hostid","triggerid", "description", "priority" ], |
379
|
|
|
|
|
|
|
sortfield => "triggerid", |
380
|
|
|
|
|
|
|
sortorder => "DESC", |
381
|
|
|
|
|
|
|
expandData => "host", |
382
|
|
|
|
|
|
|
}; |
383
|
|
|
|
|
|
|
|
384
|
0
|
|
|
|
|
|
my $hresult; |
385
|
|
|
|
|
|
|
|
386
|
0
|
0
|
|
|
|
|
if ($V eq "2") { |
387
|
0
|
|
|
|
|
|
my $r = $z->get("trigger",$getv2); |
388
|
0
|
|
|
|
|
|
my $host=$r->{'result'}; |
389
|
0
|
|
|
|
|
|
for (@$host) { |
390
|
0
|
|
|
|
|
|
my $hostid = gethostID( $z, $_->{'host'}); |
391
|
0
|
|
|
|
|
|
my $etime = getTgtime( $z, $_->{'triggerid'}, $hostid ); |
392
|
0
|
0
|
|
|
|
|
next unless $etime; |
393
|
0
|
|
|
|
|
|
my $time = strftime( "%Y-%m-%d %H:%M:%S", localtime($etime) ); |
394
|
|
|
|
|
|
|
$hresult->{$etime} = |
395
|
0
|
|
|
|
|
|
"$time : $_->{'host'}: " . $_->{'description'} . "\n"; |
396
|
|
|
|
|
|
|
} |
397
|
|
|
|
|
|
|
} |
398
|
|
|
|
|
|
|
else { |
399
|
|
|
|
|
|
|
|
400
|
0
|
|
|
|
|
|
my $r= $z->get("trigger",$getv3); |
401
|
0
|
|
|
|
|
|
my $host=$r->{'result'}; |
402
|
0
|
|
|
|
|
|
for (@$host) { |
403
|
0
|
|
|
|
|
|
my $hostid = $_->{'hosts'}->[0]->{'hostid'}; |
404
|
0
|
|
|
|
|
|
my $host=getHost($z,$hostid); |
405
|
0
|
|
|
|
|
|
my $etime = getTgtime( $z, $_->{'triggerid'}, $_->{'hosts'}->[0]->{'hostid'} ); |
406
|
0
|
0
|
|
|
|
|
next unless $etime; |
407
|
0
|
|
|
|
|
|
my $time = strftime( "%Y-%m-%d %H:%M:%S", localtime($etime) ); |
408
|
|
|
|
|
|
|
$hresult->{$etime} = |
409
|
0
|
|
|
|
|
|
"$time : $host " . $_->{'description'} . "\n"; |
410
|
|
|
|
|
|
|
} |
411
|
|
|
|
|
|
|
} |
412
|
0
|
|
|
|
|
|
return $hresult; |
413
|
|
|
|
|
|
|
} |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
### 给定触发器,触发器处罚时间(限制24小时候内的). |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
sub getTgtime { |
418
|
|
|
|
|
|
|
|
419
|
0
|
|
|
0
|
0
|
|
my ( $z, $tgid, $host ) = @_; |
420
|
0
|
|
|
|
|
|
my $hostid=$host; |
421
|
|
|
|
|
|
|
#my $hostid = gethostID( $z, $host ); |
422
|
0
|
|
|
|
|
|
my $ysterday = time() - 20 * 3600; |
423
|
0
|
|
|
|
|
|
my $vkey = $tgid . $hostid; |
424
|
0
|
0
|
|
|
|
|
unless ( exists $EVcache{$vkey} ) { |
425
|
0
|
|
|
|
|
|
my $r = $z->get( |
426
|
|
|
|
|
|
|
"event", |
427
|
|
|
|
|
|
|
{ |
428
|
|
|
|
|
|
|
filter => { |
429
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
# value => 1, |
431
|
|
|
|
|
|
|
# objectids => '19011' , |
432
|
|
|
|
|
|
|
# triggerids => '19011' , |
433
|
|
|
|
|
|
|
#source => 0, |
434
|
|
|
|
|
|
|
}, |
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
objectids => $tgid, |
437
|
|
|
|
|
|
|
triggerids => $tgid, |
438
|
|
|
|
|
|
|
time_from => $ysterday, |
439
|
|
|
|
|
|
|
hostids => $hostid, |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
#select_acknowledges => "extend", |
442
|
|
|
|
|
|
|
output => "extend", |
443
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
sortfield => "eventid", |
445
|
|
|
|
|
|
|
sortorder => "DESC", |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
# expandData=>"host", |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
}, |
450
|
|
|
|
|
|
|
); |
451
|
0
|
0
|
|
|
|
|
$EVcache{$vkey} = $r->{'result'}->[0]->{'clock'} if $r->{'result'}; |
452
|
|
|
|
|
|
|
} |
453
|
0
|
|
|
|
|
|
return $EVcache{$vkey}; |
454
|
|
|
|
|
|
|
} |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
sub getEvent { |
458
|
0
|
|
|
0
|
0
|
|
my $z = shift; |
459
|
0
|
|
|
|
|
|
my $ysterday = time() - 1 * 3600; |
460
|
0
|
|
|
|
|
|
my $r = $z->get( |
461
|
|
|
|
|
|
|
"event", |
462
|
|
|
|
|
|
|
{ |
463
|
|
|
|
|
|
|
filter => { |
464
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
# value => 1, |
466
|
|
|
|
|
|
|
acknowledged => 0, |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
#time_from=> "$ysterday", |
469
|
|
|
|
|
|
|
}, |
470
|
|
|
|
|
|
|
time_from => "$ysterday", |
471
|
|
|
|
|
|
|
output => "extend", |
472
|
|
|
|
|
|
|
source => 0, |
473
|
|
|
|
|
|
|
select_acknowledges => "extend", |
474
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
#sortfield =>["clock", "eventid"], |
476
|
|
|
|
|
|
|
#sortorder => "DESC", |
477
|
|
|
|
|
|
|
# expandData=>"host", |
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
}, |
480
|
|
|
|
|
|
|
); |
481
|
0
|
|
|
|
|
|
my $host = $r; |
482
|
0
|
|
|
|
|
|
my $hresult = Dumper($r); |
483
|
0
|
|
|
|
|
|
return $hresult; |
484
|
|
|
|
|
|
|
} |
485
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
sub getEvents { |
488
|
0
|
|
|
0
|
0
|
|
my $z = shift; |
489
|
0
|
|
|
|
|
|
my $ysterday = time() - 15 * 3600; |
490
|
0
|
|
|
|
|
|
my $r = $z->get( |
491
|
|
|
|
|
|
|
"event",{ |
492
|
|
|
|
|
|
|
#filter => { |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
# value => 1, |
495
|
|
|
|
|
|
|
# acknowledged => 0, |
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
#time_from=> "$ysterday", |
498
|
|
|
|
|
|
|
# }, |
499
|
|
|
|
|
|
|
acknowledged =>0, |
500
|
|
|
|
|
|
|
value => 1, |
501
|
|
|
|
|
|
|
time_from => "$ysterday", |
502
|
|
|
|
|
|
|
output => "extend", |
503
|
|
|
|
|
|
|
# select_acknowledges=> "extend", |
504
|
|
|
|
|
|
|
source => 0, |
505
|
|
|
|
|
|
|
sortfield =>["clock", "eventid"], |
506
|
|
|
|
|
|
|
sortorder => "DESC", |
507
|
|
|
|
|
|
|
expandData=>"host", |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
}, |
510
|
|
|
|
|
|
|
); |
511
|
0
|
|
|
|
|
|
my $hresult = Dumper($r); |
512
|
0
|
0
|
|
|
|
|
my $result=$r->{'result'} if $r->{'result'}; |
513
|
|
|
|
|
|
|
# print getTrigger($z,$_->{'objectid'}) for(@$result); |
514
|
|
|
|
|
|
|
#return $hresult; |
515
|
|
|
|
|
|
|
} |
516
|
|
|
|
|
|
|
|
517
|
|
|
|
|
|
|
sub getAlert { |
518
|
0
|
|
|
0
|
0
|
|
my $z = shift; |
519
|
0
|
|
|
|
|
|
my $ysterday = time() - 24 * 3600; |
520
|
0
|
|
|
|
|
|
my $r = $z->get( |
521
|
|
|
|
|
|
|
"alert", |
522
|
|
|
|
|
|
|
{ |
523
|
|
|
|
|
|
|
# time_from => "$ysterday", |
524
|
|
|
|
|
|
|
output => "extend", |
525
|
|
|
|
|
|
|
sortfield => ["clock"], |
526
|
|
|
|
|
|
|
sortorder => "DESC", |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
}, |
529
|
|
|
|
|
|
|
); |
530
|
0
|
|
|
|
|
|
my $hresult = Dumper($r); |
531
|
0
|
|
|
|
|
|
return $hresult; |
532
|
|
|
|
|
|
|
} |
533
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
=head1 AUTHOR |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
ORANGE, C<< >> |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
=head1 BUGS |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
542
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
543
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
=head1 SUPPORT |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
551
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
perldoc Mojo::Zabbix::APP |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
You can also look for information at: |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
=over 4 |
558
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
560
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
L |
562
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
L |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
=item * CPAN Ratings |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
L |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
=item * Search CPAN |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
L |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
=back |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
579
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
Copyright 2016 ORANGE. |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify |
586
|
|
|
|
|
|
|
it under the same terms as the Perl 5 programming language system itself. |
587
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
=cut |
589
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
1; |
591
|
|
|
|
|
|
|
|