blib/lib/Monit/HTTP.pm

filter: Statementstmt 82.7% 115 / 139 Branchbran 58.8% 20 / 34 Conditioncond 37.5% 12 / 32 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 86.3% 19 / 22 Podpod 100% 8 / 8 totaltotal 69.1% 175 / 253 CC 38 SCAR 23.1
CC
38
Cov
72%
command_run
20.0
service_status
17.8
new
11.9
CRAP 10.1
↑ Summary
1 #!/bin/false
2 # ABSTRACT: An OOP interface to Monit.
3 # PODNAME: Monit::HTTP
4  
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Monit::HTTP;
10 6 $Monit::HTTP::VERSION = '0.08';
11 6 ▶use HTTP::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use XML::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 our (
16 6 %MONIT_ACTIONS,
17 %MONIT_ACTIONS_REV,
18 %MONIT_STATUS,
19 %MONIT_STATUS_REV,
20 %MONIT_TYPES,
21 %MONIT_TYPES_REV,
22 %MONIT_MONITOR,
23 %MONIT_MONITOR_REV,
24 );
25  
26 6 my $STATUS_URL_TEMPLATE = 'http://%s%s:%d/_status?format=xml';
27 6 my $COMMAND_URL_TEMPLATE = 'http://%s:%d/%s';
28  
29 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 6 %MONIT_ACTIONS_REV = (
32 'stop' => 'ACTION_STOP',
33 'start' => 'ACTION_START',
34 'restart' => 'ACTION_RESTART',
35 'monitor' => 'ACTION_MONITOR',
36 'unmonitor' => 'ACTION_UNMONITOR',
37 );
38 6 %MONIT_ACTIONS = reverse %MONIT_ACTIONS_REV;
39  
40 6 %MONIT_MONITOR_REV = (
41 0 => 'off',
42 1 => 'monitored',
43 2 => 'initializing',
44 );
45 6 %MONIT_MONITOR = reverse %MONIT_MONITOR_REV;
46  
47 6 %MONIT_STATUS_REV = (
48 0 => 'Running',
49 32 => 'Connection Failed',
50 512 => 'Does not exist',
51 );
52 6 %MONIT_STATUS = reverse %MONIT_STATUS_REV;
53  
54 6 %MONIT_TYPES_REV = (
55 0 => 'TYPE_FILESYSTEM',
56 1 => 'TYPE_DIRECTORY',
57 2 => 'TYPE_FILE',
58 3 => 'TYPE_PROCESS',
59 4 => 'TYPE_HOST',
60 5 => 'TYPE_SYSTEM',
61 6 => 'TYPE_FIFO',
62 7 => 'TYPE_STATUS',
63 );
64 6 %MONIT_TYPES = reverse %MONIT_TYPES_REV;
65  
66 }
67  
68 # This creates constants from all the above values
69 # perl 5.10 has strange issues just going:
70 # use constant reverse %{ MONIT_TYPES() }
71 # So work around it with do {}
72 6 ▶use constant do { my %foo = reverse( %MONIT_TYPES_REV ); \%foo };
Subroutine BEGIN: called CC=1 SCAR=0.0
73 6 ▶use constant do { my %foo = reverse( %MONIT_ACTIONS_REV ); \%foo };
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 6 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 6 our (%EXPORT_TAGS, @EXPORT_OK);
77  
78 6 %EXPORT_TAGS = (
79 constants => [qw/
80 ACTION_MONITOR
81 ACTION_RESTART
82 ACTION_START
83 ACTION_STOP
84 ACTION_UNMONITOR
85  
86 TYPE_DIRECTORY
87 TYPE_FIFO
88 TYPE_FILE
89 TYPE_FILESYSTEM
90 TYPE_HOST
91 TYPE_PROCESS
92 TYPE_SYSTEM
93 /],
94  
95 hashes => [qw/
96 %MONIT_ACTIONS
97 %MONIT_ACTIONS_REV
98 %MONIT_STATUS
99 %MONIT_STATUS_REV
100 %MONIT_TYPES
101 %MONIT_TYPES_REV
102 %MONIT_MONITOR
103 %MONIT_MONITOR_REV
104 /],
105 );
106  
107 @EXPORT_OK = (
108 6 @{$EXPORT_TAGS{constants}},
109 6 @{$EXPORT_TAGS{hashes}},
110 );
111  
112 6 Exporter::export_ok_tags( keys %EXPORT_TAGS );
113  
114  
115 3 ▶sub new {
Subroutine new: called CC=8 SCAR=24.7
Pod documented
116 3 my ($class, %self) = @_;
117  
118 # OOP stuff
119 3 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
120 3 my $self = \%self;
121 3 bless $self, $class;
122  
123 # set some defaults, if not already set
124 3 ▶ $self->{hostname} ||= 'localhost';
Condition100%2/2
$self->{"hostname"} ||= "localhost"
l!l
12
MC/DC100%1/1
$self->{"hostname"} ||= "localhost"
$self->{"hostname"}
Truth table100%2/2
A
A= $self->{"hostname"}
Aresult
11
00
125 3 ▶ $self->{port} ||= 2812;
Condition50%1/2
$self->{"port"} ||= 2812
l!l
03
MC/DC0%0/1
$self->{"port"} ||= 2812
$self->{"port"}
Truth table50%1/2
A
A= $self->{"port"}
Aresult
11
00
126 3 ▶ $self->{use_auth} ||= 0;
Condition50%1/2
$self->{"use_auth"} ||= 0
l!l
03
MC/DC0%0/1
$self->{"use_auth"} ||= 0
$self->{"use_auth"}
Truth table50%1/2
A
A= $self->{"use_auth"}
Aresult
11
00
127 3 ▶ if ($self->{use_auth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_auth"}) 0 3 3
128 0 ▶ $self->{username} ||= 'admin';
Condition0%0/2
$self->{"username"} ||= "admin"
l!l
00
MC/DC0%0/1
$self->{"username"} ||= "admin"
$self->{"username"}
Truth table0%0/2
A
A= $self->{"username"}
Aresult
11
00
129 0 ▶ $self->{password} ||= 'monit';
Condition0%0/2
$self->{"password"} ||= "monit"
l!l
00
MC/DC0%0/1
$self->{"password"} ||= "monit"
$self->{"password"}
Truth table0%0/2
A
A= $self->{"password"}
Aresult
11
00
130 }
131  
132 3 $self->{ua} = HTTP::Tiny->new( agent => sprintf('Perl %s/%s',__PACKAGE__,$Monit::HTTP::VERSION) );
133 3 $self->_generate_url;
134  
135 3 return $self
136 }
137  
138 5 ▶sub _generate_url {
Subroutine _generate_url: called CC=4 SCAR=17.2
139  
140 5 my $self = shift;
141  
142 5 my $auth = '';
143 5 ▶ if (defined $self->{username} and defined $self->{password} and $self->{use_auth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"username"} and defined $self->{"password"} and $self->{"use_auth"}) 0 5 5
Condition33%2/6
defined $self->{"username"} and defined $self->{"password"} and $self->{"use_auth"}
!ll&&!rl&&r
500
defined $self->{"username"} and defined $self->{"password"}
!ll&&!rl&&r
500
MC/DC0%0/3
defined $self->{"username"} and defined $self->{"password"} and $self->{"use_auth"}
defined $self->{"username"} defined $self->{"password"} $self->{"use_auth"}
Truth table25%1/4
A and B and C
A= defined $self->{"username"}B= defined $self->{"password"}C= $self->{"use_auth"}
ABCresult
0XX0
10X0
1100
1111
144 $auth = sprintf('%s:%s@',$self->{username},$self->{password})
145 0 }
146  
147 $self->{status_url} = sprintf($STATUS_URL_TEMPLATE,
148 5 $auth, $self->{hostname}, $self->{port});
149 }
150  
151  
152 1 ▶sub set_hostname {
Subroutine set_hostname: called CC=1 SCAR=0.0
Pod documented
153 1 my ($self, $hostname) = @_;
154 1 $self->{hostname} = $hostname;
155 1 $self->_generate_url;
156 1 return $hostname
157 }
158  
159  
160 1 ▶sub set_port {
Subroutine set_port: called CC=1 SCAR=0.0
Pod documented
161 1 my ($self, $port) = @_;
162 1 $self->{port} = $port;
163 1 $self->_generate_url;
164 1 return $port
165 }
166  
167  
168 0 ▶sub set_username {
Subroutine set_username: not called CC=1 SCAR=6.9
Pod documented
169 0 my ($self, $username) = @_;
170 0 $self->{username} = $username;
171 0 $self->_generate_url;
172 0 return $username
173 }
174  
175  
176 0 ▶sub set_password {
Subroutine set_password: not called CC=1 SCAR=6.9
Pod documented
177 0 my ($self, $password ) = @_;
178 0 $self->{password} = $password;
179 0 $self->_generate_url;
180 0 return $password
181 }
182  
183  
184 7 ▶sub _fetch_info {
Subroutine _fetch_info: called CC=3 SCAR=11.0
185 7 my ($self) = @_;
186  
187 7 my $res = $self->{ua}->get( $self->{status_url} );
188 7 ▶ if ($res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'success'}) { } 3 4 7
189 3 $self->_set_xml($res->{content});
190 3 $self->{xml_hash} = xml2hash( $self->_get_xml );
191 }
192 else {
193 croak sprintf "Error while connecting to %s !\n" .
194 "Status: %s\nReason: %s\nContent: %s\n",
195 4 ▶ $self->{status_url}, $res->{status}, $res->{reason}, $res->{content} || 'NIL';
Condition50%1/2
$res->{'content'} || 'NIL'
l!l
40
MC/DC0%0/1
$res->{'content'} || 'NIL'
$res->{'content'}
Truth table50%1/2
A
A= $res->{'content'}
Aresult
11
00
196 }
197  
198 3 return 1
199 }
200  
201  
202 5 ▶sub get_services {
Subroutine get_services: called CC=7 SCAR=22.6
Pod documented
203 5 my ($self, $type) = @_;
204 5 my @services;
205 5 ▶ $type ||= '-1';
Condition50%1/2
$type ||= -1
l!l
05
MC/DC0%0/1
$type ||= -1
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
206  
207 croak "Don't understand this service type!\n"
208 0 ▶ unless $type == -1 or grep {$_ == $type} keys %{MONIT_TYPES()};
Branch: 1 branch
BranchTrueFalseTotal
unless $type == -1 or grep {$_ == $type;} keys %{MONIT_TYPES();} 0 5 5
Condition33%1/3
$type == -1 or grep {$_ == $type;} keys %{MONIT_TYPES();}
l!l&&r!l&&!r
500
MC/DC0%0/2
$type == -1 or grep {$_ == $type;} keys %{MONIT_TYPES();}
$type == -1 grep {$_ == $type;} keys %{MONIT_TYPES();}
Truth table33%1/3
A or B
A= $type == -1B= grep {$_ == $type;} keys %{MONIT_TYPES();}
ABresult
1X1
011
000
209  
210 5 $self->_fetch_info;
211  
212 1 for my $s (@{$self->{xml_hash}->{monit}->{service}}) {
213 2 ▶ if ($type == -1 or $s->{'-type'} == $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == -1 or $s->{"-type"} == $type) 2 0 2
Condition33%1/3
$type == -1 or $s->{"-type"} == $type
l!l&&r!l&&!r
200
MC/DC0%0/2
$type == -1 or $s->{"-type"} == $type
$type == -1 $s->{"-type"} == $type
Truth table33%1/3
A or B
A= $type == -1B= $s->{"-type"} == $type
ABresult
1X1
011
000
214 2 push @services, $s->{name};
215 }
216 }
217 1 return @services;
218 }
219  
220  
221 3 ▶sub _set_xml {
Subroutine _set_xml: called CC=1 SCAR=0.0
222 3 my ($self, $xml) = @_;
223 3 $self->{status_raw_content} = $xml;
224 }
225  
226  
227 4 ▶sub _get_xml {
Subroutine _get_xml: called CC=1 SCAR=0.0
228 4 my ($self) = @_;
229 4 return $self->{status_raw_content};
230 }
231  
232  
233 2 ▶sub service_status {
Subroutine service_status: called CC=17 SCAR=28.8
Pod documented
234 2 my ($self, $service) = @_;
235 2 my $status_href = {};
236  
237 2 $self->_fetch_info;
238  
239 2 for my $s (@{$self->{xml_hash}->{monit}->{service}}) {
240 4 ▶ if ($s->{name} eq $service) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"name"} eq $service) 2 2 4
241  
242 2 $status_href->{host} = $self->{hostname};
243  
244 $status_href->{'type'} = $s->{'-type'}
245 2 ▶ if exists $s->{'-type'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $s->{"-type"} 2 0 2
246  
247 2 for my $thing (qw/
248 children
249 collected_sec
250 collected_usec
251 euid
252 gid
253 group
254 monitor
255 monitormode
256 pid
257 ppid
258 name
259 pendingaction
260 status
261 status_hint
262 uid
263 uptime
264 /) {
265  
266 $status_href->{$thing} = $s->{$thing}
267 32 ▶ if exists $s->{$thing};
Branch: 1 branch
BranchTrueFalseTotal
if exists $s->{$thing} 12 20 32
268  
269 } # main stuff loop
270  
271 # the 'system' (type 5) service sticks these things in to ->{system}, others are top level
272 2 ▶ if (my $sys = $s->{system} || $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sys = $s->{"system"} || $s) 2 0 2
Condition33%1/3
$s->{'system'} || $s
l!l&&r!l&&!r
020
MC/DC0%0/2
$s->{'system'} || $s
$s->{'system'} $s
Truth table33%1/3
A || B
A= $s->{'system'}B= $s
ABresult
1X1
011
000
273 2 for my $thing (qw/ kilobyte kilobytetotal percent percenttotal /) {
274 $status_href->{memory}->{$thing} = $sys->{memory}->{$thing}
275 8 ▶ if exists $sys->{memory}->{$thing};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sys->{"memory"}{$thing} 6 2 8
276 } # memory loop
277  
278 2 for my $thing (qw/ kilobyte percent /) {
279 $status_href->{swap}->{$thing} = $sys->{swap}->{$thing}
280 4 ▶ if exists $sys->{swap}->{$thing};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sys->{"swap"}{$thing} 0 4 4
281 } # swap loop
282  
283 2 for my $thing (qw/ percent percenttotal /) {
284 $status_href->{cpu}->{$thing} = $sys->{cpu}->{$thing}
285 4 ▶ if exists $sys->{cpu}->{$thing};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sys->{"cpu"}{$thing} 2 2 4
286 } # cpu loop
287  
288 2 for my $thing (qw/ avg01 avg05 avg15 /) {
289 $status_href->{load}->{$thing} = $sys->{load}->{$thing}
290 6 ▶ if exists $sys->{load}->{$thing};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sys->{"load"}{$thing} 3 3 6
291 } # load loop
292  
293 }
294 }
295 }
296  
297 croak "Service $service does not exist\n"
298 2 ▶ unless scalar keys %{$status_href};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$status_href;} 0 2 2
299  
300 2 return $status_href
301  
302 }
303  
304  
305 0 ▶sub command_run {
Subroutine command_run: not called CC=4 SCAR=30.0
Pod documented
306 0 my ($self, $service, $command) = @_;
307  
308 croak "Don't understand this action\n"
309 0 ▶ unless grep { $command eq $_ } keys %{MONIT_ACTIONS()};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$command eq $_;} keys %{MONIT_ACTIONS();} 0 0 0
310  
311 0 ▶ if (not defined $service) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $service) 0 0 0
312 0 $self->{is_success} = 0;
313 0 croak "Service not specified\n";
314 }
315  
316 # if services does not exist throw error
317  
318 0 my $url = sprintf($COMMAND_URL_TEMPLATE, $self->{hostname}, $self->{port}, $service);
319  
320 0 my $res = $self->{ua}->post_form($url, { action => $command });
321 croak $res->{status}
322 0 ▶ unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 0 0 0
323  
324 0 return 1
325 }
326  
327 6 1; # End of Monit::HTTP
328  
329 __END__
↑ Summary