Menu

Home

Maciek Kolbusz

JmxProxyClient Wiki

JmxProxyClient is an Object Oriented interface for JmxProxy which is a part of the manager app in Tomcat. It offers a quick and easy access to MBeans via HTTP and can be used in shell scripts, performance graphing apps, monitoring apps etc. Currently it's only available in Perl.

Releases

0.1

Features in this release:
+ JmxProxyClient class supports qry requests with wildcards returning array of MBeans as a result (search method)
+ JmxProxyClient class supports qry requests without wildcards returing a MBean objects as a result (mbean method)
+ MBean class supports get requests for specific attributes which only work if MBean was fully created or through JmxProxyClient::mbean method

TODOs:
+ set support
+ error handling

Examples

Getting all MBeans

use JmxProxyClient;
use JmxProxyClient::MBean;

# Create a new instance of JmxProxyClient
my $jpc = new JmxProxyClient(
                server  => 'localhost',
                port    => 8080,
                user    => 'man',
                password=> 'man123',
);

# Fetch all available MBeans
my @allBeans = $jpc->get_all_mbeans;
for my $mbean (@allBeans) {
    print $mbean->name . "\n";
    my %attrs = $mbean->get_attributes;
    while (my ($attr, $val) = each (%attrs)) {
        print "\t" . $attr . ": " . $val . "\n";
    }
}

Searching for MBeans, printing MBean attributes

use JmxProxyClient;
use JmxProxyClient::MBean;

# Create a new instance of JmxProxyClient
my $jpc = new JmxProxyClient(
                server  => 'localhost',
                port    => 8080,
                user    => 'man',
                password=> 'man123',
);

# Run a query and display results
my @mbeans = $jpc->search("Catalina:type=ThreadPool,*");
for my $mbean (@mbeans) {
    print $mbean->name . "\n";
    my %attrs = $mbean->attributes;
    while (my ($attr, $val) = each (%attrs)) {
        print "\t" . $attr . ": " . $val . "\n";
    }
    print "\n\n";
    printf "Busy/Current = %d/%d = %.2f\n",
           $attrs{currentThreadsBusy},
           $attrs{currentThreadCount},
           $attrs{currentThreadsBusy} / $attrs{currentThreadCount};
    print "\n\n";
}

Getting memory information

use strict;
use JmxProxyClient;
use JmxProxyClient::MBean;

# Create a new instance of JmxProxyClient
my $jpc = new JmxProxyClient(
                server  => 'localhost',
                port    => 8080,
                user    => 'man',
                password=> 'man123',
);

my $mem = "java.lang:type=Memory";

# Using hash of results
my %memstats = $jpc->mbean($mem)->heap_mem_usage;
while (my($key, $val) = each(%memstats)) {
    printf "\t%s\t=%d\n", $key, $val;
}

# Querying individual mem areas
foreach ((qw/committed max used init/)) {
   printf "\t%20s= %d\n", $_, $jpc->mbean($mem)->heap_mem_usage($_);
}

Sending metrics to Ganglia

use strict;
use JmxProxyClient;
use JmxProxyClient::MBean;

# Create a new instance of JmxProxyClient
my $jpc = new JmxProxyClient(
                server  => 'localhost',
                port    => 8080,
                user    => 'man',
                password=> 'man123',
);

# Run a query and display results
my $gm_prefix = "tomcat_";
my $mbean_name = "Catalina:type=ThreadPool,name=http-8080";
while (1) {
    foreach (("currentThreadsBusy", "currentThreadCount")) {
        my %metric = (
                name    => $gm_prefix . $mbean_name . "_" . $_,
                type    => "int32",
                units   => "threads",
                value   => $jpc->mbean($mbean_name)->get($_)
        );
        exec(gmetric(%metric));
    }

    sleep 15;
}

sub gmetric {
    my %params = @_;
    # Replace illegal chars with _
    $params{'name'} =~ s/[^a-zA-Z0-9_]/_/g;
    my $cmd = "/usr/bin/gmetric --name='%s' --type='%s' --units='%s' --value='";
    # Set the correct format for the value
    if ($params{type} =~ /.*int.*/)  { $cmd .= "%d'"; }
    elsif ($params{type} =~ /(float)|(double)/)  { $cmd .= "%s'"; }
    else { $cmd .= "%s'"; }
    return sprintf $cmd, $params{'name'}, $params{'type'}, $params{'units'}, $params{'value'};
}