File Coverage

blib/lib/Xymon/Plugin/Server/Status.pm
Criterion Covered Total %
statement 85 87 97.7
branch 22 30 73.3
condition 9 14 64.2
subroutine 13 13 100.0
pod 6 6 100.0
total 135 150 90.0


line stmt bran cond sub pod time code
1             #
2             # status reporter
3             #
4             package Xymon::Plugin::Server::Status;
5              
6 3     3   106595 use strict;
  3         9  
  3         129  
7              
8 3     3   1621 use Xymon::Plugin::Server;
  3         8  
  3         114  
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 3     3   21 use base qw(Exporter);
  3         5  
  3         605  
40              
41             my @colors = qw(GREEN YELLOW RED CLEAR PURPLE BLUE);
42              
43             our @EXPORT_OK = @colors;
44             our %EXPORT_TAGS = (colors => \@colors);
45              
46             use constant {
47 3         5354 GREEN => 'green',
48             YELLOW => 'yellow',
49             RED => 'red',
50             CLEAR => 'clear',
51             PURPLE => 'purple',
52             BLUE => 'blue',
53 3     3   24 };
  3         7  
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     20 1 45144 my $class = shift;
65 20         53 my $host = shift;
66 20         54 my $test = shift;
67              
68 20         240 my $self = {
69             _host => $host,
70             _test => $test,
71             _color => 'clear',
72             _message => '',
73             _devmon => undef,
74             _graph => [],
75             };
76              
77 20         118 bless $self, $class;
78             }
79              
80             sub _set_color {
81 26     26   47 my $self = shift;
82 26         466 my $color = shift;
83              
84 26 100       131 return if ($self->{_color} eq RED);
85              
86 22 50       99 if ($self->{_color} eq YELLOW) {
87 0 0       0 $self->{_color} = $color if ($color eq RED);
88 0         0 return;
89             }
90              
91 22         99 $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     26 1 301 my $self = shift;
102 26         73 my ($color, $msg) = @_;
103              
104 26 100       910 if (defined($msg)) {
105 20 50       95 $msg .= "\n" if ($msg !~ /\n$/);
106              
107 20         112 $self->{_message} .= "&$color $msg";
108             }
109              
110 26         355 $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     6 1 39 my $self = shift;
121 6         14 my ($msg) = @_;
122              
123 6 50       37 $msg .= "\n" if ($msg !~ /\n$/);
124              
125 6         32 $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     2 1 18 my $self = shift;
136 2         9 my $devmon= shift;
137              
138 2         9 $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     2 1 25 my $self = shift;
151 2         12 my $test= shift;
152              
153 2         15 push (@{$self->{_graph}}, $test);
  2         16  
154             }
155              
156             sub _create_graph_html {
157 2     2   10 my $self = shift;
158 2         359 my $test = shift;
159              
160 2         12 my $type = "4.3";
161              
162 2         6 my ($major, $minor) = @{Xymon::Plugin::Server->version};
  2         52  
163 2 100 66     53 $type = "4.2" if ($major == 4 && $minor == 2);
164              
165 2         13 my $host = $self->{_host};
166 2         6 my $color = $self->{_color};
167 2   50     49 my $width = $ENV{RRDWIDTH} || 576;
168 2   50     20 my $height = $ENV{RRDHEIGHT} || 120;
169 2         10 my $end_time = time;
170 2         6 my $start_time = time - (60 * 60 * 48);
171              
172 2 100       15 if ($type eq "4.2") {
173 1   50     10 my $cgi_url = $ENV{BBSERVERCGIURL} || "/xymon-cgi";
174              
175 1         13 my $html = << "_EOS";
176            

177            
178              
179            
180            
181            

182             _EOS
183             }
184             else {
185 1   50     15 my $cgi_url = $ENV{XYMONSERVERCGIURL} || "/xymon-cgi";
186              
187 1         16 my $html = << "_EOS";
188            

189            
190            
191            
192            

193             _EOS
194              
195             }
196             }
197              
198             sub _create_report_msg {
199 20     20   42 my $self = shift;
200              
201 20         365 local $ENV{LANG} = 'C';
202 20         1694 my $datestr = scalar localtime time;
203 20         50 my $statstr = '';
204              
205 20 100 100     245 if ($self->{_color} eq GREEN) {
    100          
206 8         20 $statstr = ' - OK';
207             }
208             elsif ($self->{_color} eq YELLOW || $self->{_color} eq RED) {
209 10         623 $statstr = ' - NOT OK';
210             }
211              
212 20         186 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             $statstr);
219              
220 20         666 $msg .= "\n";
221 20         599 $msg .= $self->{_message};
222              
223 20 100       420 if ($self->{_devmon}) {
224 2         5 $msg .= "\n";
225 2         18 $msg .= $self->{_devmon}->format;
226             }
227              
228 20 100       37 if (@{$self->{_graph}} > 0) {
  20         93  
229 2         13 $msg .= "
\n"; 230               231 2         80 for my $g (@{$self->{_graph}}) {   2         19   232 2         32 $msg .= $self->_create_graph_html($g); 233             } 234               235 2         10 $msg .= "
\n"; 
236             }
237              
238 20         142 return $msg;
239             }
240              
241             =head2 report
242              
243             Report current status to Xymon server.
244              
245             =cut
246              
247             sub report {
248 20     20 1 146 my $self = shift;
249              
250 20         156 my $msg = $self->_create_report_msg;
251              
252 20 50       106 print "$msg" if ($ENV{XYMON_PLUGIN_DEBUG});
253              
254 20         513 my $bb = Xymon::Plugin::Server->home . "/bin/bb";
255              
256 20         185 for my $bbh (Xymon::Plugin::Server->display_hosts) {
257 20 50       72 print "send to $bbh\n" if ($ENV{XYMON_PLUGIN_DEBUG});
258 20 50       158661 open(my $fh, "|-", "$bb", $bbh, "@") or die "cannot execute $bb: $!";
259 20         152991 print $fh $msg;
260             }
261             }
262              
263             1;