lib/Zabbix/Reporter.pm

filter: Statementstmt 15.0% 27 / 179 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 23.8% 5 / 21 Podpod 100% 9 / 9 totaltotal 12.9% 41 / 317 CC 43 SCAR 59.2
CC
43
Cov
10%
triggers
812.0
history
72.0
fetch
20.0
CRAP 373.3
↑ Summary lib/Zabbix/Reporter/Web.pm »
1 package Zabbix::Reporter;
2 {
3 2 $Zabbix::Reporter::VERSION = '0.07';
4 }
5 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 $Zabbix::Reporter::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: Zabbix dashboard
9  
10 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Cache::MemoryCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 has 'dbh' => (
17 'is' => 'rw',
18 'isa' => 'DBI::db',
19 'lazy' => 1,
20 'builder' => '_init_dbh',
21 );
22  
23 2 has 'cache' => (
24 'is' => 'rw',
25 'isa' => 'Cache::Cache',
26 'lazy' => 1,
27 'builder' => '_init_cache',
28 );
29  
30 has 'priorities' => (
31 'is' => 'rw',
32 'isa' => 'ArrayRef',
33 0 ▶ 'default' => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 2 );
35  
36 2 has 'min_age' => (
37 'is' => 'rw',
38 'isa' => 'Int',
39 'default' => 0,
40 );
41  
42 2 has 'warn_unsupported' => (
43 'is' => 'rw',
44 'isa' => 'Bool',
45 'default' => 0,
46 );
47  
48 2 has 'warn_unattended' => (
49 'is' => 'rw',
50 'isa' => 'Bool',
51 'default' => 0,
52 );
53  
54 has 'groups' => (
55 'is' => 'rw',
56 'isa' => 'ArrayRef',
57 0 ▶ 'default' => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 2 );
59  
60 has '_severity_map' => (
61 'is' => 'ro',
62 'isa' => 'ArrayRef',
63 0 ▶ 'default' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 [qw(nc information warning average high disaster)],
65 },
66 2 );
67  
68 has '_event_value_map' => (
69 'is' => 'ro',
70 'isa' => 'ArrayRef',
71 0 ▶ 'default' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 [qw(OK PROBLEM UNKNOWN)],
73 },
74 2 );
75  
76  
77 2 with qw(Config::Yak::RequiredConfig Log::Tree::RequiredLogger);
78  
79 0 ▶sub _dsn {
Subroutine _dsn: not called CC=1 SCAR=6.9
80 0 my $self = shift;
81  
82 0 my $hostname = $self->config()->get( 'Zabbix::Reporter::DB::Hostname', { Default => 'localhost', } );
83 0 my $port = $self->config()->get( 'Zabbix::Reporter::DB::Port', { Default => 3306, } );
84 0 my $database = $self->config()->get( 'Zabbix::Reporter::DB::Database', { Default => 'zabbix', } );
85 0 my $username = $self->config()->get( 'Zabbix::Reporter::DB::Username', { Default => 'zabbix', } );
86 0 my $password = $self->config()->get( 'Zabbix::Reporter::DB::Password', { Default => 'zabbix', } );
87  
88 0 my $dsn = 'DBI:mysql:user=' . $username;
89 0 $dsn .= ';password=' . $password;
90 0 $dsn .= ';host=' . $hostname . ';port=' . $port;
91 0 $dsn .= ';database=' . $database;
92  
93 0 return $dsn;
94 }
95  
96 0 ▶sub _init_dbh {
Subroutine _init_dbh: not called CC=1 SCAR=6.9
97 0 my $self = shift;
98  
99 0 my $dbh = DBI->connect($self->_dsn());
100 0 $dbh->{'mysql_auto_reconnect'} = 1;
101  
102 0 return $dbh;
103 }
104  
105 0 ▶sub _init_cache {
Subroutine _init_cache: not called CC=1 SCAR=6.9
106 0 my $self = shift;
107  
108 0 my $Cache = Cache::MemoryCache::->new({
109 'namespace' => 'ZabbixReporter',
110 'default_expires_in' => 600,
111 });
112  
113 0 return $Cache;
114 }
115  
116 0 ▶sub fetch_n_store {
Subroutine fetch_n_store: not called CC=2 SCAR=17.9
Pod documented
117 0 my $self = shift;
118 0 my $query = shift;
119 0 my $timeout = shift;
120 0 my @args = @_;
121  
122 0 my $key = $query.join(',',@args);
123  
124 0 my $result = $self->cache()->get($key);
125  
126 0 ▶ if( ! defined($result) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 0 0
127 0 $result = $self->fetch($query,@args);
128 0 $self->cache()->set($key,$result,$timeout);
129 }
130  
131 0 return $result;
132 }
133  
134 0 ▶sub fetch {
Subroutine fetch: not called CC=4 SCAR=30.0
Pod documented
135 0 my $self = shift;
136 0 my $query = shift;
137 0 my @args = @_;
138  
139 0 ▶ my $sth = $self->dbh()->prepare($query)
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->dbh->prepare($query) or die "Could not prepare query ${query}: " . $self->dbh->errstr 0 0 0
140 or die("Could not prepare query $query: ".$self->dbh()->errstr);
141  
142 0 ▶ $sth->execute(@args)
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@args) or die "Could not execute query ${query}: " . $self->dbh->errstr 0 0 0
143 or die("Could not execute query $query: ".$self->dbh()->errstr);
144  
145 0 my @result = ();
146  
147 0 ▶ while(my $ref = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $sth->fetchrow_hashref) 0 0 0
148 0 push(@result,$ref);
149 }
150 0 $sth->finish();
151  
152 0 return \@result;
153 }
154  
155 0 ▶sub triggers {
Subroutine triggers: not called CC=28 SCAR=67.0
Pod documented
156 0 my $self = shift;
157  
158 0 my $sql = <<'EOS';
159 SELECT
160 t.priority,
161 h.host,
162 t.description,
163 h.hostid,
164 t.triggerid,
165 i.itemid,
166 i.lastvalue,
167 i.lastclock,
168 t.lastchange,
169 t.value,
170 t.comments,
171 i.units,
172 i.valuemapid,
173 d.triggerdepid
174 FROM
175 hosts AS h
176 JOIN items AS i ON (h.hostid = i.hostid)
177 JOIN functions AS f ON (i.itemid = f.itemid)
178 JOIN triggers AS t ON (f.triggerid = t.triggerid)
179 LEFT JOIN trigger_depends AS d ON (d.triggerid_down = t.triggerid)
180 WHERE
181 h.status = 0 AND
182 t.value = 1 AND
183 t.status = 0 AND
184 i.status = 0
185 EOS
186 0 ▶ if($self->priorities() && @{$self->priorities()} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->priorities and @{$self->priorities;} > 0) 0 0 0
Condition0%0/3
$self->priorities and @{$self->priorities;} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->priorities and @{$self->priorities;} > 0
$self->priorities @{$self->priorities;} > 0
Truth table0%0/3
A and B
A= $self->prioritiesB= @{$self->priorities;} > 0
ABresult
0X0
100
111
187 0 $sql .= ' AND t.priority IN ('.join(',',@{$self->priorities()}).')';
188 }
189 0 ▶ if($self->min_age()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->min_age) 0 0 0
190 0 $sql .= ' AND t.lastchange < UNIX_TIMESTAMP(NOW() - INTERVAL '.$self->min_age().' MINUTE)';
191 }
192 0 ▶ if($self->groups() && @{$self->groups()} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->groups and @{$self->groups;} > 0) 0 0 0
Condition0%0/3
$self->groups and @{$self->groups;} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->groups and @{$self->groups;} > 0
$self->groups @{$self->groups;} > 0
Truth table0%0/3
A and B
A= $self->groupsB= @{$self->groups;} > 0
ABresult
0X0
100
111
193 0 my $sub_sql = "SELECT hostid FROM host_groups WHERE groupid IN (".join(',',@{$self->groups()}).")";
194 0 my $hostids = $self->fetch_n_store($sub_sql,60);
195 0 $sql .= ' AND h.hostid IN ('.join(',',@{$hostids}).')';
196 }
197 0 $sql .= ' GROUP BY t.triggerid';
198 0 $sql .= ' ORDER BY t.priority DESC, h.host';
199 0 my $rows = $self->fetch_n_store($sql,60);
200  
201 # Post processing
202 0 my @unacked = ();
203 0 my @acked = ();
204 0 ▶ if($rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows) 0 0 0
205 0 foreach my $row (@{$rows}) {
206 0 ▶ if (defined($row->{'priority'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"priority"}) 0 0 0
207 0 $row->{'severity'} = $self->_severity_map()->[$row->{'priority'}];
208 }
209 0 ▶ if (defined($row->{'description'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"description"}) 0 0 0
210 0 $row->{'description'} =~ s/\{HOSTNAME\}/$row->{'host'}/g;
211 0 $row->{'description'} =~ s/\{HOST\.DNS\}/$row->{'host'}/g;
212 }
213 0 ▶ if (defined($row->{'triggerid'}) && defined($row->{'lastclock'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"triggerid"} and defined $row->{"lastclock"}) 0 0 0
Condition0%0/3
defined $row->{"triggerid"} and defined $row->{"lastclock"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $row->{"triggerid"} and defined $row->{"lastclock"}
defined $row->{"triggerid"} defined $row->{"lastclock"}
Truth table0%0/3
A and B
A= defined $row->{"triggerid"}B= defined $row->{"lastclock"}
ABresult
0X0
100
111
214 0 my $ack = $self->acks($row->{'triggerid'},$row->{'lastclock'});
215 0 ▶ if($ack && ref($ack) eq 'ARRAY' && scalar @{$ack} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ack and ref $ack eq "ARRAY" and scalar @{$ack;} > 0) 0 0 0
Condition0%0/6
$ack and ref $ack eq "ARRAY" and scalar @{$ack;} > 0
!ll&&!rl&&r
000
$ack and ref $ack eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
$ack and ref $ack eq "ARRAY" and scalar @{$ack;} > 0
$ack ref $ack eq "ARRAY" scalar @{$ack;} > 0
Truth table0%0/4
A and B and C
A= $ackB= ref $ack eq "ARRAY"C= scalar @{$ack;} > 0
ABCresult
0XX0
10X0
1100
1111
216 0 foreach my $field (keys %{$ack->[0]}) {
217 0 $row->{$field} = $ack->[0]->{$field};
218 }
219 }
220 }
221 # this should be the last post-processing action
222 0 ▶ if($row->{'acknowledged'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{'acknowledged'}) { } 0 0 0
223 0 push(@acked,$row);
224 } else {
225 0 push(@unacked,$row);
226 }
227 }
228 }
229 # sort acked triggers to the end
230 0 @{$rows} = (@unacked,@acked);
231  
232 # Check for any unsupported items and prepend a warning as a pseudo trigger
233 # if there are some
234 0 ▶ if($self->warn_unsupported()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->warn_unsupported) 0 0 0
235 0 my $unsupported = $self->unsupported_items();
236 0 ▶ if($unsupported && ref($unsupported) eq 'ARRAY' && scalar @{$unsupported} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unsupported and ref $unsupported eq "ARRAY" and scalar @{$unsupported;} > 0) 0 0 0
Condition0%0/6
$unsupported and ref $unsupported eq "ARRAY" and scalar @{$unsupported;} > 0
!ll&&!rl&&r
000
$unsupported and ref $unsupported eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
$unsupported and ref $unsupported eq "ARRAY" and scalar @{$unsupported;} > 0
$unsupported ref $unsupported eq "ARRAY" scalar @{$unsupported;} > 0
Truth table0%0/4
A and B and C
A= $unsupportedB= ref $unsupported eq "ARRAY"C= scalar @{$unsupported;} > 0
ABCresult
0XX0
10X0
1100
1111
237 my $row = {
238 'severity' => 'high',
239 'host' => 'Zabbix',
240 'description' => 'Unsupported Items!',
241 'lastchange' => time(),
242 0 'comments' => 'There are '.(scalar @{$unsupported}).' unsupported items.',
243 };
244 0 unshift @{$rows}, $row;
245 }
246 }
247  
248 # Check for any unattended alarms and prepend a warning as a pseudo trigger
249 # if there are some
250 0 ▶ if($self->warn_unattended()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->warn_unattended) 0 0 0
251 0 my $unattended = $self->unattended_alarms();
252 0 ▶ if($unattended && ref($unattended) eq 'ARRAY' && scalar @{$unattended} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unattended and ref $unattended eq "ARRAY" and scalar @{$unattended;} > 0) 0 0 0
Condition0%0/6
$unattended and ref $unattended eq "ARRAY" and scalar @{$unattended;} > 0
!ll&&!rl&&r
000
$unattended and ref $unattended eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
$unattended and ref $unattended eq "ARRAY" and scalar @{$unattended;} > 0
$unattended ref $unattended eq "ARRAY" scalar @{$unattended;} > 0
Truth table0%0/4
A and B and C
A= $unattendedB= ref $unattended eq "ARRAY"C= scalar @{$unattended;} > 0
ABCresult
0XX0
10X0
1100
1111
253 0 my $row = {
254 'severity' => 'high',
255 'host' => 'Zabbix',
256 'description' => 'Unattended Alarms!',
257 'lastchange' => time(),
258 'comments' => 'There are '.$unattended->[0].' unattended alarms.',
259 };
260 0 unshift @{$rows}, $row;
261 }
262 }
263 # Check for any disabled actions and prepend a warning as a pseudo trigger
264 # if there are some
265 0 my $disacts = $self->disabled_actions();
266 0 ▶ if($disacts && ref($disacts) eq 'ARRAY' && scalar @{$disacts} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disacts and ref $disacts eq "ARRAY" and scalar @{$disacts;} > 0) 0 0 0
Condition0%0/6
$disacts and ref $disacts eq "ARRAY" and scalar @{$disacts;} > 0
!ll&&!rl&&r
000
$disacts and ref $disacts eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
$disacts and ref $disacts eq "ARRAY" and scalar @{$disacts;} > 0
$disacts ref $disacts eq "ARRAY" scalar @{$disacts;} > 0
Truth table0%0/4
A and B and C
A= $disactsB= ref $disacts eq "ARRAY"C= scalar @{$disacts;} > 0
ABCresult
0XX0
10X0
1100
1111
267 my $row = {
268 'severity' => 'high',
269 'host' => 'Zabbix',
270 'description' => 'Notifications disabled!',
271 'lastchange' => time(),
272 0 'comments' => 'There are '.(scalar @{$disacts}).' notifications disabled. Please make sure you enable them again in time.',
273 };
274 0 unshift @{$rows}, $row;
275 }
276  
277 0 return $rows;
278 }
279  
280 0 ▶sub acks {
Subroutine acks: not called CC=4 SCAR=30.0
Pod documented
281 0 my $self = shift;
282 0 my $triggerid = shift;
283 0 my $triggerclock = shift;
284  
285 0 my $sql = <<'EOS';
286 SELECT
287 e.eventid,
288 e.clock AS eventclock,
289 e.value,
290 e.acknowledged,
291 a.acknowledgeid,
292 a.userid,
293 a.clock AS ackclock,
294 a.message,
295 u.alias AS user
296 FROM
297 events AS e
298 LEFT JOIN
299 acknowledges AS a
300 ON
301 e.eventid = a.eventid
302 LEFT JOIN
303 users AS u
304 ON
305 a.userid = u.userid
306 WHERE
307 e.source = 0 AND
308 e.object = 0 AND
309 e.objectid = ? AND
310 e.clock >= ?
311 ORDER BY
312 eventid DESC
313 LIMIT 1
314 EOS
315  
316 0 my $rows = $self->fetch_n_store($sql,60,($triggerid,$triggerclock));
317  
318 # Post processing
319 0 ▶ if($rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows) 0 0 0
320 0 foreach my $row (@{$rows}) {
321 0 ▶ if (defined($row->{'value'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"value"}) 0 0 0
322 0 $row->{'status'} = $self->_event_value_map()->[$row->{'value'}];
323 }
324 }
325 }
326  
327 0 return $rows;
328 }
329  
330 0 ▶sub disabled_actions {
Subroutine disabled_actions: not called CC=1 SCAR=6.9
Pod documented
331 0 my $self = shift;
332  
333 # status = 0 -> action is enabled
334 # status = 1 -> action is disabled
335 0 my $sql = <<'EOS';
336 SELECT
337 a.actionid,
338 a.name,
339 a.status
340 FROM
341 actions AS a
342 WHERE
343 a.eventsource = 0 AND
344 status = 1
345 EOS
346  
347 0 my $rows = $self->fetch_n_store($sql,60);
348  
349 0 return $rows;
350 }
351  
352 0 ▶sub enable_actions {
Subroutine enable_actions: not called CC=1 SCAR=6.9
Pod documented
353 0 my $self = shift;
354  
355 # status = 0 -> action is enabled
356 # status = 1 -> action is disabled
357 0 my $sql = <<'EOS';
358 UPDATE
359 actions
360 SET
361 status = 0
362 WHERE
363 status = 1 AND
364 eventsource = 0
365 EOS
366  
367 0 my $rows = $self->fetch($sql);
368  
369 0 return $rows;
370 }
371  
372 0 ▶sub unsupported_items {
Subroutine unsupported_items: not called CC=1 SCAR=6.9
Pod documented
373 0 my $self = shift;
374  
375 # i.status 3 == Item Error: Not Supported
376 0 my $sql = <<'EOS';
377 SELECT
378 h.host,
379 i.key_,
380 i.lastclock
381 FROM
382 items i
383 JOIN hosts h ON (h.hostid = i.hostid)
384 WHERE
385 i.status = 3
386 ORDER BY
387 i.lastclock DESC
388 EOS
389  
390 0 my $rows = $self->fetch_n_store($sql,3600);
391  
392 0 return $rows;
393 }
394  
395 0 ▶sub unattended_alarms {
Subroutine unattended_alarms: not called CC=2 SCAR=17.9
Pod documented
396 0 my $self = shift;
397 0 ▶ my $time = shift || 3600;
Condition0%0/2
shift() || 3600
l!l
00
MC/DC0%0/1
shift() || 3600
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
398  
399 0 my $sql = <<'EOS';
400 SELECT
401 COUNT(t.triggerid) AS alarms
402 FROM
403 triggers AS t
404 JOIN functions AS f ON (f.triggerid = t.triggerid)
405 JOIN items AS i ON (i.itemid = f.itemid)
406 JOIN hosts AS h ON (h.hostid = i.hostid)
407 JOIN events AS e ON (e.objectid = t.triggerid)
408 WHERE
409 t.lastchange = e.clock AND
410 h.status= 0 AND
411 t.value = 1 AND
412 t.status = 0 AND
413 i.status = 0 AND
414 t.lastchange < ( UNIX_TIMESTAMP() - ? ) AND
415 e.acknowledged = 0
416 EOS
417  
418 0 my $rows = $self->fetch_n_store($sql,360,$time);
419  
420 0 return $rows;
421 }
422  
423 0 ▶sub history {
Subroutine history: not called CC=8 SCAR=42.8
Pod documented
424 0 my $self = shift;
425 0 ▶ my $max_age = shift // 30;
Condition0%0/2
shift() // 30
l!l
00
MC/DC0%0/1
shift() // 30
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
426 0 ▶ my $max_num = shift // 100;
Condition0%0/2
shift() // 100
l!l
00
MC/DC0%0/1
shift() // 100
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
427  
428 0 my $sql = <<'EOS';
429 SELECT
430 h.host,
431 t.description,
432 t.priority,
433 e.clock
434 FROM
435 events e
436 JOIN triggers t ON (t.triggerid = e.objectid)
437 JOIN functions f ON (f.triggerid = t.triggerid)
438 JOIN items i ON (i.itemid = f.itemid)
439 JOIN hosts h ON (h.hostid = i.hostid)
440 WHERE
441 e.source = 0 AND
442 e.value = 1
443 EOS
444 0 my @args = ();
445 0 ▶ if($max_age) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_age) 0 0 0
446 0 $sql .= ' AND e.clock > UNIX_TIMESTAMP(NOW() - INTERVAL ? DAY)';
447 0 push(@args,$max_age);
448 }
449 0 $sql .= ' ORDER BY e.clock DESC';
450 0 ▶ if($max_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_num) 0 0 0
451 0 $sql .= ' LIMIT ?';
452 0 push(@args,$max_num);
453 }
454 0 my $rows = $self->fetch_n_store($sql,120,@args);
455  
456 # Postprocessing
457 0 ▶ if($rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows) 0 0 0
458 0 foreach my $row (@{$rows}) {
459 0 ▶ if (defined($row->{'priority'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"priority"}) 0 0 0
460 0 $row->{'severity'} = $self->_severity_map()->[$row->{'priority'}];
461 }
462 }
463 }
464 0 return $rows;
465 }
466 2 __PACKAGE__->meta->make_immutable;
467  
468 2 1; # End of Zabbix::Reporter
469  
470 __END__
↑ Summary lib/Zabbix/Reporter/Web.pm »