Simple/Output.pm

filter: Statementstmt 70.3% 76 / 108 Branchbran 40.7% 22 / 54 Conditioncond 31.8% 7 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 12 totaltotal 52.1% 119 / 228 CC 37 SCAR 22.9
CC
37
Cov
57%
header
27.5
out
13.5
html_line
12.0
CRAP 9.9
« Simple.pm ↑ Summary Simple/Config.pm »
1 #-----------------------------------------------------------------
2 # Monitor::Simple::Output
3 # Author: Martin Senger <martin.senger@gmail.com>
4 # For copyright and disclaimer see Monitor::Simple.
5 #
6 # ABSTRACT: See documentation in Monitor::Simple
7 # PODNAME: Monitor::Simple::Output
8 #-----------------------------------------------------------------
9 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package Monitor::Simple::Output;
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  
16 40 our $VERSION = '0.2.8'; # VERSION
17  
18 40 my @Headers = ('DATE', 'SERVICE', 'STATUS', 'MESSAGE');
19  
20 40 my $Formats = { tsv => 'TAB-separated (good for machines)',
21 human => 'Easier readable by humans',
22 html => 'Formatted as an HTML document',
23 };
24  
25 #-----------------------------------------------------------------
26 # Recognized arguments: Default value:
27 # outfile => <file> none
28 # onlyerr => boolean false
29 # format => tsv | human | html human
30 # cssurl => <url> content taken from 'monitor-default.css'
31 # config => <config>
32 #-----------------------------------------------------------------
33 76 ▶sub new {
Subroutine new: called CC=11 SCAR=24.5
Pod undocumented
34 76 my ($class, %args) = @_;
35  
36 # create an object and fill it from $args
37 76 ▶ my $self = bless {}, ref ($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0760
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
38 76 foreach my $key (keys %args) {
39 229 $self->{$key} = $args {$key};
40 }
41 LOGDIE ("outputter: Missing argument 'config'. Cannot do anything.\n")
42 76 ▶ unless $self->{config};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"} 0 76 76
43  
44 # assign some default values
45 76 ▶ $self->{format} = 'human' unless $self->{format};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"format"} 0 76 76
46 76 ▶ $self->{format} = 'human' unless exists $Formats->{ $self->{format} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$Formats{$self->{"format"}} 1 75 76
47  
48 # compute the longest service name (needed only for 'human' format)
49 76 ▶ if ($self->{format} eq 'human') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"format"} eq "human") 1 75 76
50 1 my $longest_name_len = length ($Headers[1]);
51 map {
52 3 ▶ my $len = length ($_->{name} || $_->{id});
Condition33%1/3
$_->{'name'} || $_->{'id'}
l!l&&r!l&&!r
300
MC/DC0%0/2
$_->{'name'} || $_->{'id'}
$_->{'name'} $_->{'id'}
Truth table33%1/3
A || B
A= $_->{'name'}B= $_->{'id'}
ABresult
1X1
011
000
53 1 ▶ $longest_name_len = $len if $len > $longest_name_len } @{ $self->{config}->{services} };
Branch: 1 branch
BranchTrueFalseTotal
if $len > $longest_name_len 2 1 3
54 1 $self->{report_format} = "%-30s %-${longest_name_len}s %6s %s\n";
55 }
56  
57 # prepare output
58 76 ▶ if ($self->{outfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'outfile'}) { } 75 1 76
59 open ($self->{fhout}, '>', $self->{outfile})
60 75 ▶ or LOGDIE ("Cannot open file '$self->{outfile}' for writing: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"fhout"}, ">", $self->{"outfile"} or LOGDIE(qq[Cannot open file '$self->{"outfile"}' for writing: $!\n]) 75 0 75
61 75 close ($self->{fhout});
62 } else {
63 1 $self->{fhout} = *STDOUT;
64 1 my $oldfh = select ($self->{fhout}); $| = 1; select ($oldfh); # turn autoflush on
65 }
66  
67 # done
68 76 return $self;
69 }
70  
71 #-----------------------------------------------------------------
72 # Return a hashref with all available formats: keys are format
73 # identifiers and values their descriptions.
74 # -----------------------------------------------------------------
75 1 ▶sub list_formats {
Subroutine list_formats: called CC=1 SCAR=0.0
Pod undocumented
76 1 my $self = shift;
77 1 return $Formats;
78 }
79  
80 #-----------------------------------------------------------------
81 # Join and return @fields into a string, depending on
82 # $self->{format}. $service_config may be useful for some formatting.
83 # -----------------------------------------------------------------
84 14 ▶sub create_report {
Subroutine create_report: called CC=3 SCAR=15.1
Pod undocumented
85 14 my ($self, $service_config, @fields) = @_;
86 14 ▶ if ($self->{format} eq 'tsv') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'format'} eq 'tsv') { } 14 0 14
elsif ($self->{'format'} eq 'html') { } 0 0 0
87 14 return join ("\t", @fields) . "\n";
88  
89 } elsif ($self->{format} eq 'html') {
90 0 return $self->html_line ($service_config, @fields);
91  
92 } else {
93 0 return sprintf ($self->{report_format}, @fields);
94 }
95 }
96  
97 0 ▶sub html_header {
Subroutine html_header: not called CC=2 SCAR=17.9
Pod undocumented
98 0 my ($self, @fields) = @_;
99 0 my ($link, $style);
100 0 ▶ if ($self->{cssurl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cssurl'}) { } 0 0 0
101 0 $link = qq{<link href="$self->{cssurl}" rel="stylesheet" type="text/css">};
102 0 $style = '';
103 } else {
104 0 $link = '';
105 0 $style = <<'END_OF_STYLE';
106 <style>
107 .mon-line, .mon-header-line {
108 vertical-align:text-top;
109 }
110 .mon-header-line {
111 font-weight: bold;
112 background: grey;
113 }
114 .mon-line-ok {
115 background: lightgreen;
116 }
117 .mon-line-warning {
118 background: lightblue;
119 }
120 .mon-line-critical, .mon-line-unknown, .mon-line-fatal {
121 background: red;
122 color: yellow;
123 }
124 .mon-date {
125 white-space: nowrap;
126 }
127 .mon-date, .mon-service, .mon-code, .mon-msg, .mon-header-date, .mon-header-service, .mon-header-code, .mon-header-msg {
128 padding: 3px;
129 }
130 .mon-caption {
131 color: brown;
132 font-style: italid;
133 font-size: 80%;
134 }
135 </style>
136 END_OF_STYLE
137 }
138  
139 0 my $current_date = localtime;
140 0 return <<"END_OF_HEADER";
141 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
142 "http://www.w3.org/TR/html4/strict.dtd">
143 <html>
144 <head>
145 $link
146 </head>
147 <body>
148 $style
149 <table border="0" class="mon-table">
150 <caption class="mon-caption">Created: $current_date</caption>
151 <tr class="mon-header-line">
152 <td class="mon-header-date">$fields[0]</td>
153 <td class="mon-header-service">$fields[1]</td>
154 <td class="mon-header-code">$fields[2]</td>
155 <td class="mon-header-msg">$fields[3]</td>
156 </tr>
157 END_OF_HEADER
158 }
159  
160 40 my $Status = {
161 Monitor::Simple::RETURN_OK => 'OK',
162 Monitor::Simple::RETURN_WARNING => 'WARNING',
163 Monitor::Simple::RETURN_CRITICAL => 'CRITICAL',
164 Monitor::Simple::RETURN_UNKNOWN => 'UNKNOWN',
165 Monitor::Simple::RETURN_FATAL => 'FATAL',
166 };
167  
168 0 ▶sub html_line {
Subroutine html_line: not called CC=3 SCAR=24.8
Pod undocumented
169 0 my ($self, $service_config, @fields) = @_;
170 0 ▶ my $status = $Status->{$fields[2]} || "Unrecognized";
Condition0%0/2
$$Status{$fields[2]} || 'Unrecognized'
l!l
00
MC/DC0%0/1
$$Status{$fields[2]} || 'Unrecognized'
$$Status{$fields[2]}
Truth table0%0/2
A
A= $$Status{$fields[2]}
Aresult
11
00
171 0 my $class_suffix = lc ($status);
172 0 my $title = $service_config->{description};
173 0 ▶ if ($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) { } 0 0 0
174 0 $title = 'title="' . $self->escapeHTML ($title) . '"';
175 } else {
176 0 $title = '';
177 }
178 0 my $service_name = $self->escapeHTML ($fields[1]);
179 0 my $message = $self->escapeHTML ($fields[3]);
180  
181 0 return <<"END_OF_LINE";
182 <tr class="mon-line mon-line-$class_suffix">
183 <td class="mon-date mon-date-$class_suffix">$fields[0]</td>
184 <td class="mon-service mon-service-$class_suffix" $title>$service_name</td>
185 <td class="mon-code mon-code-$class_suffix">$status</td>
186 <td class="mon-msg mon-msg-$class_suffix">$message</td>
187 </tr>
188 END_OF_LINE
189 }
190  
191 0 ▶sub html_footer {
Subroutine html_footer: not called CC=1 SCAR=6.9
Pod undocumented
192 0 return "</table>\n";
193 }
194  
195 1 ▶sub escapeHTML {
Subroutine escapeHTML: called CC=1 SCAR=0.0
Pod undocumented
196 1 my ($self, $value) = @_;
197 1 $value =~ s{&}{&amp;}gso;
198 1 $value =~ s{<}{&lt;}gso;
199 1 $value =~ s{>}{&gt;}gso;
200 1 $value =~ s{"}{&#34;}gso;
201 1 return $value;
202 }
203  
204 #------------------------------------------------------------------
205 # An atomic output, protected by file locking. All output goes through
206 # here.
207 # -----------------------------------------------------------------
208 14 ▶sub _out {
Subroutine _out: called CC=5 SCAR=18.4
209 14 my ($self, $msg) = @_;
210 14 ▶ if ($self->{outfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'outfile'}) { } 14 0 14
211 14 local *DATA;
212 open (my $data, "+<", $self->{outfile})
213 14 ▶ or LOGDIE "Cannot open " . $self->{outfile} . ": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $data, "+<", $self->{"outfile"} or LOGDIE('Cannot open ' . $self->{"outfile"} . ": $!\n") 14 0 14
214 14 lock_file ($data);
215 14 ▶ print $data $msg or LOGDIE ("Output missed: $msg");
Branch: 1 branch
BranchTrueFalseTotal
print $data $msg or LOGDIE("Output missed: $msg") 14 0 14
216 14 close $data;
217 } else {
218 0 ▶ print STDOUT $msg or LOGDIE ("Output missed: $msg");
Branch: 1 branch
BranchTrueFalseTotal
print STDOUT $msg or LOGDIE("Output missed: $msg") 0 0 0
219 }
220 }
221  
222 #-----------------------------------------------------------------
223 # Output header-line in a format given in $self->{format}, or given by
224 # (otherwise optional) $header.
225 # -----------------------------------------------------------------
226 75 ▶sub header {
Subroutine header: called CC=6 SCAR=33.1
Pod undocumented
227 75 my ($self, $header) = @_;
228 75 ▶ return if $self->{format} eq 'tsv'; # no headers for machines
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"format"} eq "tsv" 75 0 75
229  
230 0 ▶ if ($self->{outfile} or not $self->{onlyerr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"outfile"} or not $self->{"onlyerr"}) 0 0 0
Condition0%0/3
$self->{"outfile"} or not $self->{"onlyerr"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"outfile"} or not $self->{"onlyerr"}
$self->{"outfile"} not $self->{"onlyerr"}
Truth table0%0/3
A or not(B)
A= $self->{"outfile"}B= not $self->{"onlyerr"}
ABresult
1X1
011
000
231 0 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) { } 0 0 0
232 # print whatever header was sent here
233 0 $self->_out ($header);
234  
235 } else {
236 # ...or make a default header (which depends on the format)
237 0 ▶ if ($self->{format} eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'format'} eq 'html') { } 0 0 0
238 0 $self->_out ($self->html_header (@Headers));
239 } else {
240 0 $self->_out ($self->create_report ({}, @Headers));
241 }
242 }
243 }
244 }
245  
246 # using header() to do the printing
247 61 ▶sub footer {
Subroutine footer: called CC=3 SCAR=11.4
Pod undocumented
248 61 my ($self, $footer) = @_;
249 61 ▶ return $self->header ($footer) if $footer;
Branch: 1 branch
BranchTrueFalseTotal
if $footer 0 61 61
250 61 ▶ return $self->header ($self->html_footer()) if $self->{format} eq 'html';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"format"} eq "html" 0 61 61
251 61 return;
252 }
253  
254 #-----------------------------------------------------------------
255 # Format and output one message about a just finished service check
256 # (with an additional date field):
257 # $service_id ... what service is the report about
258 # $code ... error or not error? (see $Monitor::Simple::RETURN*)
259 # $msg ... the real message
260 #
261 # Here are various conditions and their combinations:
262 #
263 # outfile onlyerr what will be done
264 # ---------------------------------------------
265 # yes no - all output to file
266 # yes yes - all output to file
267 # - errors also on STDOUT
268 # no no - all output to STDOUT
269 # no yes - only errors to STDOUT
270 #
271 # Note that additionally to the above the STDERR can also be printed
272 # to - if a plugin chooses to produce some. The STDERR is not
273 # controlled by this module.
274 # -----------------------------------------------------------------
275 14 ▶sub out {
Subroutine out: called CC=8 SCAR=26.0
Pod undocumented
276 14 my ($self, $service_id, $code, $msg) = @_;
277 my $service_config =
278 14 ▶ ( Monitor::Simple::Config->extract_service_config ($service_id, $self->{config}) ||
Condition50%1/2
'Monitor::Simple::Config'->extract_service_config($service_id, $self->{'config'}) || {'id', $service_id, 'name', $service_id}
l!l
140
MC/DC0%0/1
'Monitor::Simple::Config'->extract_service_config($service_id, $self->{'config'}) || {'id', $service_id, 'name', $service_id}
'Monitor::Simple::Config'->extract_service_config($service_id, $self->{'config'})
Truth table50%1/2
A
A= 'Monitor::Simple::Config'->extract_service_config($service_id, $self->{'config'})
Aresult
11
00
279 { id => $service_id, name => $service_id } );
280 14 ▶ my $service_name = ($service_config->{name} or $service_config->{id});
Condition33%1/3
$service_config->{'name'} || $service_config->{'id'}
l!l&&r!l&&!r
1400
MC/DC0%0/2
$service_config->{'name'} || $service_config->{'id'}
$service_config->{'name'} $service_config->{'id'}
Truth table33%1/3
A || B
A= $service_config->{'name'}B= $service_config->{'id'}
ABresult
1X1
011
000
281 14 my $doc = $self->create_report ($service_config, scalar localtime(), $service_name, $code, $msg);
282 14 ▶ if ($self->{outfile} or not $self->{onlyerr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"outfile"} or not $self->{"onlyerr"}) 14 0 14
Condition33%1/3
$self->{"outfile"} or not $self->{"onlyerr"}
l!l&&r!l&&!r
1400
MC/DC0%0/2
$self->{"outfile"} or not $self->{"onlyerr"}
$self->{"outfile"} not $self->{"onlyerr"}
Truth table33%1/3
A or not(B)
A= $self->{"outfile"}B= not $self->{"onlyerr"}
ABresult
1X1
011
000
283 14 $self->_out ($doc);
284 }
285 14 ▶ if ($code ne Monitor::Simple::RETURN_OK and $self->{onlyerr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 0 and $self->{"onlyerr"}) 0 14 14
Condition66%2/3
$code ne 0 and $self->{"onlyerr"}
!ll&&!rl&&r
4100
MC/DC0%0/2
$code ne 0 and $self->{"onlyerr"}
$code ne 0 $self->{"onlyerr"}
Truth table66%2/3
A and B
A= $code ne 0B= $self->{"onlyerr"}
ABresult
0X0
100
111
286 0 ▶ print STDOUT $doc or LOGDIE ("Output missed: $doc");
Branch: 1 branch
BranchTrueFalseTotal
print STDOUT $doc or LOGDIE("Output missed: $doc") 0 0 0
287 }
288 }
289  
290 40 ▶use Fcntl qw(:flock SEEK_END); # import LOCK_* and SEEK_END constants
Subroutine BEGIN: called CC=1 SCAR=0.0
291 14 ▶sub lock_file {
Subroutine lock_file: called CC=3 SCAR=11.7
Pod undocumented
292 14 my ($fh) = @_;
293 14 ▶ flock ($fh, LOCK_EX) or LOGDIE ("Cannot lock output file with reports: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or LOGDIE("Cannot lock output file with reports: $!\n") 14 0 14
294 14 ▶ seek ($fh, 0, SEEK_END) or LOGDIE ("Cannot seek output file with reports: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 2 or LOGDIE("Cannot seek output file with reports: $!\n") 14 0 14
295 }
296  
297 0 ▶sub unlock_file {
Subroutine unlock_file: not called CC=2 SCAR=17.9
Pod undocumented
298 0 my ($fh) = @_;
299 0 ▶ flock ($fh, LOCK_UN)
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 8 or LOGDIE("Cannot unlock output file with reports: $!\n") 0 0 0
300 or LOGDIE ("Cannot unlock output file with reports: $!\n");
301 }
302  
303 40 1;
304  
305  
306 =pod
307  
308 =head1 NAME
309  
310 Monitor::Simple::Output - See documentation in Monitor::Simple
311  
312 =head1 VERSION
313  
314 version 0.2.8
315  
316 =head1 AUTHOR
317  
318 Martin Senger <martin.senger@gmail.com>
319  
320 =head1 COPYRIGHT AND LICENSE
321  
322 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.
323  
324 This is free software; you can redistribute it and/or modify it under
325 the same terms as the Perl 5 programming language system itself.
326  
327 =cut
328  
329  
330 __END__
« Simple.pm ↑ Summary Simple/Config.pm »