Simple/Notifier.pm

filter: Statementstmt 77.4% 86 / 111 Branchbran 60.3% 35 / 58 Conditioncond 78.2% 18 / 23 MC/DCmcdc 73.3% 11 / 15 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 6 totaltotal 72.1% 163 / 226 CC 47 SCAR 35.7
CC
47
Cov
72%
notify
130.0
matching_code
15.0
get_relevant_notifiers
10.0
CRAP 35.7
« Simple/Utils.pm ↑ Summary Simple.pm »
1 #-----------------------------------------------------------------
2 # Monitor::Simple::Notifier
3 # Author: Martin Senger <martin.senger@gmail.com>
4 # For copyright and disclaimer see below.
5 #
6 # ABSTRACT: See documentation in Monitor::Simple
7 # PODNAME: Monitor::Simple::Notifier
8 #-----------------------------------------------------------------
9  
10 package Monitor::Simple::Notifier;
11 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Monitor::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use IO::CaptureOutput qw(capture_exec);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 40 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 40 our $VERSION = '0.2.8'; # VERSION
20  
21 40 my $Codes = {
22 Monitor::Simple::NOTIFY_OK => 1,
23 Monitor::Simple::NOTIFY_WARNING => 1,
24 Monitor::Simple::NOTIFY_CRITICAL => 1,
25 Monitor::Simple::NOTIFY_UNKNOWN => 1,
26 Monitor::Simple::NOTIFY_ALL => 1,
27 Monitor::Simple::NOTIFY_ERRORS => 1,
28 Monitor::Simple::NOTIFY_NONE => 1,
29 };
30  
31  
32 #-----------------------------------------------------------------
33 # Recognized arguments:
34 # config => <config>
35 # cfgfile => <config-file>
36 #-----------------------------------------------------------------
37 78 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod undocumented
38 78 my ($class, %args) = @_;
39  
40 # create an object and fill it from $args
41 78 ▶ my $self = bless {}, ref ($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0780
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
42 78 foreach my $key (keys %args) {
43 154 $self->{$key} = $args {$key};
44 }
45  
46 # done
47 78 return $self;
48 }
49  
50 #------------------------------------------------------------------
51 # Given a $result of a service check, make all expected notifications
52 # (as defined in the $config given in a Notifier constructor). The
53 # $result is a hashref with this content:
54 #
55 # { service => $service_id,
56 # code => $code,
57 # msg => $msg }
58 #
59 # -----------------------------------------------------------------
60 14 ▶sub notify {
Subroutine notify: called CC=13 SCAR=48.7
Pod undocumented
61 14 my ($self, $result) = @_;
62  
63 # some defaults
64 $result->{code} = Monitor::Simple::NOTIFY_ERRORS
65 14 ▶ unless exists $result->{code};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $result->{"code"} 0 14 14
66  
67 # find all relevant notifiers (both general and service specific)
68 14 my @all_relevant_notifiers = $self->get_relevant_notifiers ($result);
69  
70 # notify
71 14 my $msg_files = {}; # keys are formats, values are names of temporary files
72 14 foreach my $notifier (@all_relevant_notifiers) {
73 my $notifier_str =
74 join (', ',
75 0 map { "$_ => " .
76 0 ▶ (ref ($notifier->{$_}) ? join (',', @{ $notifier->{$_} }) : $notifier->{$_})
Branch: 1 branch
BranchTrueFalseTotal
ref $notifier->{$_} ? : 0 0 0
77 } keys %$notifier);
78 0 DEBUG ("Using notifier: $notifier_str");
79  
80 # create (or re-use) a temporary file with the message
81 # (content depends on the format in the notifier)
82 0 ▶ my $format = $notifier->{format} || 'human';
Condition0%0/2
$notifier->{'format'} || 'human'
l!l
00
MC/DC0%0/1
$notifier->{'format'} || 'human'
$notifier->{'format'}
Truth table0%0/2
A
A= $notifier->{'format'}
Aresult
11
00
83 0 my $msgfile = $msg_files->{$format};
84 0 ▶ unless ($msgfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msgfile) 0 0 0
85 0 $msgfile = File::Temp->new();
86 my $outputter = Monitor::Simple::Output->new (outfile => $msgfile,
87 'format' => $format,
88 0 config => $self->{config});
89 0 $outputter->header();
90 0 $outputter->out ($result->{service}, $result->{code}, $result->{msg});
91 0 $outputter->footer();
92 0 close $msgfile;
93 0 $msg_files->{$format} = $msgfile; # remember this file for other notifiers
94 }
95  
96 # execute an external notifier...
97 0 my $command = $notifier->{command};
98 0 ▶ unless (File::Spec->file_name_is_absolute ($command)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($command)) 0 0 0
99 0 my $notifiers_dir = $self->{config}->{general}->{'notifiers-dir'};
100 0 ▶ if ($notifiers_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notifiers_dir) 0 0 0
101 0 $command = File::Spec->catfile ($notifiers_dir, $command);
102 }
103 }
104 0 my @command = ($command, $self->create_notifier_args ($notifier,
105 $msgfile));
106 0 DEBUG ("Calling notifier: " . join (' ', @command));
107 0 my ($stdout, $stderr, $success, $exit_code) = capture_exec (@command);
108 0 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 0 0
109 0 ▶ DEBUG ("Notification for '$notifier_str' successful." .
Branch: 2 branches
BranchTrueFalseTotal
$stderr ? : 0 0 0
$stdout ? : 0 0 0
110 ($stderr ? " STDERR: $stderr" : '') .
111 ($stdout ? " STDOUT: $stdout" : ''));
112 } else {
113 0 ▶ ERROR ("Notification for '$notifier_str' failed: EXIT: $exit_code" .
Branch: 2 branches
BranchTrueFalseTotal
$stderr ? : 0 0 0
$stdout ? : 0 0 0
114 ($stderr ? ", STDERR: $stderr" : '') .
115 ($stdout ? ", STDOUT: $stdout" : ''));
116 }
117 }
118 }
119  
120 #-----------------------------------------------------------------
121 # Find and return an array with all relevant notifiers (both general
122 # and services specific) for the given $result which is a hashref with
123 # this content:
124 #
125 # { service => $service_id,
126 # code => $code,
127 # msg => $msg }
128 #
129 # It also copies service ID from the $result into each returned
130 # notifier.
131 # -----------------------------------------------------------------
132 17 ▶sub get_relevant_notifiers {
Subroutine get_relevant_notifiers: called CC=10 SCAR=23.1
Pod undocumented
133 17 my ($self, $result) = @_;
134  
135 17 my @all_relevant_notifiers = ();
136 17 ▶ if (exists $self->{config}->{general}->{notifier}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"config"}{"general"}{"notifier"}) 3 14 17
137 3 foreach my $notifier (@{ $self->{config}->{general}->{notifier} }) {
138 push (@all_relevant_notifiers, $notifier)
139 3 ▶ if $self->matching_code ($result->{code}, $notifier->{on});
Branch: 1 branch
BranchTrueFalseTotal
if $self->matching_code($result->{"code"}, $notifier->{"on"}) 3 0 3
140 }
141 }
142 17 foreach my $service (@{ $self->{config}->{services} }) {
143 93 ▶ next unless $result->{service} eq $service->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $result->{"service"} eq $service->{"id"} 76 17 93
144 @all_relevant_notifiers = ()
145 17 ▶ if exists $service->{'ignore-general-notifiers'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $service->{"ignore-general-notifiers"} 2 15 17
146 17 foreach my $notifier (@{ $service->{notifier} }) {
147 push (@all_relevant_notifiers, $notifier)
148 3 ▶ if $self->matching_code ($result->{code}, $notifier->{on});
Branch: 1 branch
BranchTrueFalseTotal
if $self->matching_code($result->{"code"}, $notifier->{"on"}) 3 0 3
149 }
150 }
151  
152 17 foreach my $notifier (@all_relevant_notifiers) {
153 4 $notifier->{service} = $result->{service};
154 }
155  
156 17 return @all_relevant_notifiers;
157 }
158  
159 #-----------------------------------------------------------------
160 #
161 #-----------------------------------------------------------------
162 38 ▶sub matching_code {
Subroutine matching_code: called CC=15 SCAR=27.1
Pod undocumented
163 38 my ($self, $code_from_result, $code_from_config) = @_;
164  
165 38 foreach my $code (split (m{\s*,\s*}, $code_from_config)) {
166 41 ▶ next unless exists $Codes->{$code};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Codes->{$code} 0 41 41
167 41 ▶ next if $code eq Monitor::Simple::NOTIFY_NONE;
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "none" 4 37 41
168 37 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "all" 9 28 37
169 if $code eq Monitor::Simple::NOTIFY_ALL;
170 28 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $code_from_result == 0 and $code eq "ok" 1 27 28
Condition100%3/3
$code_from_result == 0 and $code eq "ok"
!ll&&!rl&&r
2161
MC/DC100%2/2
$code_from_result == 0 and $code eq "ok"
$code_from_result == 0 $code eq "ok"
Truth table100%3/3
A and B
A= $code_from_result == 0B= $code eq "ok"
ABresult
0X0
100
111
171 if $code_from_result == Monitor::Simple::RETURN_OK and $code eq Monitor::Simple::NOTIFY_OK;
172 27 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $code_from_result != 0 and $code eq "err" 3 24 27
Condition100%3/3
$code_from_result != 0 and $code eq "err"
!ll&&!rl&&r
6183
MC/DC100%2/2
$code_from_result != 0 and $code eq "err"
$code_from_result != 0 $code eq "err"
Truth table100%3/3
A and B
A= $code_from_result != 0B= $code eq "err"
ABresult
0X0
100
111
173 if $code_from_result != Monitor::Simple::RETURN_OK and $code eq Monitor::Simple::NOTIFY_ERRORS;
174 24 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $code_from_result == 1 and $code eq "w" 2 22 24
Condition100%3/3
$code_from_result == 1 and $code eq "w"
!ll&&!rl&&r
1752
MC/DC100%2/2
$code_from_result == 1 and $code eq "w"
$code_from_result == 1 $code eq "w"
Truth table100%3/3
A and B
A= $code_from_result == 1B= $code eq "w"
ABresult
0X0
100
111
175 if $code_from_result == Monitor::Simple::RETURN_WARNING and $code eq Monitor::Simple::NOTIFY_WARNING;
176 22 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $code_from_result == 3 and $code eq "u" 2 20 22
Condition100%3/3
$code_from_result == 3 and $code eq "u"
!ll&&!rl&&r
1642
MC/DC100%2/2
$code_from_result == 3 and $code eq "u"
$code_from_result == 3 $code eq "u"
Truth table100%3/3
A and B
A= $code_from_result == 3B= $code eq "u"
ABresult
0X0
100
111
177 if $code_from_result == Monitor::Simple::RETURN_UNKNOWN and $code eq Monitor::Simple::NOTIFY_UNKNOWN;
178 20 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $code_from_result == 2 and $code eq "c" 2 18 20
Condition100%3/3
$code_from_result == 2 and $code eq "c"
!ll&&!rl&&r
1532
MC/DC100%2/2
$code_from_result == 2 and $code eq "c"
$code_from_result == 2 $code eq "c"
Truth table100%3/3
A and B
A= $code_from_result == 2B= $code eq "c"
ABresult
0X0
100
111
179 if $code_from_result == Monitor::Simple::RETURN_CRITICAL and $code eq Monitor::Simple::NOTIFY_CRITICAL;
180 }
181 19 return 0;
182 }
183  
184 #-----------------------------------------------------------------
185 # Extract proper command-line arguments for the given $notifier.
186 # Similar to Monitor::Simple::Config::create_plugin_args().
187 # -----------------------------------------------------------------
188 2 ▶sub create_notifier_args {
Subroutine create_notifier_args: called CC=3 SCAR=11.0
Pod undocumented
189 2 my ($self, $notifier, $msgfile) = @_;
190  
191 2 my @args = ();
192 2 ▶ if (exists $notifier->{args}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $notifier->{"args"}) 1 1 2
193 # args exists: use them
194 1 push (@args, @{ $notifier->{args} });
195 }
196  
197 # ...add email addresses (if any)
198 2 my $emails = $self->extract_emails ($notifier);
199 2 ▶ push (@args, '-emails', join (',', @$emails))
Branch: 1 branch
BranchTrueFalseTotal
if @$emails > 0 1 1 2
200 if @$emails > 0;
201  
202 # ...and the default arguments
203 push (@args, ( # '-cfg', $self->{cfgfile},
204 '-service', $notifier->{service},
205 # '-notifier', $notifier->{command},
206 2 '-msg', $msgfile,
207 Monitor::Simple::Log->logging_args()));
208 2 return @args;
209 }
210  
211 # -----------------------------------------------------------------
212 # Extract all email addresses from the given notifier (and from the
213 # current $config). Return them as an arrayref (which may be empty but
214 # not undef). Make sure that there are no duplicates returned.
215 # -----------------------------------------------------------------
216 4 ▶sub extract_emails {
Subroutine extract_emails: called CC=8 SCAR=20.8
Pod undocumented
217 4 my ($self, $notifier) = @_;
218 4 my @results = ();
219 4 ▶ if (my $email = $notifier->{email}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $email = $notifier->{"email"}) 3 1 4
220 3 push (@results, split (m{\s*,\s*}, $email));
221 }
222 4 ▶ if (my $group = $notifier->{'email-group'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $notifier->{"email-group"}) 2 2 4
223 2 my @email_groups_in_notifier = split (m{\s*,\s*}, $group);
224 2 ▶ if (my $email_groups_in_config = $self->{config}->{general}->{'email-group'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $email_groups_in_config = $self->{"config"}{"general"}{"email-group"}) 2 0 2
225 2 foreach my $eg (@email_groups_in_notifier) {
226 4 foreach my $eg_in_config (@$email_groups_in_config) {
227 6 ▶ if ($eg_in_config->{id} eq $eg and $eg_in_config->{email}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eg_in_config->{"id"} eq $eg and $eg_in_config->{"email"}) 4 2 6
Condition66%2/3
$eg_in_config->{"id"} eq $eg and $eg_in_config->{"email"}
!ll&&!rl&&r
204
MC/DC50%1/2
$eg_in_config->{"id"} eq $eg and $eg_in_config->{"email"}
$eg_in_config->{"id"} eq $eg $eg_in_config->{"email"}
Truth table66%2/3
A and B
A= $eg_in_config->{"id"} eq $egB= $eg_in_config->{"email"}
ABresult
0X0
100
111
228 4 push (@results, @{ $eg_in_config->{email} });
229 4 last;
230 }
231 }
232 }
233 }
234 }
235  
236 # return \@results;
237 4 return [ keys %{{ map { $_ => 1 } @results }} ]; # removing duplicates
238 }
239  
240  
241 40 1;
242  
243  
244 =pod
245  
246 =head1 NAME
247  
248 Monitor::Simple::Notifier - See documentation in Monitor::Simple
249  
250 =head1 VERSION
251  
252 version 0.2.8
253  
254 =head1 AUTHOR
255  
256 Martin Senger <martin.senger@gmail.com>
257  
258 =head1 COPYRIGHT AND LICENSE
259  
260 This software is copyright (c) 2013 by Martin Senger, CBRC-KAUST (Computational Biology Research Center - King Abdullah University of Science and Technology) All Rights Reserved.
261  
262 This is free software; you can redistribute it and/or modify it under
263 the same terms as the Perl 5 programming language system itself.
264  
265 =cut
266  
267  
268 __END__
« Simple/Utils.pm ↑ Summary Simple.pm »