Automating JMX calls using JMXTerm

On the project I’m currently working on, we have a lot of tasks which are exposed through JMX and which we use very often. Some tasks need to be executed on a daily base, some even more often. As we all know, repetitive task are the ones we should automate and so I did.

 

 

Since I didn’t feel like writing another Java application just to call a Java application over JMX, I did some searches for a good command line JXM application and so I found JMXTerm. After fiddling around a bit, I wrote the following script which will connect to an Oracle XE database, retreive some data and use this data to call a JMX method which accepts a couple of parameters (many of which are just 0).

 

 

# prepare Oracle connection
. /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh

# Find the exact bean name
BEAN="$(java -jar jmxterm-1.0-alpha4.jar s -l service:jmx:rmi:///jndi/rmi://localhost:2000/myjmx -n -i discover | grep "process=MyBeanProcess,type=MyProcess")"
echo $BEAN

# Variable I'll use to query the database
BPS="40706 42001 41038 40705 40186"
for bp in $BPS; do
        # send commands to sqlplus, spool results to a file output.out
        echo "SPOOL output.out;
        SELECT REF_ID FROM MYTABLE;
        SPOOL OFF;
        exit;" | sqlplus sys/oracle@localhost as SYSDBA

        # extract the id's from the (human readable) output
        DATA=$(cat output.out | egrep "^[ ]{5}[0-9]+" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')

        # create an input script for jmxterm
        echo bean $BEAN > tmpJmxScript
        for x in $DATA; do
                echo run myJmxMethod 0 "$bp" 0 0 "${x}" 0 >> tmpJmxScript
        done;

        # connect to JMX server and execute the previously generated script
        java -jar jmxterm-1.0-alpha4.jar s -l service:jmx:rmi:///jndi/rmi://localhost:2000/myjmx -i tmpJmxScript -n
done;

The tmpJmxScript file will look something like this

bean Processes:name=MyBeanProcess,Id=2,process=MyBeanProcess,type=MyProcess
run myJmxMethod 0 40186 0 0 15907 0
run myJmxMethod 0 40186 0 0 16340 0
run myJmxMethod 0 40186 0 0 16496 0

This script tells JMXTerm to use the bean ‘MyBeanProcess’ and execute the myJmxMethod JMX method using the parameters that follow.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s