I'm not sure about bash-like environment. You might try some simple wrapper programs in Java (with program arguments) that invoke your MBeans on the remote server. You can then call these wrappers from the shell script
If you can use something like Python or Perl, you might be interested in JSR-262 which allows you to expose JMX operations over web services. This is scheduled to be included in Java 7 but you might be able to use a release candidate of the reference implementation
You might want also to have a look at jmx4perl. It provides java-less access to a remote Java EE Server's MBeans. However, a small agent servlet needs to be installed on the target platform, which provides a restful JMX Access via HTTP with a JSON payload. (Version 0.50 will add an agentless mode by implementing a JSR-160 proxy).
Advantages are quick startup times compared to launching a local java JVM and ease of use. jmx4perl comes with a full set of Perl modules which can be easily used in your own scripts:
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Alias; # Import certains aliases for MBeans
print "Memory Used: ",
JMX::Jmx4Perl
->new(url => "http://localhost:8080/j4p")
->get_attribute(MEMORY_HEAP_USED);
You can also use alias for common MBean/Attribute/Operation combos (e.g. for most MXBeans).
For additional features (Nagios-Plugin, XPath-like access to complex attribute types, ...), please refer to the documentation of jmx4perl.
I've developed jmxfuse which exposes JMX Mbeans as a Linux FUSE filesystem with similar functionality as the /proc fs. It relies on Jolokia as the bridge to JMX. Attributes and operations are exposed for reading and writing.
A little risky, but you could run a curl POST command with the values from the form from the JMX console, its URL and http authentication (if required):
curl -s -X POST --user 'myuser:mypass'
--data "action=invokeOp&name=App:service=ThisServiceOp&methodIndex=3&arg0=value1&arg1=value1&submit=Invoke"
http://yourhost.domain.com/jmx-console/HtmlAdaptor
Beware: the method index may change with changes to the software. And the implementation of the web form could change.
The above is based on source of the JMX service page for the operation you want to perform:
@Dougnukem answer helped me a lot. I have taken the Groovy approach (using groovy 2.3.3).
I did some changes on Dougnukem code.
This will work with Java 7 and will print two attributes to stdout every 10 sec.
package com.my.company.jmx
import groovy.util.GroovyMBean;
import javax.management.remote.JMXServiceURL
import javax.management.remote.JMXConnectorFactory
import java.lang.management.*
class Monitor {
static main(args) {
def serverUrl = 'service:jmx:rmi:///jndi/rmi://localhost:5019/jmxrmi'
String beanName = "Catalina:type=DataSource,class=javax.sql.DataSource,name=\"jdbc/CommonDB\""
println "numIdle,numActive"
while(1){
def server = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl))
//make sure to reconnect in case the jvm was restrated
server.connect()
GroovyMBean mbean = new GroovyMBean(server.MBeanServerConnection, beanName)
println "${mbean.numIdle},${mbean.numActive}"
server.close()
sleep(10000)
}
}
}
Compile this code into a jar using maven-compiler-plugin so you will not require groovy installation only the groovy-all.jar .
Below is the relevant plugin definition and dependency.