Simple/Utils.pm

filter: Statementstmt 80.0% 88 / 110 Branchbran 48.0% 24 / 50 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 7 totaltotal 68.6% 127 / 185 CC 30 SCAR 37.4
CC
30
Cov
70%
exec_or_exit
78.6
process_exit
4.6
report_and_exit
3.0
CRAP 42.1
« Simple/UserAgent.pm ↑ Summary Simple/Notifier.pm »
1 #-----------------------------------------------------------------
2 # Monitor::Simple::Utils
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::Utils
8 #-----------------------------------------------------------------
9  
10 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 package Monitor::Simple::Utils;
13  
14 40 ▶use Getopt::Long 2.38 qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use Monitor::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use IO::CaptureOutput qw(capture_exec);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 40 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 40 our $VERSION = '0.2.8'; # VERSION
20  
21 #-----------------------------------------------------------------
22 # Read plugin's command-line arguments @args.
23 #
24 # It returns configuration file name (may be undef) and service ID (if
25 # not found in @args, use $default_service_id). It uses logging
26 # options (if any found) to set the logging system.
27 # -----------------------------------------------------------------
28 6 ▶sub parse_plugin_args {
Subroutine parse_plugin_args: called CC=2 SCAR=6.9
Pod undocumented
29 6 my ($self, $default_service_id, @args) = @_;
30  
31 6 my ($opt_service, $opt_config);
32 6 my ($opt_logfile, $opt_loglevel, $opt_logformat);
33 6 Getopt::Long::Configure ('no_ignore_case');
34 6 GetOptionsFromArray (\@args,
35 'id|service=s' => \$opt_service,
36 'cfg|config=s' => \$opt_config,
37  
38 # logging
39 'logfile=s' => \$opt_logfile,
40 'loglevel=s' => \$opt_loglevel,
41 'logformat=s' => \$opt_logformat,
42 );
43 6 ▶ $opt_service = $default_service_id unless $opt_service;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt_service 2 4 6
44 6 Monitor::Simple::Log->log_init ({ level => $opt_loglevel,
45 file => $opt_logfile,
46 layout => $opt_logformat });
47  
48 6 return ($opt_config, $opt_service);
49 }
50  
51 #-----------------------------------------------------------------
52 # Report it and exit accordingly. $config is not used (at least now)
53 # and can be undef.
54 # -----------------------------------------------------------------
55 14 ▶sub report_and_exit {
Subroutine report_and_exit: called CC=3 SCAR=11.0
Pod undocumented
56 14 my ($self, $service_id, $config, $return_code, $return_msg) = @_;
57  
58 # $return_msg should not be ever empty
59 14 ▶ $return_msg = 'Message empty or lost...' unless $return_msg;
Branch: 1 branch
BranchTrueFalseTotal
unless $return_msg 0 14 14
60  
61 14 ▶ $return_msg .= "\n" unless $return_msg =~ m{\n$};
Branch: 1 branch
BranchTrueFalseTotal
unless $return_msg =~ /\n$/ 13 1 14
62 14 INFO ("Done: $service_id $return_code $return_msg");
63 14 print STDOUT $return_msg;
64  
65 14 exit ($return_code);
66 }
67  
68 #--------------------------------------------------------------------
69 # For the given plugin $command, it processes the real $exit_code and
70 # returns it as a processsed return code and accompanying message -
71 # which may be just a copy of the given $stdout.
72 # -------------------------------------------------------------------
73 18 ▶sub process_exit {
Subroutine process_exit: called CC=4 SCAR=15.2
Pod undocumented
74 18 my ($self, $command, $exit_code, $stdout) = @_;
75  
76 18 ▶ if ($exit_code == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exit_code == -1) { } 1 17 18
elsif ($exit_code & 127) { } 0 17 17
77 # external command was not executed, at all
78 1 return (-1, "Failed to execute plugin '$command'");
79  
80 } elsif ($exit_code & 127) {
81 # external command was killed by a signal
82 0 ▶ return (2, sprintf ("Plugin '$command' died with signal %d, %s coredump",
Branch: 1 branch
BranchTrueFalseTotal
$exit_code & 128 ? : 0 0 0
83 ($exit_code & 127), ($exit_code & 128) ? 'with' : 'without'));
84  
85 } else {
86 # external command finished (good or bad, it depends on its exit code)
87 17 chomp $stdout;
88 17 return ( ($exit_code >> 8), $stdout);
89 }
90 }
91  
92 #-----------------------------------------------------------------
93 # Read notifier's command-line arguments $args (an arrayref - so the
94 # recognized arguments can be removed from the provided array).
95 #
96 # It returns a service ID, a file name with the notification message
97 # and a reference to an array with all email addresses (may be empty
98 # for some notifiers).
99 #
100 # It uses logging options (if any found) to set the logging system.
101 # -----------------------------------------------------------------
102 2 ▶sub parse_notifier_args {
Subroutine parse_notifier_args: called CC=1 SCAR=0.0
Pod undocumented
103 2 my ($self, $args) = @_;
104  
105 2 my ($opt_service, $opt_msgfile, @opt_emails);
106 2 my ($opt_logfile, $opt_loglevel, $opt_logformat);
107 2 Getopt::Long::Configure ('no_ignore_case', 'pass_through');
108 2 GetOptionsFromArray ($args,
109 'service=s' => \$opt_service,
110 'msg=s' => \$opt_msgfile,
111 'emails=s' => \@opt_emails,
112  
113 # logging
114 'logfile=s' => \$opt_logfile,
115 'loglevel=s' => \$opt_loglevel,
116 'logformat=s' => \$opt_logformat,
117 );
118 2 Monitor::Simple::Log->log_init ({ level => $opt_loglevel,
119 file => $opt_logfile,
120 layout => $opt_logformat });
121  
122 2 @opt_emails = split (m{\s*,\s*}, join (',', @opt_emails));
123  
124 2 return ($opt_service, $opt_msgfile, \@opt_emails);
125 }
126  
127 # -------------------------------------------------------------------
128 # It executes an external program with the given arguments and
129 # (optionally) checks its STDOUT and/or STDERR for the given
130 # content. If everything okay it just returns. Otherwise, it exits
131 # with the nagios-compliant reporting (see more about it in
132 # report_and_exit()).
133 #
134 # It reads (and understands) the configuration (an example):
135 #
136 # <plugin command="check-prg.pl">
137 # <prg-test>
138 # <program>mrsclient</program>
139 # <args>
140 # <arg>-H</arg> <arg>mrs.cbrc.kaust.edu.sa</arg>
141 # <arg>-l</arg>
142 # </args>
143 # <stdout>
144 # <contains>enzyme</contains>
145 # <contains>gene</contains>
146 # <contains>sprot</contains>
147 # </stdout>
148 # </prg-test>
149 # <prg-test>
150 # <program>mrsclient</program>
151 # <args>
152 # <arg>-H</arg> <arg>mrs.cbrc.kaust.edu.sa</arg>
153 # <arg>-d</arg> <arg>sprot</arg>
154 # <arg>-q</arg> <arg>canine</arg>
155 # <arg>-c</arg>
156 # <arg>-n</arg>
157 # <arg></arg>
158 # </args>
159 # <stdout>
160 # <not-empty/>
161 # <is-integer/>
162 # </stdout>
163 # </prg-test>
164 # </plugin>
165 # -------------------------------------------------------------------
166 2 ▶sub exec_or_exit {
Subroutine exec_or_exit: called CC=23 SCAR=43.6
Pod undocumented
167 2 my ($self, $service_id, $full_config) = @_;
168 2 my $config = $self->service_config_or_exit ($service_id, $full_config);
169  
170 # warn (and exit) if the test cannot be executed
171 2 ▶ unless (exists $config->{plugin}->{'prg-test'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config->{"plugin"}{"prg-test"}) 0 2 2
172 # a warning: test ignored
173 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
174 Monitor::Simple::RETURN_WARNING,
175 'PRG test(s) ignored');
176 }
177 2 foreach my $prgtest (@{ $config->{plugin}->{'prg-test'} }) {
178 2 ▶ unless ($prgtest->{program}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prgtest->{"program"}) 0 2 2
179 # a warning: test cannot be executed
180 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
181 Monitor::Simple::RETURN_WARNING,
182 "PRG test cannot be executed (missing 'program' parameter)");
183 }
184 # building a command-line
185 2 my @command = ($prgtest->{program});
186 2 ▶ if (defined $prgtest->{args}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prgtest->{"args"}) 2 0 2
187 2 foreach my $arg (@{ $prgtest->{args} }) {
188 2 ▶ push (@command, $arg) if defined $arg; # ignoring empty arguments
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 2 0 2
189 }
190 }
191  
192 # prepare for timeout (zero means no timeout)
193 2 ▶ my $timeout = $prgtest->{timeout} || 0;
Condition50%1/2
$prgtest->{'timeout'} || 0
l!l
02
MC/DC0%0/1
$prgtest->{'timeout'} || 0
$prgtest->{'timeout'}
Truth table50%1/2
A
A= $prgtest->{'timeout'}
Aresult
11
00
194 2 ▶ if (!$self->is_integer ($timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_integer($timeout)) 0 2 2
195 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
196 Monitor::Simple::RETURN_WARNING,
197 'PRG test cannot be executed (a non-integer value in timeout)');
198 }
199 2 ▶ $timeout = 0 unless $timeout > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout > 0 2 0 2
200  
201 # call the external program
202 2 DEBUG ("Executing: " . join (' ', @command));
203 2 my ($stdout, $stderr, $success, $exit_code);
204  
205 2 ▶ if ($timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) { } 0 2 2
206 0 eval {
207 0 ▶ local $SIG{ALRM} = sub { die "alarm\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 alarm $timeout;
209 0 ($stdout, $stderr, $success, $exit_code) = capture_exec (@command);
210 0 alarm 0;
211 };
212 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
213 0 ▶ if ($@ eq "alarm\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ eq "alarm\n") { } 0 0 0
214 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
215 Monitor::Simple::RETURN_WARNING,
216 "Timeout after waiting for $timeout seconds");
217 } else {
218 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
219 Monitor::Simple::RETURN_UNKNOWN,
220 $@);
221 }
222 }
223  
224 } else {
225 2 ($stdout, $stderr, $success, $exit_code) = capture_exec (@command);
226 }
227  
228 2 ▶ if ($stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr) 1 1 2
229 1 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
230 Monitor::Simple::RETURN_UNKNOWN,
231 $stderr);
232 }
233 1 my ($code, $msg) = Monitor::Simple::Utils->process_exit ($command[0], $exit_code, $stdout);
234 1 ▶ unless ($code == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code == 0) 0 1 1
235 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
236 Monitor::Simple::RETURN_CRITICAL,
237 $msg);
238 }
239  
240 # check the response
241 1 ▶ if (exists $prgtest->{stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prgtest->{"stdout"}) 1 0 1
242  
243 # check for emptyness (have we got anything?)
244 1 ▶ if (exists $prgtest->{stdout}->{'not-empty'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prgtest->{"stdout"}{"not-empty"}) 0 1 1
245 0 ▶ unless ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg) 0 0 0
246 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
247 Monitor::Simple::RETURN_WARNING,
248 "Returned content is empty");
249 }
250 }
251  
252 # have we got an expected content?
253 1 my $expected = $prgtest->{stdout}->{contains};
254 1 ▶ if (defined $expected) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected) 1 0 1
255 1 foreach my $content (@$expected) {
256 1 my $quoted = quotemeta ($content);
257 1 ▶ if ($msg !~ m{$quoted}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg =~ /$quoted/) 0 1 1
258 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
259 Monitor::Simple::RETURN_WARNING,
260 "Returned content does not contain: $content");
261 }
262 }
263 }
264  
265 # check for numeric response
266 1 ▶ if (exists $prgtest->{stdout}->{'is-integer'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prgtest->{"stdout"}{"is-integer"}) 0 1 1
267 0 ▶ if ($msg !~ m{\s*\d+\s*}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg =~ /\s*\d+\s*/) 0 0 0
268 0 $msg =~ s{^\s*|\s*$}{}g;
269 0 Monitor::Simple::Utils->report_and_exit ($service_id, $full_config,
270 Monitor::Simple::RETURN_WARNING,
271 "Returned content '$msg' is not an integer");
272 }
273 }
274  
275 }
276 }
277  
278 # everything is okay
279 1 return;
280 }
281  
282 40 ▶use constant PAT_INT => "[-+]?_*[0-9][0-9_]*"; # taken from Getopt::Long
Subroutine BEGIN: called CC=1 SCAR=0.0
283 9 ▶sub is_integer {
Subroutine is_integer: called CC=1 SCAR=0.0
Pod undocumented
284 9 my ($self, $value) = @_;
285 9 my $valid = PAT_INT;
286 9 return $value =~ /^$valid$/si;
287 }
288  
289 #-----------------------------------------------------------------
290 # Extract and return the configuration for the given service
291 # $service_id from $full_config. Exit with a warning exit code if such
292 # configuration cannot be found.
293 # -----------------------------------------------------------------
294 2 ▶sub service_config_or_exit {
Subroutine service_config_or_exit: called CC=2 SCAR=7.4
Pod undocumented
295 2 my ($self, $service_id, $full_config) = @_;
296 2 my $config = Monitor::Simple::Config->extract_service_config ($service_id, $full_config);
297  
298 # warn (and exit) if the service is unknown to the configuration
299 2 ▶ unless ($config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config) 0 2 2
300 # a warning: test ignored
301 0 $self->report_and_exit ($service_id, $full_config,
302 Monitor::Simple::RETURN_WARNING,
303 "Service '$service_id' unknown. Test(s) ignored.");
304 }
305 2 return $config;
306 }
307  
308  
309 40 1;
310  
311  
312 =pod
313  
314 =head1 NAME
315  
316 Monitor::Simple::Utils - See documentation in Monitor::Simple
317  
318 =head1 VERSION
319  
320 version 0.2.8
321  
322 =head1 AUTHOR
323  
324 Martin Senger <martin.senger@gmail.com>
325  
326 =head1 COPYRIGHT AND LICENSE
327  
328 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.
329  
330 This is free software; you can redistribute it and/or modify it under
331 the same terms as the Perl 5 programming language system itself.
332  
333 =cut
334  
335  
336 __END__
« Simple/UserAgent.pm ↑ Summary Simple/Notifier.pm »