Server/Status.pm

filter: Statementstmt 98.2% 115 / 117 Branchbran 77.7% 28 / 36 Conditioncond 68.7% 11 / 16 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 90.0% 181 / 201 CC 28 SCAR 15.1
CC
28
Cov
91%
_create_graph_html
8.2
_create_report_msg
7.0
report
5.2
CRAP 4.5
« Server/Hosts.pm ↑ Summary Server/Dispatch.pm »
1 #
2 # status reporter
3 #
4 package Xymon::Plugin::Server::Status;
5  
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Xymon::Plugin::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Xymon::Plugin::Server::Status - Xymon status reporter
15  
16 =head1 SYNOPSIS
17  
18 use Xymon::Plugin::Server::Status qw(:colors);
19 my $status = Xymon::Plugin::Server::Status->new("myhostname", "test");
20  
21 $status->add_status(GREEN, "this entry is OK");
22 $status->add_status(RED, "this entry is NOT OK");
23 $status->add_message("Hello! world");
24  
25 $status->add_devmon($devmon); # see Xymon::Plugin::Server::Devmon
26  
27 $status->add_graph("disk");
28  
29 $status->report; # send status to Xymon server
30  
31 =head1 EXPORT
32  
33 Color names
34  
35 GREEN YELLOW RED CLEAR PURPLE BLUE
36  
37 are exported with tag ':colors'
38  
39 =cut
40  
41 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 4 my @colors = qw(GREEN YELLOW RED CLEAR PURPLE BLUE);
44  
45 4 our @EXPORT_OK = @colors;
46 4 our %EXPORT_TAGS = (colors => \@colors);
47  
48 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 GREEN => 'green',
50 YELLOW => 'yellow',
51 RED => 'red',
52 CLEAR => 'clear',
53 PURPLE => 'purple',
54 BLUE => 'blue',
55 4 };
56  
57 =head1 SUBROUTINES/METHODS
58  
59 =head2 new(hostname, testname, attr)
60  
61 Create status object for hostname and testname.
62  
63 attr is optional hashref to change actions of object.
64 Currently, following parameter is defined.
65  
66 =over
67  
68 =item EscapeMessage
69  
70 =over
71  
72 =item 0 - nochange (default)
73  
74 Make no change to message.
75  
76 =item 1 - replace to '_'
77  
78 Some characters (<, >, &) are replaced to '_'.
79  
80 =item 2 - html escape
81  
82 '<', '>, '&' are replaced to '&lt;', '&gt;', '&amp' respectively.
83  
84 =back
85  
86 =back
87  
88 =cut
89  
90 23 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
91 23 my $class = shift;
92 23 my $host = shift;
93 23 my $test = shift;
94 23 my $attr = shift;
95  
96 23 ▶ my $self = {
Condition100%2/2
$attr || {}
l!l
221
MC/DC100%1/1
$attr || {}
$attr
Truth table100%2/2
A
A= $attr
Aresult
11
00
97 _host => $host,
98 _test => $test,
99 _color => 'clear',
100 _message => '',
101 _devmon => undef,
102 _graph => [],
103 _attr => $attr || {}
104 };
105  
106 23 bless $self, $class;
107 }
108  
109 #
110 # CLEAR, BLUE, GREEN, PURPLE, YELLOW, RED
111 #
112 4 my %order = (CLEAR , 0,
113 BLUE , 1,
114 GREEN , 2,
115 PURPLE , 3,
116 YELLOW , 4,
117 RED , 5,
118 );
119  
120 29 ▶sub _set_color {
Subroutine _set_color: called CC=4 SCAR=14.4
121 29 my $self = shift;
122 29 my $color = shift;
123  
124 29 my $cur = $order{$self->{_color}};
125 29 my $new = $order{$color};
126  
127 29 ▶ carp "Unknown color: $color" unless (defined($new));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 0 29 29
128  
129 # if purple is selected, this report will be eliminated by Xymon.
130 # so we change color.
131 29 ▶ if ($color eq PURPLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color eq "purple") 0 29 29
132 0 $color = YELLOW;
133 0 $new = $order{$color};
134 }
135
136 29 ▶ if ($cur < $new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur < $new) 23 6 29
137 23 $self->{_color} = $color;
138 }
139  
140 29 return $self->{_color};
141 }
142  
143 =head2 add_status(color, msg)
144  
145 Add status and its short message.
146  
147 =cut
148  
149 29 ▶sub add_status {
Subroutine add_status: called CC=3 SCAR=11.0
Pod documented
150 29 my $self = shift;
151 29 my ($color, $msg) = @_;
152  
153 29 ▶ if (defined($msg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg) 23 6 29
154 23 ▶ $msg .= "\n" if ($msg !~ /\n$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n$/ 23 0 23
155 23 my $m = $self->_escape_string($msg);
156  
157 23 $self->{_message} .= "&$color $m";
158 }
159  
160 29 $self->_set_color($color);
161 }
162  
163 =head2 add_message(msg)
164  
165 Add message shown in Xymon status page.
166  
167 =cut
168  
169 9 ▶sub add_message {
Subroutine add_message: called CC=2 SCAR=7.0
Pod documented
170 9 my $self = shift;
171 9 my ($msg) = @_;
172  
173 9 ▶ $msg .= "\n" if ($msg !~ /\n$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n$/ 9 0 9
174  
175 9 $self->{_message} .= $self->_escape_string($msg);
176 }
177  
178 =head2 add_devmon(devmon)
179  
180 Add devmon data. See Xymon::Plugin::Server::Devmon
181  
182 =cut
183  
184 2 ▶sub add_devmon {
Subroutine add_devmon: called CC=1 SCAR=0.0
Pod documented
185 2 my $self = shift;
186 2 my $devmon= shift;
187  
188 2 $self->{_devmon} = $devmon;
189 }
190  
191 =head2 add_graph(testname)
192  
193 Add graph shown in Xymon status page.
194 "test" name must be defined in graph definition file.
195 (named hobbitgraph.cfg in Xymon 4.2, graphs.cfg in Xymon 4.3)
196  
197 =cut
198  
199 2 ▶sub add_graph {
Subroutine add_graph: called CC=1 SCAR=0.0
Pod documented
200 2 my $self = shift;
201 2 my $test= shift;
202  
203 2 push (@{$self->{_graph}}, $test);
204 }
205  
206 2 ▶sub _create_graph_html {
Subroutine _create_graph_html: called CC=8 SCAR=21.1
207 2 my $self = shift;
208 2 my $test = shift;
209  
210 2 my $type = "4.3";
211  
212 2 my ($major, $minor) = @{Xymon::Plugin::Server->version};
213 2 ▶ $type = "4.2" if ($major == 4 && $minor == 2);
Branch: 1 branch
BranchTrueFalseTotal
if $major == 4 and $minor == 2 1 1 2
Condition66%2/3
$major == 4 and $minor == 2
!ll&&!rl&&r
011
MC/DC50%1/2
$major == 4 and $minor == 2
$major == 4 $minor == 2
Truth table66%2/3
A and B
A= $major == 4B= $minor == 2
ABresult
0X0
100
111
214  
215 2 my $host = $self->{_host};
216 2 my $color = $self->{_color};
217 2 ▶ my $width = $ENV{RRDWIDTH} || 576;
Condition50%1/2
$ENV{'RRDWIDTH'} || 576
l!l
02
MC/DC0%0/1
$ENV{'RRDWIDTH'} || 576
$ENV{'RRDWIDTH'}
Truth table50%1/2
A
A= $ENV{'RRDWIDTH'}
Aresult
11
00
218 2 ▶ my $height = $ENV{RRDHEIGHT} || 120;
Condition50%1/2
$ENV{'RRDHEIGHT'} || 120
l!l
02
MC/DC0%0/1
$ENV{'RRDHEIGHT'} || 120
$ENV{'RRDHEIGHT'}
Truth table50%1/2
A
A= $ENV{'RRDHEIGHT'}
Aresult
11
00
219 2 my $end_time = time;
220 2 my $start_time = time - (60 * 60 * 48);
221  
222 2 ▶ if ($type eq "4.2") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq '4.2') { } 1 1 2
223 1 ▶ my $cgi_url = $ENV{BBSERVERCGIURL} || "/xymon-cgi";
Condition50%1/2
$ENV{'BBSERVERCGIURL'} || '/xymon-cgi'
l!l
01
MC/DC0%0/1
$ENV{'BBSERVERCGIURL'} || '/xymon-cgi'
$ENV{'BBSERVERCGIURL'}
Truth table50%1/2
A
A= $ENV{'BBSERVERCGIURL'}
Aresult
11
00
224  
225 1 my $html = << "_EOS";
226 <p>
227 <A HREF="$cgi_url/hobbitgraph.sh?host=$host&amp;service=$test&amp;graph_width=$width&amp;graph_height=$height&amp;disp=$host&amp;nostale&amp;color=$color&amp;action=menu">
228  
229 <IMG BORDER=0 SRC="$cgi_url/hobbitgraph.sh?host=$host&amp;service=$test&amp;graph_width=$width&amp;graph_height=$height&amp;disp=$host&amp;nostale&amp;color=$color&amp;graph=hourly&amp;action=view">
230 </A>
231 </p>
232 _EOS
233 }
234 else {
235 1 ▶ my $cgi_url = $ENV{XYMONSERVERCGIURL} || "/xymon-cgi";
Condition50%1/2
$ENV{'XYMONSERVERCGIURL'} || '/xymon-cgi'
l!l
01
MC/DC0%0/1
$ENV{'XYMONSERVERCGIURL'} || '/xymon-cgi'
$ENV{'XYMONSERVERCGIURL'}
Truth table50%1/2
A
A= $ENV{'XYMONSERVERCGIURL'}
Aresult
11
00
236  
237 1 my $html = << "_EOS";
238 <p>
239 <A HREF="$cgi_url/showgraph.sh?host=$host&amp;service=$test&amp;graph_width=$width&amp;graph_height=$height&amp;disp=$host&amp;nostale&amp;color=$color&amp;graph_start=$start_time&amp;graph_end=$end_time&amp;action=menu">
240 <IMG BORDER=0 SRC="$cgi_url/showgraph.sh?host=$host&amp;service=$test&amp;graph_width=$width&amp;graph_height=$height&amp;disp=$host&amp;nostale&amp;color=$color&amp;graph_start=$start_time&amp;graph_end=$end_time&amp;graph=hourly&amp;action=view">
241 </A>
242 </p>
243 _EOS
244  
245 }
246 }
247  
248 2 ▶sub _escape_replace {
Subroutine _escape_replace: called CC=1 SCAR=0.0
249 2 my $s = shift;
250 2 $s =~ s/[<>&]/_/g;
251 2 return $s;
252 }
253  
254 2 ▶sub _escape_entity {
Subroutine _escape_entity: called CC=1 SCAR=0.0
255 2 my $s = shift;
256  
257 2 $s =~ s/&/&amp;/g;
258 2 $s =~ s/</&lt;/g;
259 2 $s =~ s/>/&gt;/g;
260 2 return $s;
261 }
262  
263 32 ▶sub _escape_string {
Subroutine _escape_string: called CC=4 SCAR=13.9
264 32 my $self = shift;
265 32 my $s = shift;
266  
267 32 ▶ if ($self->{_attr}->{EscapeMessage}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_attr"}{"EscapeMessage"}) 4 28 32
268 4 my $n = $self->{_attr}->{EscapeMessage};
269 4 ▶ if ($n == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n == 1) { } 2 2 4
elsif ($n == 2) { } 2 0 2
270 2 return _escape_replace($s);
271 }
272 elsif ($n == 2) {
273 2 return _escape_entity($s);
274 }
275  
276 }
277  
278 28 return $s;
279 }
280  
281 23 ▶sub _create_report_msg {
Subroutine _create_report_msg: called CC=7 SCAR=19.5
282 23 my $self = shift;
283  
284 23 local $ENV{LANG} = 'C';
285 23 my $datestr = scalar localtime time;
286 23 my $statstr = '';
287  
288 23 ▶ if ($self->{_color} eq GREEN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_color'} eq 'green') { } 11 12 23
elsif ($self->{'_color'} eq 'yellow' or $self->{'_color'} eq 'red') { } 10 2 12
289 11 $statstr = ' - OK';
290 }
291 ▶ elsif ($self->{_color} eq YELLOW || $self->{_color} eq RED) {
Condition100%3/3
$self->{"_color"} eq "yellow" or $self->{"_color"} eq "red"
l!l&&r!l&&!r
462
MC/DC100%2/2
$self->{"_color"} eq "yellow" or $self->{"_color"} eq "red"
$self->{"_color"} eq "yellow" $self->{"_color"} eq "red"
Truth table100%3/3
A or B
A= $self->{"_color"} eq "yellow"B= $self->{"_color"} eq "red"
ABresult
1X1
011
000
292 10 $statstr = ' - NOT OK';
293 }
294  
295 my $msg = sprintf("status %s.%s %s %s %s%s\n",
296 $self->{_host},
297 $self->{_test},
298 $self->{_color},
299 $datestr,
300 $self->{_test},
301 23 $statstr);
302  
303 23 $msg .= "\n";
304 23 $msg .= $self->{_message};
305  
306 23 ▶ if ($self->{_devmon}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_devmon"}) 2 21 23
307 2 $msg .= "\n";
308 2 $msg .= $self->{_devmon}->format;
309 }
310  
311 23 ▶ if (@{$self->{_graph}} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_graph"};} > 0) 2 21 23
312 2 $msg .= "</PRE></TD></TR></TABLE>\n";
313  
314 2 for my $g (@{$self->{_graph}}) {
315 2 $msg .= $self->_create_graph_html($g);
316 }
317  
318 2 $msg .= "<TABLE><TR><TD><PRE>\n";
319 }
320  
321 23 return $msg;
322 }
323  
324 =head2 report
325  
326 Report current status to Xymon server.
327  
328 =cut
329  
330 23 ▶sub report {
Subroutine report: called CC=5 SCAR=16.6
Pod documented
331 23 my $self = shift;
332  
333 23 my $msg = $self->_create_report_msg;
334  
335 23 ▶ print "$msg" if ($ENV{XYMON_PLUGIN_DEBUG});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"XYMON_PLUGIN_DEBUG"} 0 23 23
336  
337 23 my $bb = Xymon::Plugin::Server->home . "/bin/bb";
338  
339 23 for my $bbh (Xymon::Plugin::Server->display_hosts) {
340 23 ▶ print "send to $bbh\n" if ($ENV{XYMON_PLUGIN_DEBUG});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"XYMON_PLUGIN_DEBUG"} 0 23 23
341 23 ▶ open(my $fh, "|-", "$bb", $bbh, "@") or die "cannot execute $bb: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "|-", "$bb", $bbh, "\@" or die "cannot execute ${bb}: $!" 23 0 23
342 23 print $fh $msg;
343 }
344 }
345  
346 4 1;
« Server/Hosts.pm ↑ Summary Server/Dispatch.pm »