« Tomcat Management: Setting up the Tomcat Manager Application | Main | Tomcat Management: Using the JMXProxy »

05/08/2009

Tomcat Management: Use Groovy to Interact with Tomcat Manager

With the Tomcat Manager enabled, you can script a whole bunch of interesting ways to interact with your tomcat server. I've been using the HTTPBuilder module for Groovy a lot lately to get certain statistics from my Tomcat Manager enabled Tomcat servers. Below are some examples.

The first example is a basic script that gets ServerInfo from tomcat. It, and the following scripts, also demonstrate how to pass basic authentication over http requests with HTTPBuilder.

Get Tomcat ServerInfo Script

#!/usr/bin/env groovy 


package groovyx.net.http
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder( 'http://localhost:8080/manager/serverinfo' )
http.request( GET,TEXT ) { req ->
http.auth.basic( 'tomcat', 'tomcat' )
headers.'User-Agent' = 'GroovyHTTPBuilderTest/0.4'

response.success = { resp, reader ->
println "-----Response-----"
println System.out << reader
println "\n------------------"
}
response.failure = { resp ->
println "Something bad happened. ${resp.statusLine}"
}
}
Output will look similar to the following:
-----Response-----
OK - Server info
Tomcat Version: Apache Tomcat/6.0.18
OS Name: Mac OS X
OS Version: 10.5.6
OS Architecture: i386
JVM Version: 1.5.0_16-b06-284
JVM Vendor: Apple Inc.
org.codehaus.groovy.runtime.FlushingStreamWriter@2580b3

------------------
A second example will list all running applications.

Script to List All Running Applications

#!/usr/bin/env groovy 


package groovyx.net.http
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder( 'http://localhost:8080/manager/list')
http.request( GET,TEXT ) { req ->
http.auth.basic( 'tomcat', 'tomcat' )
headers.'User-Agent' = 'GroovyHTTPBuilderTest/0.4'

response.success = { resp, reader ->
println "-----Response-----"
println System.out << reader
println "\n------------------"
}
response.failure = { resp ->
println "Something bad happened. ${resp.statusLine}"
}
}
Output will look similar to the following:
-----Response-----
OK - Listed applications for virtual host localhost
/lenya:running:0:lenya
/examples:running:0:examples
/host-manager:running:0:host-manager
/docs:running:0:docs
/:running:0:ROOT
/manager:running:6:manager
org.codehaus.groovy.runtime.FlushingStreamWriter@2580b3

------------------

This final example polls the tomcat manager for a specific application, (in this case the /manager application) to report on session activity.

Script to Get Tomcat Session Info

#!/usr/bin/env groovy 


package groovyx.net.http
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder( 'http://localhost:8080/manager/sessions?path=/manager')
http.request( GET,TEXT ) { req ->
http.auth.basic( 'tomcat', 'tomcat' )
headers.'User-Agent' = 'GroovyHTTPBuilderTest/0.4'

response.success = { resp, reader ->
println "-----Response-----"
println System.out << reader
println "\n------------------"
}
response.failure = { resp ->
println "Something bad happened. ${resp.statusLine}"
}
}
Output will look similar to the following:
-----Response-----
OK - Session information for application at context path /manager
Default maximum session inactive interval 30 minutes
<1 minutes:1 sessions
1 - <2 minutes:2 sessions
4 - <5 minutes:1 sessions
11 - <12 minutes:1 sessions
>=30 minutes:1 sessions
org.codehaus.groovy.runtime.FlushingStreamWriter@2580b3

------------------
The next article in this series will discuss another hidden gem inside of Tomcat Manager: The JMXProxy.

TrackBack

TrackBack URL for this entry:
https://www.typepad.com/services/trackback/6a01156fbc6fe6970c0115722883f3970b

Listed below are links to weblogs that reference Tomcat Management: Use Groovy to Interact with Tomcat Manager:

Comments