blib/lib/JMX/Jmx4Perl/Config.pm

filter: Statementstmt 73.6% 42 / 57 Branchbran 42.8% 12 / 28 Conditioncond 27.2% 3 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 75.0% 6 / 8 Podpod 100% 4 / 4 totaltotal 58.7% 67 / 114 SCAR 40.0
CC
22
Cov
59%
_extract_servers
23.7
new
10.8
get_servers
6.0
CRAP 54.5
« blib/lib/JMX/Jmx4Perl/Agent/Jolokia/Verifier.pm ↑ Summary blib/lib/JMX/Jmx4Perl/J4psh/Command.pm »
1 #!/usr/bin/perl
2 package JMX::Jmx4Perl::Config;
3 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 my $HAS_CONFIG_GENERAL;
6  
7 BEGIN {
8 3 ▶ eval {
Subroutine BEGIN: called CC=2 SCAR=7.1
9 3 require "Config/General.pm";
10 };
11 3 ▶ $HAS_CONFIG_GENERAL = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 3 3
12 }
13  
14 =head1 NAME
15  
16 JMX::Jmx4Perl::Config - Configuration file support for Jmx4Perl
17  
18 =head1 SYNOPSIS
19  
20 =over
21  
22 =item Configuration file format
23  
24 # ================================================================
25 # Sample configuration for jmx4perl
26  
27 # localhost is the name how this config could accessed
28 <Server localhost>
29 # Options for JMX::Jmx4Perl->new, case is irrelevant
30 Url = http://localhost:8080/j4p
31 User = roland
32 Password = test
33 Product = JBoss
34  
35 # HTTP proxy for accessing the agent
36 <Proxy>
37 Url = http://proxy:8001
38 User = proxyuser
39 Password = ppaasswwdd
40 </Proxy>
41 # Target for running j4p in proxy mode
42 <Target>
43 Url service:jmx:iiop://....
44 User weblogic
45 Password weblogic
46 </Target>
47 </Server>
48  
49 =item Usage
50  
51 my $config = new JMX::Jmx4Perl::Config($config_file);
52  
53 =back
54  
55  
56 =head1 DESCRIPTION
57  
58  
59 =head1 METHODS
60  
61 =over
62  
63 =item $cfg = JMX::Jmx4Perl::Config->new($file_or_hash)
64  
65 Create a new configuration object with the given file name. If no file name
66 is given the configuration F<~/.j4p> is tried. If the file does not
67 exist, C<server_config_exists> will alway return C<false> and
68 C<get_server_config> will always return C<undef>
69  
70 If a hash is given as argument, this hash is used to extract the server
71 information.
72  
73 =cut
74  
75 sub new {
76 1 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=23.8
Pod documented
77 1 my $file_or_hash = shift;
78 1 my $self = {};
79 1 my $config = undef;;
80 1 ▶ if (!ref($file_or_hash)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $file_or_hash) { } 1 0 1
elsif (ref $file_or_hash eq 'HASH') { } 0 0 0
81 1 ▶ my $file = $file_or_hash ? $file_or_hash : $ENV{HOME} . "/.j4p";
Branch: 1 branch
BranchTrueFalseTotal
$file_or_hash ? : 1 0 1
82 1 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 1 0 1
83 1 ▶ if ($HAS_CONFIG_GENERAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_CONFIG_GENERAL) { } 1 0 1
84 1 ▶ local $SIG{__WARN__} = sub {}; # Keep Config::General silent
Subroutine __ANON__: not called CC=1 SCAR=0.0
85 # when including things twice
86 1 $config = {
87 new Config::General(-ConfigFile => $file,-LowerCaseNames => 1,
88 -UseApacheInclude => 1,-IncludeRelative => 1, -IncludeAgain => 0,
89 -IncludeGlob => 1, -IncludeDirectories => 1, -CComments => 0)->getall
90 };
91 } else {
92 0 warn "Configuration file $file found, but Config::General is not installed.\n" .
93 "Please install Config::General, for the moment we are ignoring the content of $file\n\n";
94 }
95 }
96 } elsif (ref($file_or_hash) eq "HASH") {
97 0 $config = $file_or_hash;
98 } else {
99 0 die "Invalid argument ",$file_or_hash;
100 }
101 1 ▶ if ($config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config) 1 0 1
102 1 $self->{server_config} = &_extract_servers($config);
103 1 $self->{servers} = [ values %{$self->{server_config}} ];
104 1 map { $self->{$_} = $config->{$_ } } grep { $_ ne "server" } keys %$config;
105 #print Dumper($self);
106 }
107  
108 1 ▶ bless $self,(ref($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
109 1 return $self;
110 }
111  
112 =item $exists = $config->server_config_exists($name)
113  
114 Check whether a configuration entry for the server with name $name
115 exist.
116  
117 =cut
118  
119 sub server_config_exists {
120 1 ▶ my $self = shift;
Subroutine server_config_exists: called CC=3 SCAR=11.4
Pod documented
121 1 ▶ my $name = shift || die "No server name given to reference to get config for";
Condition50%1/2
shift() || die('No server name given to reference to get config for')
l!l
10
MC/DC0%0/1
shift() || die('No server name given to reference to get config for')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
122 1 my $cfg = $self->get_server_config($name);
123 1 ▶ return defined($cfg) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg ? : 1 0 1
124 }
125  
126 =item $server_config = $config->get_server_config($name)
127  
128 Get the configuration for the given server or C<undef>
129 if no such configuration exist.
130  
131 =cut
132  
133 sub get_server_config {
134 2 ▶ my $self = shift;
Subroutine get_server_config: called CC=3 SCAR=11.7
Pod documented
135 2 ▶ my $name = shift || die "No server name given to reference to get config for";
Condition50%1/2
shift() || die('No server name given to reference to get config for')
l!l
20
MC/DC0%0/1
shift() || die('No server name given to reference to get config for')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
136 2 ▶ return $self->{server_config} ? $self->{server_config}->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'server_config'} ? : 2 0 2
137 }
138  
139 =item $servers = $config->get_servers
140  
141 Get an arrayref to all configured servers or an empty arrayref.
142  
143 =cut
144  
145 sub get_servers {
146 0 ▶ my $self = shift;
Subroutine get_servers: not called CC=2 SCAR=17.9
Pod documented
147 0 ▶ return $self->{servers} || [];
Condition0%0/2
$self->{'servers'} || []
l!l
00
MC/DC0%0/1
$self->{'servers'} || []
$self->{'servers'}
Truth table0%0/2
A
A= $self->{'servers'}
Aresult
11
00
148 }
149  
150 sub _extract_servers {
151 1 ▶ my $config = shift;
Subroutine _extract_servers: called CC=9 SCAR=31.7
152 1 my $servers = $config->{server};
153 1 my $ret = {};
154 1 ▶ return $ret unless $servers;
Branch: 1 branch
BranchTrueFalseTotal
unless $servers 0 1 1
155 1 ▶ if (ref($servers) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $servers eq 'ARRAY') { } 0 1 1
elsif (ref $servers eq 'HASH') { } 1 0 1
156 # Its a list of servers using old style (no named section, but with
157 # embedded 'name'
158 0 for my $s (@$servers) {
159 0 ▶ die "No name given for server config " . Dumper($s) . "\n" unless $s->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"name"} 0 0 0
160 0 $ret->{$s->{name}} = $s;
161 }
162 0 return $ret;
163 } elsif (ref($servers) eq "HASH") {
164 1 for my $name (keys %$servers) {
165 2 ▶ if (ref($servers->{$name}) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $servers->{$name} eq 'HASH') { } 2 0 2
166 # A single, 'named' server section
167 2 $servers->{$name}->{name} = $name;
168 } else {
169 # It's a single server entry with 'old' style naming (e.g. no
170 # named section but a 'Name' property
171 0 my $ret = {};
172 0 ▶ my $name = $servers->{name} || die "Missing name for server section ",Dumper($servers);
Condition0%0/2
$servers->{'name'} || die('Missing name for server section ', Dumper($servers))
l!l
00
MC/DC0%0/1
$servers->{'name'} || die('Missing name for server section ', Dumper($servers))
$servers->{'name'}
Truth table0%0/2
A
A= $servers->{'name'}
Aresult
11
00
173 0 $ret->{$name} = $servers;
174 0 return $ret;
175 }
176 }
177 1 return $servers;
178 } else {
179 0 die "Invalid configuration type ",ref($servers),"\n";
180 }
181 }
182  
183 =back
184  
185 =head1 LICENSE
186  
187 This file is part of jmx4perl.
188  
189 Jmx4perl is free software: you can redistribute it and/or modify
190 it under the terms of the GNU General Public License as published by
191 the Free Software Foundation, either version 2 of the License, or
192 (at your option) any later version.
193  
194 jmx4perl is distributed in the hope that it will be useful,
195 but WITHOUT ANY WARRANTY; without even the implied warranty of
196 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
197 GNU General Public License for more details.
198  
199 You should have received a copy of the GNU General Public License
200 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
201  
202 A commercial license is available as well. Please contact roland@cpan.org for
203 further details.
204  
205 =head1 PROFESSIONAL SERVICES
206  
207 Just in case you need professional support for this module (or Nagios or JMX in
208 general), you might want to have a look at
209 http://www.consol.com/opensource/nagios/. Contact roland.huss@consol.de for
210 further information (or use the contact form at http://www.consol.com/contact/)
211  
212 =head1 AUTHOR
213  
214 roland@cpan.org
215  
216 =cut
217  
218 3 1;
« blib/lib/JMX/Jmx4Perl/Agent/Jolokia/Verifier.pm ↑ Summary blib/lib/JMX/Jmx4Perl/J4psh/Command.pm »