| 1 | #!/usr/bin/perl | ||||||||||||||||||||
| 2 | package JMX::Jmx4Perl::Config; | ||||||||||||||||||||
| 3 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 5 | my $HAS_CONFIG_GENERAL; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 5 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 8 | 5 | eval { | |||||||||||||||||||
| 9 | 5 | require "Config/General.pm"; | |||||||||||||||||||
| 10 | }; | ||||||||||||||||||||
| 11 | 5 | ▶ | $HAS_CONFIG_GENERAL = $@ ? 0 : 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 is | ||||||||||||||||||||
| 66 | given the configuration F<~/.j4p> is tried. In case the given file is a | ||||||||||||||||||||
| 67 | directory, a file F<dir/jxm4perl.cfg> is tried as configuration file. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | If the file does not exist, C<server_config_exists> will alway return C<false> and | ||||||||||||||||||||
| 70 | C<get_server_config> will always return C<undef> | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | If a hash is given as argument, this hash is used to extract the server | ||||||||||||||||||||
| 73 | information. | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=25.1
Pod
documented
| |||||||||||||||||||||
| 78 | 2 | my $class = shift; | |||||||||||||||||||
| 79 | 2 | my $file_or_hash = shift; | |||||||||||||||||||
| 80 | 2 | my $self = {}; | |||||||||||||||||||
| 81 | 2 | my $config = undef;; | |||||||||||||||||||
| 82 | 2 | ▶ | if (!ref($file_or_hash)) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 83 | 2 | ▶ | my $file = $file_or_hash ? $file_or_hash : $ENV{HOME} . "/.j4p"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 2 | ▶ | $file = $file . "/jmx4perl.cfg" if -d $file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 2 | ▶ | if (-e $file) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 86 | 1 | ▶ | if ($HAS_CONFIG_GENERAL) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 1 | ▶ | local $SIG{__WARN__} = sub {}; # Keep Config::General silent | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 88 | # when including things twice | ||||||||||||||||||||
| 89 | 1 | $config = { | |||||||||||||||||||
| 90 | new Config::General(-ConfigFile => $file,-LowerCaseNames => 1, | ||||||||||||||||||||
| 91 | -UseApacheInclude => 1,-IncludeRelative => 1, -IncludeAgain => 0, | ||||||||||||||||||||
| 92 | -IncludeGlob => 1, -IncludeDirectories => 1, -CComments => 0)->getall | ||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||
| 95 | 0 | warn "Configuration file $file found, but Config::General is not installed.\n" . | |||||||||||||||||||
| 96 | "Please install Config::General, for the moment we are ignoring the content of $file\n\n"; | ||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } elsif (-d $file) { | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | } elsif (ref($file_or_hash) eq "HASH") { | ||||||||||||||||||||
| 102 | 0 | $config = $file_or_hash; | |||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||
| 104 | 0 | die "Invalid argument ",$file_or_hash; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | 2 | ▶ | if ($config) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 1 | $self->{server_config} = &_extract_servers($config); | |||||||||||||||||||
| 108 | 1 | $self->{servers} = [ values %{$self->{server_config}} ]; | |||||||||||||||||||
| 109 | 0 | map { $self->{$_} = $config->{$_ } } grep { $_ ne "server" } keys %$config; | |||||||||||||||||||
| 110 | #print Dumper($self); | ||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 2 | ▶ | bless $self,(ref($class) || $class); | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 114 | 2 | return $self; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =item $exists = $config->server_config_exists($name) | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | Check whether a configuration entry for the server with name $name | ||||||||||||||||||||
| 120 | exist. | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 1 | ▶ | sub server_config_exists { | ||||||||||||||||||
|
Subroutine
server_config_exists: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||
| 125 | 1 | my $self = shift; | |||||||||||||||||||
| 126 | 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')
MC/DC0%0/1
shift() || die('No server name given to reference to get config for')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 127 | 1 | my $cfg = $self->get_server_config($name); | |||||||||||||||||||
| 128 | 1 | ▶ | return defined($cfg) ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =item $server_config = $config->get_server_config($name) | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | Get the configuration for the given server or C<undef> | ||||||||||||||||||||
| 134 | if no such configuration exist. | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 2 | ▶ | sub get_server_config { | ||||||||||||||||||
|
Subroutine
get_server_config: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||
| 139 | 2 | my $self = shift; | |||||||||||||||||||
| 140 | 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')
MC/DC0%0/1
shift() || die('No server name given to reference to get config for')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 141 | 2 | ▶ | return $self->{server_config} ? $self->{server_config}->{$name} : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =item $servers = $config->get_servers | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | Get an arrayref to all configured servers or an empty arrayref. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | ▶ | sub get_servers { | ||||||||||||||||||
|
Subroutine
get_servers: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 151 | 0 | my $self = shift; | |||||||||||||||||||
| 152 | 0 | ▶ | return $self->{servers} || []; | ||||||||||||||||||
|
Condition0%0/2
$self->{'servers'} || []
MC/DC0%0/1
$self->{'servers'} || []
$self->{'servers'}
Truth table0%0/2
A
A= $self->{'servers'}
| |||||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 1 | ▶ | sub _extract_servers { | ||||||||||||||||||
|
Subroutine
_extract_servers: called CC=9 SCAR=31.7
| |||||||||||||||||||||
| 156 | 1 | my $config = shift; | |||||||||||||||||||
| 157 | 1 | my $servers = $config->{server}; | |||||||||||||||||||
| 158 | 1 | my $ret = {}; | |||||||||||||||||||
| 159 | 1 | ▶ | return $ret unless $servers; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 160 | 1 | ▶ | if (ref($servers) eq "ARRAY") { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 161 | # Its a list of servers using old style (no named section, but with | ||||||||||||||||||||
| 162 | # embedded 'name' | ||||||||||||||||||||
| 163 | 0 | for my $s (@$servers) { | |||||||||||||||||||
| 164 | 0 | ▶ | die "No name given for server config " . Dumper($s) . "\n" unless $s->{name}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | 0 | $ret->{$s->{name}} = $s; | |||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | 0 | return $ret; | |||||||||||||||||||
| 168 | } elsif (ref($servers) eq "HASH") { | ||||||||||||||||||||
| 169 | 1 | for my $name (keys %$servers) { | |||||||||||||||||||
| 170 | 2 | ▶ | if (ref($servers->{$name}) eq "HASH") { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | # A single, 'named' server section | ||||||||||||||||||||
| 172 | 2 | $servers->{$name}->{name} = $name; | |||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||
| 174 | # It's a single server entry with 'old' style naming (e.g. no | ||||||||||||||||||||
| 175 | # named section but a 'Name' property | ||||||||||||||||||||
| 176 | 0 | my $ret = {}; | |||||||||||||||||||
| 177 | 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))
MC/DC0%0/1
$servers->{'name'} || die('Missing name for server section ', Dumper($servers))
$servers->{'name'}
Truth table0%0/2
A
A= $servers->{'name'}
| |||||||||||||||||||||
| 178 | 0 | $ret->{$name} = $servers; | |||||||||||||||||||
| 179 | 0 | return $ret; | |||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | 1 | return $servers; | |||||||||||||||||||
| 183 | } else { | ||||||||||||||||||||
| 184 | 0 | die "Invalid configuration type ",ref($servers),"\n"; | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =back | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =head1 LICENSE | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | This file is part of jmx4perl. | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | Jmx4perl is free software: you can redistribute it and/or modify | ||||||||||||||||||||
| 195 | it under the terms of the GNU General Public License as published by | ||||||||||||||||||||
| 196 | the Free Software Foundation, either version 2 of the License, or | ||||||||||||||||||||
| 197 | (at your option) any later version. | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | jmx4perl is distributed in the hope that it will be useful, | ||||||||||||||||||||
| 200 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||
| 201 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||
| 202 | GNU General Public License for more details. | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | You should have received a copy of the GNU General Public License | ||||||||||||||||||||
| 205 | along with jmx4perl. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | A commercial license is available as well. Please contact roland@cpan.org for | ||||||||||||||||||||
| 208 | further details. | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | =head1 PROFESSIONAL SERVICES | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | Just in case you need professional support for this module (or Nagios or JMX in | ||||||||||||||||||||
| 213 | general), you might want to have a look at | ||||||||||||||||||||
| 214 | http://www.consol.com/opensource/nagios/. Contact roland.huss@consol.de for | ||||||||||||||||||||
| 215 | further information (or use the contact form at http://www.consol.com/contact/) | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =head1 AUTHOR | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | roland@cpan.org | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | =cut | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | 5 | 1; | |||||||||||||||||||