I used it for years if it can help:
public static void main(String[] a) throws Exception {
if (a.length != 2) {
System.out.println("put the objectname name as parameter:
<program> <host> <port>");
return;
}
JMXServiceURL url = new
JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + a[0] + ":" + a[1] +
"/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
Set<ObjectName> names = mbsc.queryNames(null, null);
for (ObjectName on : names) {
MBeanInfo infos = mbsc.getMBeanInfo(on);
System.out.println(on.getCanonicalName());
System.out.println("====================");
for (MBeanAttributeInfo info : infos.getAttributes()) {
String name = info.getName();
Object value;
try {
value = mbsc.getAttribute(on, name);
System.out.println("\t" + name + " = " + getValue(value));
} catch (Exception e) {
System.out.println("\t" + name + " = [unable to
get this attribute: " + e.getMessage() + "]");
}
}
System.out.println();
System.out.println();
}
}
private static String getValue(Object value) {
if (value instanceof Object[]) {
return getArrayValue((Object[]) value);
}
if (value.getClass().getName().startsWith("java.lang")) {
return value.toString();
}
if (value instanceof CompositeData) {
CompositeData datas = (CompositeData) value;
return datas.values().toString();
}
if (!(value instanceof String)) {
return new ToStringBuilder(value).toString();
}
return (String) value;
}
private static String getArrayValue(Object[] value) {
String[] strs = new String[value.length];
for (int i = 0; i < strs.length; i++) {
strs[i] = getValue(value[i]);
}
return Arrays.asList(strs).toString();
}
Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau
2013/11/7 Thiago Veronezi <[email protected]>:
> Cool... txk! :)
>
> I was playing a little bit here. If you want to see the attributes values,
> execute...
>
> //***********************************************************
> import java.lang.management.ManagementFactory
>
> def server = ManagementFactory.getPlatformMBeanServer()
> def beans = server.queryMBeans(null, null)
> println("We have ${(beans ? beans.size() : 0)} bean(s)")
>
> def getValue = { bean, attr ->
> try {
> return server.getAttribute(bean.objectName, attr.name)
> } catch (ignore) {
> return 'NA'
> }
> }
>
> def printAttributes = { bean ->
> def info = server.getMBeanInfo(bean.objectName)
> info?.getAttributes()?.each {
> println(" '${it.name}' = ${getValue(bean, it)}")
> }
> }
>
> beans?.each { bean ->
> println()
> println("Bean '${bean.objectName}'")
> println(" class: ${bean.className}")
> printAttributes(bean)
> }
> //***********************************************************
>
> https://dl.dropboxusercontent.com/u/1459144/tomee-list/mbeans_2.png
>
> Sometimes a scripting language like Groovy can be very handy. :)
>
> []s,
> Thiago.
>
>
>
> On Thu, Nov 7, 2013 at 9:41 AM, Howard W. Smith, Jr. <[email protected]
>> wrote:
>
>> +1 Thiago and Romain, good stuff!
>>
>>
>>
>> On Thu, Nov 7, 2013 at 9:21 AM, Romain Manni-Bucau <[email protected]
>> >wrote:
>>
>> > Hehe, the code is in sirona. You have the JMX local tree + you can see
>> > attributes and invoke basic operations. here is the server part
>> >
>> >
>> https://svn.apache.org/repos/asf/incubator/sirona/trunk/server/reporting/src/main/java/org/apache/sirona/reporting/web/plugin/jmx/JMXEndpoints.java
>> > . Views are
>> >
>> https://svn.apache.org/repos/asf/incubator/sirona/trunk/server/reporting/src/main/resources/templates/jmx/
>> > Romain Manni-Bucau
>> > Twitter: @rmannibucau
>> > Blog: http://rmannibucau.wordpress.com/
>> > LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> > Github: https://github.com/rmannibucau
>> >
>> >
>> >
>> > 2013/11/7 Thiago Veronezi <[email protected]>:
>> > > Out of curiosity, you can use the webaccess* to see some of that stuff
>> > via
>> > > jmx.
>> > > You just need to open the console and execute...
>> > >
>> > > // Groovy code
>> > > // ********************************************************************
>> > > import java.lang.management.ManagementFactory
>> > >
>> > > def server = ManagementFactory.getPlatformMBeanServer()
>> > > def beans = server.queryMBeans(null, null)
>> > > println("We have ${(beans ? beans.size() : 0)} bean(s)")
>> > >
>> > > beans?.each { bean ->
>> > > println()
>> > > println("---------------------------------------------")
>> > > println(" ${bean.objectName}: ${bean.className}")
>> > > }
>> > > // ********************************************************************
>> > >
>> > > https://dl.dropboxusercontent.com/u/1459144/tomee-list/mbeans.png
>> > >
>> > > ... or you can simply use jconsole.
>> > >
>> > > It would be nice to have a tree with all the mbeans... hmmm maybe we
>> will
>> > > have it. :)
>> > >
>> > > []s,
>> > > Thiago.
>> > >
>> > > webaccess*: oh boy... I still didn't rename it!
>> > >
>> > >
>> > >
>> > > On Thu, Nov 7, 2013 at 7:44 AM, Howard W. Smith, Jr. <
>> > [email protected]
>> > >> wrote:
>> > >
>> > >> +1 Romain, thanks.
>> > >>
>> > >> On Thu, Nov 7, 2013 at 7:30 AM, Romain Manni-Bucau <
>> > [email protected]
>> > >> >wrote:
>> > >>
>> > >> > well, before working on [monitoring] and launching sirona I
>> evaluated
>> > >> > hawtio. It is really nice but has big drawbacks which are blocking
>> to
>> > >> > be usable for me (I don't say it is a bad solution, just it doesn't
>> > >> > fit my needs):
>> > >> >
>> > >>
>> > >> does not fit my needs, too
>> > >>
>> > >>
>> > >> > 1) it is not Java (well the solutions are working but not entreprise
>> > >> > friendly IMO, in particular for monitoring where you don't want to
>> > >> > loose time learning a techno)
>> > >> >
>> > >>
>> > >> thanks! that says a lot!
>> > >>
>> > >>
>> > >> > 2) it is JMX based (JMX is important but there are a lot of other
>> > things)
>> > >> > 3) when I tested I didn't find how to aggregate servers (maybe you
>> can
>> > >> now)
>> > >> > 4) it is not *easily* pluggable (you can doing an overlay which is
>> not
>> > >> > a solution)
>> > >> > 5) there is no real agents (it is mainly a JMX gui)
>> > >> > 6) there is no counter for perfs
>> > >> >
>> > >> > surely few others...
>> > >> >
>> > >>
>> >
>>