lib/Monitoring/Reporter.pm

filter: Statementstmt 18.1% 26 / 143 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 27.2% 6 / 22 Podpod 100% 7 / 7 totaltotal 14.8% 39 / 263 CC 37 SCAR 47.2
CC
37
Cov
12%
triggers
272.0
_backend_filter_match
156.0
_do_backend_action
30.0
CRAP 112.0
« lib/Monitoring/Reporter/Backend/ZabbixDBI.pm ↑ Summary lib/Monitoring/Reporter/Web.pm »
1 package Monitoring::Reporter;
2 {
3 1 $Monitoring::Reporter::VERSION = '0.01';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Monitoring::Reporter::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 Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 has 'backends' => (
18 'is' => 'rw',
19 'isa' => 'HashRef',
20 'lazy' => 1,
21 'builder' => '_init_backends',
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 'warn_unsupported' => (
38 'is' => 'rw',
39 'isa' => 'Bool',
40 'default' => 0,
41 );
42  
43 1 has 'warn_unattended' => (
44 'is' => 'rw',
45 'isa' => 'Bool',
46 'default' => 0,
47 );
48  
49 1 with qw(Config::Yak::RequiredConfig Log::Tree::RequiredLogger);
50  
51 0 ▶sub _init_cache {
Subroutine _init_cache: not called CC=1 SCAR=6.9
52 0 my $self = shift;
53  
54 0 my $Cache = Cache::MemoryCache::->new({
55 'namespace' => 'MonitoringReporter',
56 'default_expires_in' => 600,
57 });
58  
59 0 return $Cache;
60 }
61  
62 0 ▶sub _init_backends {
Subroutine _init_backends: not called CC=3 SCAR=24.8
63 0 my $self = shift;
64  
65 0 my $backends_config = $self->config()->get( 'Monitoring::Reporter::Backend', { Default => {}, }, );
66 0 my $backends = {};
67  
68 0 BACKEND: foreach my $backend ( sort keys %{$backends_config} ) {
69 0 my $arg_ref = {
70 'cache' => $self->cache(),
71 'config' => $self->config(),
72 'logger' => $self->logger(),
73 'name' => $backend,
74 };
75  
76 0 my $klass = 'Monitoring::Reporter::Backend::'.$backends_config->{$backend}->{'type'};
77 ## no critic (ProhibitStringyEval)
78 0 my $eval_status = eval "require $klass;";
79 ## use critic
80 0 ▶ if(!$eval_status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eval_status) 0 0 0
81 0 $self->logger()->log( message => 'Failed to log backend '.$klass.': '.$@, level => 'warning', );
82 0 next BACKEND;
83 }
84 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 my $BE = $klass->new($arg_ref);
86 0 $backends->{$backend} = $BE;
87 0 $self->logger()->log( message => 'Initialized backend '.$klass, level => 'debug', );
88 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 $self->logger()->log( message => 'Failed to initialize backend '.$klass.' w/ error: '.$_, level => 'warning', );
90 0 };
91 }
92  
93 0 return $backends;
94 }
95  
96 0 ▶sub triggers {
Subroutine triggers: not called CC=16 SCAR=56.1
Pod documented
97 0 my $self = shift;
98  
99 0 my $row_ref = $self->_do_backend_action('triggers');
100  
101 # Post processing
102 # - Sort triggers by severity and age
103 0 $row_ref = [sort { $b->{'priority'} <=> $a->{'priority'} } @{$row_ref}];
104 # - Sort acked triggers to the end
105 0 my @unacked = ();
106 0 my @acked = ();
107 0 ROW: foreach my $row (@{$row_ref}) {
108 0 ▶ if(!$row || ref($row) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $row or ref $row ne "HASH") 0 0 0
Condition0%0/3
not $row or ref $row ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $row or ref $row ne "HASH"
not $row ref $row ne "HASH"
Truth table0%0/3
not(A) or B
A= not $rowB= ref $row ne "HASH"
ABresult
1X1
011
000
109 0 $self->logger()->log( message => 'Skipping invalid row: '.$row, level => 'warning', );
110 0 next ROW;
111 }
112 # this should be the last post-processing action
113 0 ▶ if($row->{'acknowledged'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{'acknowledged'}) { } 0 0 0
114 0 push(@acked,$row);
115 } else {
116 0 push(@unacked,$row);
117 }
118 }
119 # sort acked triggers to the end
120 0 my @rows = (@unacked,@acked);
121  
122 # Check for any unsupported items and prepend a warning as a pseudo trigger
123 # if there are some
124 0 ▶ if($self->warn_unsupported()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->warn_unsupported) 0 0 0
125 0 my $unsupported = $self->unsupported_items();
126 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
127 my $row = {
128 'severity' => 'high',
129 'host' => 'Monitoring',
130 'description' => 'Unsupported Items!',
131 'lastchange' => time(),
132 0 'comments' => 'There are '.(scalar @{$unsupported}).' unsupported items.',
133 };
134 0 unshift @rows, $row;
135 }
136 }
137  
138 # Check for any unattended alarms and prepend a warning as a pseudo trigger
139 # if there are some
140 0 ▶ if($self->warn_unattended()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->warn_unattended) 0 0 0
141 0 my $unattended = $self->unattended_alarms();
142 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
143 0 my $row = {
144 'severity' => 'high',
145 'host' => 'Monitoring',
146 'description' => 'Unattended Alarms!',
147 'lastchange' => time(),
148 'comments' => 'There are '.$unattended->[0].' unattended alarms.',
149 };
150 0 unshift @rows, $row;
151 }
152 }
153 # Check for any disabled actions and prepend a warning as a pseudo trigger
154 # if there are some
155 0 my $disacts = $self->disabled_actions();
156 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
157 my $row = {
158 'severity' => 'high',
159 'host' => 'Monitoring',
160 'description' => 'Notifications disabled!',
161 'lastchange' => time(),
162 0 'comments' => 'There are '.(scalar @{$disacts}).' notifications disabled. Please make sure you enable them again in time.',
163 };
164 0 unshift @rows, $row;
165 }
166  
167 0 return \@rows;
168 }
169  
170 0 ▶sub disabled_actions {
Subroutine disabled_actions: not called CC=1 SCAR=6.9
Pod documented
171 0 my $self = shift;
172  
173 0 return $self->_do_backend_action('disabled_actions');
174 }
175  
176 0 ▶sub enable_actions {
Subroutine enable_actions: not called CC=1 SCAR=6.9
Pod documented
177 0 my $self = shift;
178  
179 0 return $self->_do_backend_action('enable_actions');
180 }
181  
182 0 ▶sub unsupported_items {
Subroutine unsupported_items: not called CC=1 SCAR=6.9
Pod documented
183 0 my $self = shift;
184  
185 0 return $self->_do_backend_action('unsupported_items');
186 }
187  
188 0 ▶sub unattended_alarms {
Subroutine unattended_alarms: not called CC=2 SCAR=17.9
Pod documented
189 0 my $self = shift;
190 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
191  
192 0 return $self->_do_backend_action('unattended_alarms', { 'time' => 3600, }, );
193 }
194  
195 0 ▶sub history {
Subroutine history: not called CC=3 SCAR=24.8
Pod documented
196 0 my $self = shift;
197 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
198 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
199  
200 0 my $arg_ref = {
201 'max_age' => $max_age,
202 'max_num' => $max_num,
203 };
204 0 return $self->_do_backend_action('history', $arg_ref);
205 }
206  
207 0 ▶sub selftest {
Subroutine selftest: not called CC=2 SCAR=17.9
Pod documented
208 0 my $self = shift;
209  
210 0 ▶ if(scalar(keys %{$self->backends()}) < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->backends;} < 1) 0 0 0
211 0 return (0, 'No Backends configured!');
212 }
213 0 my $be_stati = $self->_do_backend_action('selftest');
214 # TODO check backend stati
215 #return (1, 'OK');
216 0 return (0, 'Not implemented');
217 }
218  
219 0 ▶sub _do_backend_action {
Subroutine _do_backend_action: not called CC=5 SCAR=34.0
220 0 my ($self, $action, $arg_ref) = @_;
221  
222 0 my @rows = ();
223  
224 0 BACKEND: foreach my $backend (sort keys %{$self->backends()}) {
225 0 ▶ if($self->_backend_filter_match($backend, $arg_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_backend_filter_match($backend, $arg_ref)) { } 0 0 0
226 0 $self->logger()->log( message => 'Executing action '.$action.' on backend '.$backend, level => 'debug', );
227 } else {
228 0 $self->logger()->log( message => 'Skipping Backend '.$backend.' for action '.$action.': No filter match.', level => 'notice', );
229 0 next BACKEND;
230 }
231 0 my $row_ref;
232 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 0 $row_ref = $self->backends()->{$backend}->$action($arg_ref);
234 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
235 0 $self->logger()->log( message => 'Failed to execute action '.$action.' on backend '.$backend.': '.$_, level => 'error', );
236 0 };
237 0 ▶ if($row_ref && ref($row_ref) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_ref and ref $row_ref eq 'ARRAY') { } 0 0 0
Condition0%0/3
$row_ref and ref $row_ref eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$row_ref and ref $row_ref eq "ARRAY"
$row_ref ref $row_ref eq "ARRAY"
Truth table0%0/3
A and B
A= $row_refB= ref $row_ref eq "ARRAY"
ABresult
0X0
100
111
238 0 push(@rows,@{$row_ref});
239 } else {
240 0 $self->logger()->log( message => 'Ignoring non-ARRAY result from backend '.$backend.' for action '.$action, level => 'warning', );
241 }
242 }
243  
244 0 return \@rows;
245 }
246  
247 0 ▶sub _backend_filter_match {
Subroutine _backend_filter_match: not called CC=12 SCAR=50.5
248 0 my $self = shift;
249 0 my $backend = shift;
250 0 my $arg_ref = shift;
251  
252 # No backend given? Default to "MATCH"
253 0 ▶ if(!$backend) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($backend) 0 0 0
254 0 return 1;
255 }
256  
257 # No arg_ref given or no hash? Default to "MATCH"
258 0 ▶ if(!$arg_ref || ref($arg_ref) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $arg_ref or ref $arg_ref ne "HASH") 0 0 0
Condition0%0/3
not $arg_ref or ref $arg_ref ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $arg_ref or ref $arg_ref ne "HASH"
not $arg_ref ref $arg_ref ne "HASH"
Truth table0%0/3
not(A) or B
A= not $arg_refB= ref $arg_ref ne "HASH"
ABresult
1X1
011
000
259 0 return 1;
260 }
261  
262 # No backend filter specified in arg_ref? Default to "MATCH"
263 0 ▶ if(! exists $arg_ref->{'_backend_filter'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $arg_ref->{"_backend_filter"}) 0 0 0
264 0 return 1;
265 }
266  
267 0 my $filter = $arg_ref->{'_backend_filter'};
268 0 ▶ if(ref($filter) eq 'Regexp') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $filter eq 'Regexp') { } 0 0 0
elsif (ref $filter eq 'ARRAY') { } 0 0 0
elsif (not ref $filter) { } 0 0 0
269 # Regexp: Try to match against backend
270 0 ▶ if($backend =~ m/$filter/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend =~ /$filter/) { } 0 0 0
271 0 return 1;
272 } else {
273 0 return;
274 }
275 } elsif(ref($filter) eq 'ARRAY') {
276 # Array: Try to match backend against list
277 0 foreach my $f (sort @{$filter}) {
278 0 ▶ if($backend eq $f) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq $f) 0 0 0
279 0 return 1;
280 }
281 }
282 0 return;
283 } elsif(!ref($filter)) {
284 # Scalar/String: Try to match
285 0 ▶ if($backend eq $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend eq $filter) { } 0 0 0
286 0 return 1;
287 } else {
288 0 return;
289 }
290 } else {
291 # Unknown filter condition: Default to "NO MATCH"
292 # unkown filter type
293 0 return;
294 }
295 }
296  
297 1 __PACKAGE__->meta->make_immutable;
298  
299 1 1; # End of Monitoring::Reporter
300  
301 __END__
« lib/Monitoring/Reporter/Backend/ZabbixDBI.pm ↑ Summary lib/Monitoring/Reporter/Web.pm »