2 Replies Latest reply on Aug 18, 2010 4:48 PM by erabeck

    How to write Jython scripts to communicate with MBeans

    maxdiconoclast

      Hi,
      I am working on JBoss Enterprise Application Platform(4.3.0) and need to write Jython scripts to fetch server monitoring data from JBoss MBeans.
      I am a newbee to both MBeans and Jython and just can't figure out a way to fetch required information like active thread count, JVM free memory etc. using Jython scripts.

      I know there could be tools like twiddle, JOPR etc. to do this for us, but the objective is to use command line scripts to extract such stats.

      Any help in this regard would be a life saver!

      Thanks a bunch in advance.

        • 1. Re: How to write Jython scripts to communicate with MBeans
          maxdiconoclast

          I seemed to have found the answer, what I was doing wrong was that, the RMI connxn was not being initialized correctly to the server.
          Below is a small snippet which worked for me.

          from java.util import Properties
          from java.lang import System
          from javax.naming import InitialContext
          from javax.management import NotificationListener,ObjectName,MBeanInfo,MBeanAttributeInfo

          class ClientListener(NotificationListener):
          print "Will it work :("
          props = Properties(System.getProperties())
          props.put("java.naming.provider.url", "jnp://localhost:1099")
          props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory")
          props.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces")
          ctx = InitialContext(props)
          adapterName = "jmx/invoker/RMIAdaptor"
          server = ctx.lookup(adapterName)
          ctx.close()
          name = ObjectName("jboss:service=JNDIView")
          info = server.getMBeanInfo(name)
          print "JNDIView Class: " + info.getClassName()
          print " "
          aValue = server.getAttribute(name,"HANamingService")
          print "Value of HANamingService is::: " + aValue
          print "It worked Bye Bye! :)"

          +++++++++++++++++++++++++++++++++++++++++++++

          This made the communication with the MBean server and fetched me a dummy value of the configured MBean.

          • 2. Re: How to write Jython scripts to communicate with MBeans
            erabeck

            Manav,

             

            This is great. I've been considering doing some similar monitoring and would rather wrap things in a single script than calling twiddle.sh over and over again. Do you have any more code you'd be willing to share and/or links to the resources you used to get this far?

             

            Thanks,

            --Evan