Archive

Posts Tagged ‘jmx’

Configure glassfish via JMX and Groovy

May 19th, 2009 1 comment

At my new work, we have a lot of glassfish in production. For some of them, we even have almost 10 instances per DAS (Domain Admin Server). As a (good) system administrator, I’m a lazy man. After having to installed and created a glassfish with 10 instances, I was really tired to click, click, and click again through the web interface. Yes I know, it’s possible to edit the files directly, but I think it’s not very convenient. First, it’s easy to make mistakes, it’s not atomic, and finally you need to restart/reload the instance.

So, I was wondering if it’s possible to proceed some configuration using JMX/AMX (AppServer Management Extensions). I start to browse MBeans using JConsole, and check that’s really possible to modify the configuration, and the answer is yes. Ok, my first try is a simple one, I just want enable access.log, meaning set accessLoggingEnabled property to true for each http-service. Since I’m not really good to write Java, I wrote a groovy script. Here it comes :

 
import javax.management.ObjectName
import javax.management.remote.JMXConnectorFactory as JmxFactory
import javax.management.remote.JMXServiceURL as JmxUrl
import javax.management.MBeanServerConnection
 
def uri = 'service:jmx:rmi:///jndi/rmi://localhost:8686/jmxrmi'
def login = 'admin'
def password = 'adminadmin'
 
import javax.management.Query
 
def jmxurl = new JmxUrl(uri)
def attributes = new Hashtable()
 
def buffer = [ login, password ]
 
attributes.put("jmx.remote.credentials", (String[]) buffer)
 
def server = JmxFactory.connect(jmxurl, attributes).MBeanServerConnection
 
def enableAccessLog = { serviceUri ->
    properties = [ "accessLoggingEnabled", "true" ]
    def signature = [ "java.lang.String", "java.lang.String"]
    server.invoke(serviceUri, "setPropertyValue", properties as Object[], signature as String[] );
}
 
def checkAccessLog = { serviceUri ->
    def properties = [ "accessLoggingEnabled" ]
    def answer = server.invoke(serviceUri, "getPropertyValue", properties as Object[], "java.lang.String");
    println "Access log for: " + serviceUri + " enabled: " + answer
}
 
 
def query = new ObjectName("amx:*,j2eeType=X-HTTPServiceConfig")
 
server.queryNames(query, null).each { service ->
    checkAccessLog(service)
    enableAccessLog(service)
    checkAccessLog(service)
}

That’s magic… Sure I’ll post some others script to manage more complex configuration, like create a connection pool, etc.

Categories: Java, Sysadmin Tags: , ,