lib/Monitoring/Reporter/Backend/ZabbixDBI.pm

filter: Statementstmt 13.6% 32 / 235 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 21.4% 6 / 28 Podpod 100% 11 / 11 totaltotal 11.9% 49 / 409 CC 56 SCAR 56.6
CC
56
Cov
9%
triggers
812.0
history
72.0
selftest
30.0
CRAP 288.0
↑ Summary lib/Monitoring/Reporter.pm »
1 package Monitoring::Reporter::Backend::ZabbixDBI;
2 {
3 1 $Monitoring::Reporter::Backend::ZabbixDBI::VERSION = '0.01';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Monitoring::Reporter::Backend::ZabbixDBI::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: Monitoring dashboard
9  
10 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Cache::MemoryCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 has 'dbh' => (
18 'is' => 'rw',
19 'isa' => 'DBI::db',
20 'lazy' => 1,
21 'builder' => '_init_dbh',
22 );
23  
24 1 has 'cache' => (
25 'is' => 'rw',
26 'isa' => 'Cache::Cache',
27 'lazy' => 1,
28 'builder' => '_init_cache',
29 );
30  
31 has 'priorities' => (
32 'is' => 'rw',
33 'isa' => 'ArrayRef',
34 0 ▶ 'default' => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 1 );
36  
37 1 has 'min_age' => (
38 'is' => 'rw',
39 'isa' => 'Int',
40 'default' => 0,
41 );
42  
43 1 has 'warn_unsupported' => (
44 'is' => 'rw',
45 'isa' => 'Bool',
46 'default' => 0,
47 );
48  
49 1 has 'warn_unattended' => (
50 'is' => 'rw',
51 'isa' => 'Bool',
52 'default' => 0,
53 );
54  
55 has 'groups' => (
56 'is' => 'rw',
57 'isa' => 'ArrayRef',
58 0 ▶ 'default' => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 1 );
60  
61 has '_severity_map' => (
62 'is' => 'ro',
63 'isa' => 'ArrayRef',
64 0 ▶ 'default' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 [qw(nc information warning average high disaster)],
66 },
67 1 );
68  
69 has '_event_value_map' => (
70 'is' => 'ro',
71 'isa' => 'ArrayRef',
72 0 ▶ 'default' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 [qw(OK PROBLEM UNKNOWN)],
74 },
75 1 );
76  
77 1 has 'zabbix_server_address' => (
78 'is' => 'ro',
79 'isa' => 'Str',
80 'lazy' => 1,
81 'builder' => '_init_zabbix_server_address',
82 );
83  
84 1 extends 'Monitoring::Reporter::Backend';
85  
86 1 with qw(Config::Yak::RequiredConfig Log::Tree::RequiredLogger);
87  
88 0 ▶sub _dsn {
Subroutine _dsn: not called CC=1 SCAR=6.9
89 0 my $self = shift;
90  
91 0 my $hostname = $self->config()->get( 'Monitoring::Reporter::DB::Hostname', { Default => 'localhost', } );
92 0 my $port = $self->config()->get( 'Monitoring::Reporter::DB::Port', { Default => 3306, } );
93 0 my $database = $self->config()->get( 'Monitoring::Reporter::DB::Database', { Default => 'zabbix', } );
94 0 my $username = $self->config()->get( 'Monitoring::Reporter::DB::Username', { Default => 'zabbix', } );
95 0 my $password = $self->config()->get( 'Monitoring::Reporter::DB::Password', { Default => 'zabbix', } );
96  
97 0 my $dsn = 'DBI:mysql:user=' . $username;
98 0 $dsn .= ';password=' . $password;
99 0 $dsn .= ';host=' . $hostname . ';port=' . $port;
100 0 $dsn .= ';database=' . $database;
101  
102 0 return $dsn;
103 }
104  
105 0 ▶sub _init_dbh {
Subroutine _init_dbh: not called CC=1 SCAR=6.9
106 0 my $self = shift;
107  
108 0 my $dbh = DBI->connect($self->_dsn());
109 0 $dbh->{'mysql_auto_reconnect'} = 1;
110  
111 0 return $dbh;
112 }
113  
114 0 ▶sub _init_zabbix_server_address { return 'localhost'; }
Subroutine _init_zabbix_server_address: not called CC=1 SCAR=6.9
115  
116 0 ▶sub _init_cache {
Subroutine _init_cache: not called CC=1 SCAR=6.9
117 0 my $self = shift;
118  
119 0 my $Cache = Cache::MemoryCache::->new({
120 'namespace' => 'MonitoringReporter',
121 'default_expires_in' => 600,
122 });
123  
124 0 return $Cache;
125 }
126  
127 0 ▶sub fetch_n_store {
Subroutine fetch_n_store: not called CC=2 SCAR=17.9
Pod documented
128 0 my $self = shift;
129 0 my $query = shift;
130 0 my $timeout = shift;
131 0 my @args = @_;
132  
133 0 my $key = $query.join(',',@args);
134  
135 0 my $result = $self->cache()->get($key);
136  
137 0 ▶ if( ! defined($result) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 0 0
138 0 $result = $self->fetch($query,@args);
139 0 $self->cache()->set($key,$result,$timeout);
140 }
141  
142 0 return $result;
143 }
144  
145 0 ▶sub fetch {
Subroutine fetch: not called CC=4 SCAR=30.0
Pod documented
146 0 my $self = shift;
147 0 my $query = shift;
148 0 my @args = @_;
149  
150 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
151 or die("Could not prepare query $query: ".$self->dbh()->errstr);
152  
153 0 ▶ $sth->execute(@args)
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@args) or die "Could not execute query ${query}: " . $self->dbh->errstr 0 0 0
154 or die("Could not execute query $query: ".$self->dbh()->errstr);
155  
156 0 my @result = ();
157  
158 0 ▶ while(my $ref = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $sth->fetchrow_hashref) 0 0 0
159 0 push(@result,$ref);
160 }
161 0 $sth->finish();
162  
163 0 return \@result;
164 }
165  
166 0 ▶sub do {
Subroutine do: not called CC=3 SCAR=24.8
Pod documented
167 0 my $self = shift;
168 0 my $query = shift;
169 0 my @args = @_;
170  
171 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
172 or die("Could not prepare query $query: ".$self->dbh()->errstr);
173  
174 0 ▶ $sth->execute(@args)
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@args) or die "Could not execute query ${query}: " . $self->dbh->errstr 0 0 0
175 or die("Could not execute query $query: ".$self->dbh()->errstr);
176  
177 0 return 1;
178 }
179  
180 0 ▶sub triggers {
Subroutine triggers: not called CC=28 SCAR=67.0
Pod documented
181 0 my $self = shift;
182  
183 0 my $sql = <<'EOS';
184 SELECT
185 t.priority,
186 h.host,
187 t.description,
188 h.hostid,
189 t.triggerid,
190 i.itemid,
191 i.lastvalue,
192 i.lastclock,
193 t.lastchange,
194 t.value,
195 t.comments,
196 i.units,
197 i.valuemapid,
198 d.triggerdepid
199 FROM
200 hosts AS h
201 JOIN items AS i ON (h.hostid = i.hostid)
202 JOIN functions AS f ON (i.itemid = f.itemid)
203 JOIN triggers AS t ON (f.triggerid = t.triggerid)
204 LEFT JOIN trigger_depends AS d ON (d.triggerid_down = t.triggerid)
205 WHERE
206 h.status = 0 AND
207 t.value = 1 AND
208 t.status = 0 AND
209 i.status = 0
210 EOS
211 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
212 0 $sql .= ' AND t.priority IN ('.join(',',@{$self->priorities()}).')';
213 }
214 0 ▶ if($self->min_age()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->min_age) 0 0 0
215 0 $sql .= ' AND t.lastchange < UNIX_TIMESTAMP(NOW() - INTERVAL '.$self->min_age().' MINUTE)';
216 }
217 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
218 0 my $sub_sql = "SELECT hostid FROM host_groups WHERE groupid IN (".join(',',@{$self->groups()}).")";
219 0 my $hostids = $self->fetch_n_store($sub_sql,60);
220 0 $sql .= ' AND h.hostid IN ('.join(',',@{$hostids}).')';
221 }
222 0 $sql .= ' GROUP BY t.triggerid';
223 0 $sql .= ' ORDER BY t.priority DESC, h.host';
224 0 my $rows = $self->fetch_n_store($sql,60);
225  
226 # Post processing
227 0 my @unacked = ();
228 0 my @acked = ();
229 0 ▶ if($rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows) 0 0 0
230 0 foreach my $row (@{$rows}) {
231 0 ▶ if (defined($row->{'priority'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"priority"}) 0 0 0
232 0 $row->{'severity'} = $self->_severity_map()->[$row->{'priority'}];
233 }
234 0 ▶ if (defined($row->{'description'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"description"}) 0 0 0
235 0 $row->{'description'} =~ s/\{HOSTNAME\}/$row->{'host'}/g;
236 0 $row->{'description'} =~ s/\{HOST\.DNS\}/$row->{'host'}/g;
237 }
238 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
239 0 my $ack = $self->acks($row->{'triggerid'},$row->{'lastclock'});
240 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
241 0 foreach my $field (keys %{$ack->[0]}) {
242 0 $row->{$field} = $ack->[0]->{$field};
243 }
244 }
245 }
246 # this should be the last post-processing action
247 0 ▶ if($row->{'acknowledged'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{'acknowledged'}) { } 0 0 0
248 0 push(@acked,$row);
249 } else {
250 0 push(@unacked,$row);
251 }
252 }
253 }
254 # sort acked triggers to the end
255 0 @{$rows} = (@unacked,@acked);
256  
257 # Check for any unsupported items and prepend a warning as a pseudo trigger
258 # if there are some
259 0 ▶ if($self->warn_unsupported()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->warn_unsupported) 0 0 0
260 0 my $unsupported = $self->unsupported_items();
261 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
262 my $row = {
263 'severity' => 'high',
264 'host' => 'Monitoring',
265 'description' => 'Unsupported Items!',
266 'lastchange' => time(),
267 0 'comments' => 'There are '.(scalar @{$unsupported}).' unsupported items.',
268 };
269 0 unshift @{$rows}, $row;
270 }
271 }
272  
273 # Check for any unattended alarms and prepend a warning as a pseudo trigger
274 # if there are some
275 0 ▶ if($self->warn_unattended()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->warn_unattended) 0 0 0
276 0 my $unattended = $self->unattended_alarms();
277 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
278 0 my $row = {
279 'severity' => 'high',
280 'host' => 'Monitoring',
281 'description' => 'Unattended Alarms!',
282 'lastchange' => time(),
283 'comments' => 'There are '.$unattended->[0].' unattended alarms.',
284 };
285 0 unshift @{$rows}, $row;
286 }
287 }
288 # Check for any disabled actions and prepend a warning as a pseudo trigger
289 # if there are some
290 0 my $disacts = $self->disabled_actions();
291 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
292 my $row = {
293 'severity' => 'high',
294 'host' => 'Monitoring',
295 'description' => 'Notifications disabled!',
296 'lastchange' => time(),
297 0 'comments' => 'There are '.(scalar @{$disacts}).' notifications disabled. Please make sure you enable them again in time.',
298 };
299 0 unshift @{$rows}, $row;
300 }
301  
302 0 return $rows;
303 }
304  
305 0 ▶sub acks {
Subroutine acks: not called CC=4 SCAR=30.0
Pod documented
306 0 my $self = shift;
307 0 my $triggerid = shift;
308 0 my $triggerclock = shift;
309  
310 0 my $sql = <<'EOS';
311 SELECT
312 e.eventid,
313 e.clock AS eventclock,
314 e.value,
315 e.acknowledged,
316 a.acknowledgeid,
317 a.userid,
318 a.clock AS ackclock,
319 a.message,
320 u.alias AS user
321 FROM
322 events AS e
323 LEFT JOIN
324 acknowledges AS a
325 ON
326 e.eventid = a.eventid
327 LEFT JOIN
328 users AS u
329 ON
330 a.userid = u.userid
331 WHERE
332 e.source = 0 AND
333 e.object = 0 AND
334 e.objectid = ? AND
335 e.clock >= ?
336 ORDER BY
337 eventid DESC
338 LIMIT 1
339 EOS
340  
341 0 my $rows = $self->fetch_n_store($sql,60,($triggerid,$triggerclock));
342  
343 # Post processing
344 0 ▶ if($rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows) 0 0 0
345 0 foreach my $row (@{$rows}) {
346 0 ▶ if (defined($row->{'value'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"value"}) 0 0 0
347 0 $row->{'status'} = $self->_event_value_map()->[$row->{'value'}];
348 }
349 }
350 }
351  
352 0 return $rows;
353 }
354  
355 0 ▶sub disabled_actions {
Subroutine disabled_actions: not called CC=1 SCAR=6.9
Pod documented
356 0 my $self = shift;
357  
358 # status = 0 -> action is enabled
359 # status = 1 -> action is disabled
360 0 my $sql = <<'EOS';
361 SELECT
362 a.actionid,
363 a.name,
364 a.status
365 FROM
366 actions AS a
367 WHERE
368 a.eventsource = 0 AND
369 status = 1
370 EOS
371  
372 0 my $rows = $self->fetch_n_store($sql,60);
373  
374 0 return $rows;
375 }
376  
377 0 ▶sub enable_actions {
Subroutine enable_actions: not called CC=1 SCAR=6.9
Pod documented
378 0 my $self = shift;
379  
380 # status = 0 -> action is enabled
381 # status = 1 -> action is disabled
382 0 my $sql = <<'EOS';
383 UPDATE
384 actions
385 SET
386 status = 0
387 WHERE
388 status = 1 AND
389 eventsource = 0
390 EOS
391  
392 0 my $rows = $self->do($sql);
393  
394 0 return $rows;
395 }
396  
397 0 ▶sub unsupported_items {
Subroutine unsupported_items: not called CC=1 SCAR=6.9
Pod documented
398 0 my $self = shift;
399  
400 # i.status 3 == Item Error: Not Supported
401 0 my $sql = <<'EOS';
402 SELECT
403 h.host,
404 i.key_,
405 i.lastclock
406 FROM
407 items i
408 JOIN hosts h ON (h.hostid = i.hostid)
409 WHERE
410 i.status = 3
411 ORDER BY
412 i.lastclock DESC
413 EOS
414  
415 0 my $rows = $self->fetch_n_store($sql,3600);
416  
417 0 return $rows;
418 }
419  
420 0 ▶sub unattended_alarms {
Subroutine unattended_alarms: not called CC=2 SCAR=17.9
Pod documented
421 0 my $self = shift;
422 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
423  
424 0 my $sql = <<'EOS';
425 SELECT
426 COUNT(t.triggerid) AS alarms
427 FROM
428 triggers AS t
429 JOIN functions AS f ON (f.triggerid = t.triggerid)
430 JOIN items AS i ON (i.itemid = f.itemid)
431 JOIN hosts AS h ON (h.hostid = i.hostid)
432 JOIN events AS e ON (e.objectid = t.triggerid)
433 WHERE
434 t.lastchange = e.clock AND
435 h.status= 0 AND
436 t.value = 1 AND
437 t.status = 0 AND
438 i.status = 0 AND
439 t.lastchange < ( UNIX_TIMESTAMP() - ? ) AND
440 e.acknowledged = 0
441 EOS
442  
443 0 my $rows = $self->fetch_n_store($sql,360,$time);
444  
445 0 return $rows;
446 }
447  
448 0 ▶sub history {
Subroutine history: not called CC=8 SCAR=42.8
Pod documented
449 0 my $self = shift;
450 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
451 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
452  
453 0 my $sql = <<'EOS';
454 SELECT
455 h.host,
456 t.description,
457 t.priority,
458 e.clock
459 FROM
460 events e
461 JOIN triggers t ON (t.triggerid = e.objectid)
462 JOIN functions f ON (f.triggerid = t.triggerid)
463 JOIN items i ON (i.itemid = f.itemid)
464 JOIN hosts h ON (h.hostid = i.hostid)
465 WHERE
466 e.source = 0 AND
467 e.value = 1
468 EOS
469 0 my @args = ();
470 0 ▶ if($max_age) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_age) 0 0 0
471 0 $sql .= ' AND e.clock > UNIX_TIMESTAMP(NOW() - INTERVAL ? DAY)';
472 0 push(@args,$max_age);
473 }
474 0 $sql .= ' ORDER BY e.clock DESC';
475 0 ▶ if($max_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_num) 0 0 0
476 0 $sql .= ' LIMIT ?';
477 0 push(@args,$max_num);
478 }
479 0 my $rows = $self->fetch_n_store($sql,120,@args);
480  
481 # Postprocessing
482 0 ▶ if($rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows) 0 0 0
483 0 foreach my $row (@{$rows}) {
484 0 ▶ if (defined($row->{'priority'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"priority"}) 0 0 0
485 0 $row->{'severity'} = $self->_severity_map()->[$row->{'priority'}];
486 }
487 }
488 }
489 0 return $rows;
490 }
491  
492 0 ▶sub selftest {
Subroutine selftest: not called CC=5 SCAR=34.0
Pod documented
493 0 my $self = shift;
494  
495 0 my $ok = 1;
496 0 my @msgs = ();
497  
498 0 ▶ if($self->_selftest_check_db_ping()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_selftest_check_db_ping) { } 0 0 0
499 0 push(@msgs,"OK - DB Connection is working");
500 } else {
501 0 push(@msgs,"ERROR - DB connection not working!");
502 0 $ok = 0;
503 }
504  
505 # Make sure there were any event during the last 5 minutes
506 0 ▶ if($self->_selftest_check_db_count('SELECT COUNT(*) FROM events WHERE clock >= UNIX_TIMESTAMP(NOW())-300')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_selftest_check_db_count('SELECT COUNT(*) FROM events WHERE clock >= UNIX_TIMESTAMP(NOW())-300')) { } 0 0 0
507 0 push(@msgs,"OK - Some events during the last 5 minutes");
508 } else {
509 0 push(@msgs,"ERROR - No events during the last 5 minutes");
510 0 $ok = 0;
511 }
512  
513 # Make sure there was at least on trigger event in the last 24h
514 0 ▶ if($self->_selftest_check_db_count('SELECT COUNT(*) FROM triggers AS t WHERE t.lastchange > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_selftest_check_db_count('SELECT COUNT(*) FROM triggers AS t WHERE t.lastchange > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')) { } 0 0 0
515 0 push(@msgs,"OK - Some events during the last 5 minutes");
516 } else {
517 0 push(@msgs,"ERROR - No events during the last 5 minutes");
518 0 $ok = 0;
519 }
520  
521 # Make sure the server process is listening on port 10051
522 0 ▶ if($self->_selftest_check_open_port(10051)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_selftest_check_open_port(10051)) { } 0 0 0
523 0 push(@msgs,"OK - Server is listening on port 10051");
524 } else {
525 0 push(@msgs,"ERROR - Server is not listening on port 10051");
526 0 $ok = 0,
527 }
528
529 0 return ($ok,\@msgs);
530 }
531  
532 0 ▶sub _selftest_check_open_port {
Subroutine _selftest_check_open_port: not called CC=3 SCAR=24.8
533 0 my $self = shift;
534 0 ▶ my $port = shift || 10051;
Condition0%0/2
shift() || 10051
l!l
00
MC/DC0%0/1
shift() || 10051
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
535  
536 0 my $sock = IO::Socket::INET::->new(
537 Proto => 'tcp',
538 PeerAddr => $self->zabbix_server_address(),
539 PeerPort => $port,
540 Timeout => 10,
541 );
542  
543 0 ▶ if($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 0 0 0
544 0 close($sock);
545 0 return 1;
546 }
547  
548 0 return;
549 }
550  
551 0 ▶sub _selftest_check_db_ping {
Subroutine _selftest_check_db_ping: not called CC=2 SCAR=17.9
552 0 my $self = shift;
553  
554 0 ▶ if($self->dbh()->ping()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dbh->ping) 0 0 0
555 0 return 1;
556 }
557  
558 0 return;
559 }
560  
561 0 ▶sub _selftest_check_db_count {
Subroutine _selftest_check_db_count: not called CC=5 SCAR=34.0
562 0 my $self = shift;
563 0 my $sql = shift;
564  
565 0 ▶ if(!$sql) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sql) 0 0 0
566 0 return;
567 }
568  
569 0 my $sth = $self->dbh()->prepare($sql);
570 0 ▶ if(!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
571 0 return;
572 }
573 0 ▶ if(!$sth->execute()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->execute) 0 0 0
574 0 return;
575 }
576 0 my $count = $sth->fetchrow_array();
577  
578 0 ▶ if($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 0 0 0
579 0 return 1;
580 }
581  
582 0 return;
583 }
584 1 __PACKAGE__->meta->make_immutable;
585  
586 1 1; # End of Monitoring::Reporter
587  
588 __END__
↑ Summary lib/Monitoring/Reporter.pm »