Server/Status.pm

filter: Statementstmt 97.8% 89 / 91 Branchbran 73.3% 22 / 30 Conditioncond 64.2% 9 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 87.6% 142 / 162 CC 24 SCAR 16.1
CC
24
Cov
89%
_create_graph_html
8.2
_create_report_msg
7.0
report
5.2
CRAP 5.0
« 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 Xymon::Plugin::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Xymon::Plugin::Server::Status - Xymon status reporter
13  
14 =head1 SYNOPSIS
15  
16 use Xymon::Plugin::Server::Status qw(:colors);
17 my $status = Xymon::Plugin::Server::Status->new("myhostname", "test");
18  
19 $status->add_status(GREEN, "this entry is OK");
20 $status->add_status(RED, "this entry is NOT OK");
21 $status->add_message("Hello! world");
22  
23 $status->add_devmon($devmon); # see Xymon::Plugin::Server::Devmon
24  
25 $status->add_graph("disk");
26  
27 $status->report; # send status to Xymon server
28  
29 =head1 EXPORT
30  
31 Color names
32  
33 GREEN YELLOW RED CLEAR PURPLE BLUE
34  
35 are exported with tag ':colors'
36  
37 =cut
38  
39 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 4 my @colors = qw(GREEN YELLOW RED CLEAR PURPLE BLUE);
42  
43 4 our @EXPORT_OK = @colors;
44 4 our %EXPORT_TAGS = (colors => \@colors);
45  
46 use constant {
47 4 GREEN => 'green',
48 YELLOW => 'yellow',
49 RED => 'red',
50 CLEAR => 'clear',
51 PURPLE => 'purple',
52 BLUE => 'blue',
53 4 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 =head1 SUBROUTINES/METHODS
56  
57 =head2 new(hostname, testname)
58  
59 Create status object for hostname and testname.
60  
61 =cut
62  
63 sub new {
64 20 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
65 20 my $host = shift;
66 20 my $test = shift;
67  
68 20 my $self = {
69 _host => $host,
70 _test => $test,
71 _color => 'clear',
72 _message => '',
73 _devmon => undef,
74 _graph => [],
75 };
76  
77 20 bless $self, $class;
78 }
79  
80 sub _set_color {
81 26 ▶ my $self = shift;
Subroutine _set_color: called CC=4 SCAR=15.9
82 26 my $color = shift;
83  
84 26 ▶ return if ($self->{_color} eq RED);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_color"} eq "red" 4 22 26
85  
86 22 ▶ if ($self->{_color} eq YELLOW) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_color"} eq "yellow") 0 22 22
87 0 ▶ $self->{_color} = $color if ($color eq RED);
Branch: 1 branch
BranchTrueFalseTotal
if $color eq "red" 0 0 0
88 0 return;
89 }
90  
91 22 $self->{_color} = $color;
92 }
93  
94 =head2 add_status(color, msg)
95  
96 Add status and its short message.
97  
98 =cut
99  
100 sub add_status {
101 26 ▶ my $self = shift;
Subroutine add_status: called CC=3 SCAR=11.0
Pod documented
102 26 my ($color, $msg) = @_;
103  
104 26 ▶ if (defined($msg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg) 20 6 26
105 20 ▶ $msg .= "\n" if ($msg !~ /\n$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n$/ 20 0 20
106  
107 20 $self->{_message} .= "&$color $msg";
108 }
109  
110 26 $self->_set_color($color);
111 }
112  
113 =head2 add_message(msg)
114  
115 Add message shown in Xymon status page.
116  
117 =cut
118  
119 sub add_message {
120 6 ▶ my $self = shift;
Subroutine add_message: called CC=2 SCAR=7.0
Pod documented
121 6 my ($msg) = @_;
122  
123 6 ▶ $msg .= "\n" if ($msg !~ /\n$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n$/ 6 0 6
124  
125 6 $self->{_message} .= "$msg";
126 }
127  
128 =head2 add_devmon(devmon)
129  
130 Add devmon data. See Xymon::Plugin::Server::Devmon
131  
132 =cut
133  
134 sub add_devmon {
135 2 ▶ my $self = shift;
Subroutine add_devmon: called CC=1 SCAR=0.0
Pod documented
136 2 my $devmon= shift;
137  
138 2 $self->{_devmon} = $devmon;
139 }
140  
141 =head2 add_graph(testname)
142  
143 Add graph shown in Xymon status page.
144 "test" name must be defined in graph definition file.
145 (named hobbitgraph.cfg in Xymon 4.2, graphs.cfg in Xymon 4.3)
146  
147 =cut
148  
149 sub add_graph {
150 2 ▶ my $self = shift;
Subroutine add_graph: called CC=1 SCAR=0.0
Pod documented
151 2 my $test= shift;
152  
153 2 push (@{$self->{_graph}}, $test);
154 }
155  
156 sub _create_graph_html {
157 2 ▶ my $self = shift;
Subroutine _create_graph_html: called CC=8 SCAR=21.1
158 2 my $test = shift;
159  
160 2 my $type = "4.3";
161  
162 2 my ($major, $minor) = @{Xymon::Plugin::Server->version};
163 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
164  
165 2 my $host = $self->{_host};
166 2 my $color = $self->{_color};
167 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
168 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
169 2 my $end_time = time;
170 2 my $start_time = time - (60 * 60 * 48);
171  
172 2 ▶ if ($type eq "4.2") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq '4.2') { } 1 1 2
173 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
174  
175 1 my $html = << "_EOS";
176 <p>
177 <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">
178  
179 <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">
180 </A>
181 </p>
182 _EOS
183 }
184 else {
185 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
186  
187 1 my $html = << "_EOS";
188 <p>
189 <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">
190 <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">
191 </A>
192 </p>
193 _EOS
194  
195 }
196 }
197  
198 sub _create_report_msg {
199 20 ▶ my $self = shift;
Subroutine _create_report_msg: called CC=7 SCAR=19.5
200  
201 20 local $ENV{LANG} = 'C';
202 20 my $datestr = scalar localtime time;
203 20 my $statstr = '';
204  
205 20 ▶ if ($self->{_color} eq GREEN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_color'} eq 'green') { } 8 12 20
elsif ($self->{'_color'} eq 'yellow' or $self->{'_color'} eq 'red') { } 10 2 12
206 8 $statstr = ' - OK';
207 }
208 ▶ 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
209 10 $statstr = ' - NOT OK';
210 }
211  
212 my $msg = sprintf("status %s.%s %s %s %s%s\n",
213 $self->{_host},
214 $self->{_test},
215 $self->{_color},
216 $datestr,
217 $self->{_test},
218 20 $statstr);
219  
220 20 $msg .= "\n";
221 20 $msg .= $self->{_message};
222  
223 20 ▶ if ($self->{_devmon}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_devmon"}) 2 18 20
224 2 $msg .= "\n";
225 2 $msg .= $self->{_devmon}->format;
226 }
227  
228 20 ▶ if (@{$self->{_graph}} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_graph"};} > 0) 2 18 20
229 2 $msg .= "</PRE></TD></TR></TABLE>\n";
230  
231 2 for my $g (@{$self->{_graph}}) {
232 2 $msg .= $self->_create_graph_html($g);
233 }
234  
235 2 $msg .= "<TABLE><TR><TD><PRE>\n";
236 }
237  
238 20 return $msg;
239 }
240  
241 =head2 report
242  
243 Report current status to Xymon server.
244  
245 =cut
246  
247 sub report {
248 20 ▶ my $self = shift;
Subroutine report: called CC=5 SCAR=16.6
Pod documented
249  
250 20 my $msg = $self->_create_report_msg;
251  
252 20 ▶ print "$msg" if ($ENV{XYMON_PLUGIN_DEBUG});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"XYMON_PLUGIN_DEBUG"} 0 20 20
253  
254 20 my $bb = Xymon::Plugin::Server->home . "/bin/bb";
255  
256 20 for my $bbh (Xymon::Plugin::Server->display_hosts) {
257 20 ▶ print "send to $bbh\n" if ($ENV{XYMON_PLUGIN_DEBUG});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"XYMON_PLUGIN_DEBUG"} 0 20 20
258 20 ▶ open(my $fh, "|-", "$bb", $bbh, "@") or die "cannot execute $bb: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "|-", "$bb", $bbh, "\@" or die "cannot execute ${bb}: $!" 0 20 20
259 20 print $fh $msg;
260 }
261 }
262  
263 4 1;
« Server/Hosts.pm ↑ Summary Server/Dispatch.pm »