Recently, I've been spending a bit of time setting up Jolokia Proxy on a couple environments with different versions of Java and WebLogic.

I feel like Jolokia documentation is quite good, but does not provide any example or guide for Java newbies such as me. I guess I'm not the only one out and you might be stuck at some point setting all this up, so I'll try to summarize the steps necessary to have a working proxy.

For further reading and if you can understand French, I recommend this post that has helped me get things started.

Disclaimer: I really don't know much about Java, Jetty and the like. I'm writing this for anyone who is stuck and needs to get Jolokia to just work. Let me know if there are some improvements that you judge useful or necessary.

Environment

First, I chose to use Jetty to run Jolokia. You need to choose a version compatible with the Java JDK you have at your disposal. For example, according to this page, the latest version, Jetty 9, is only compatible with JDK 1.8. I have some servers still running JDK 1.7 and had a little surprise when trying to start Jetty: had to downgrade to Jetty 8. Anyway, this is fine for a simple tasks such has running Jolokia's WAR.

Installing Jetty

Jetty comes in an archive file. Pick a directory such as /opt/jetty, extract the archive there, and create a jetty user (unless you want to run Jetty as root). You'll also need to create /var/run/jetty:

adduser jetty
mkdir /var/run/jetty /opt/jetty
cd /opt/jetty
cp /tmp/jetty-distribution-9.4.31.v20200723.zip .
unzip jetty-distribution-9.4.31.v20200723.zip
ln -s jetty-distribution-9.4.31.v20200723/ current

I like to keep things organized, the currentsymlink can help having smoother upgrades, if necessary.
Then, copy or link the provided init.d script to the right place, and also copy the Jolokia WAR file:

ln -s /opt/jetty/current/bin/jetty.sh /etc/init.d/jetty
cp /tmp/jolokia-war-unsecured-1.6.2.war /opt/jetty/current/webapps/jolokia.war

And finally, give ownership to jettyuser for all these files:

chown -R jetty. /opt/jetty/ /var/run/jetty/

Configuration

At this point, Jetty should start correctly, but you probably need to configure a few things. First, a /etc/default/jetty file allows control over some parameters of Jetty, here's mine:

JETTY_HOME=/opt/jetty/current
JETTY_BASE=/opt/jetty/current
JETTY_USER=jetty
JETTY_PORT=8080
JETTY_HOST=127.0.0.1
# these two lines are only necessary if Java path is not defined in your global PATH
JAVA_HOME=/opt/jdk1.7.0_72_32bits
JAVA=$JAVA_HOME/bin/java
# only necessary for Jetty <= 8
JETTY_LOGS=/opt/jetty/current/logs/
/etc/default/jetty

If you change the default port, please note that you might have to change it in all Jetty's built-in files - if you don't, Jetty will try to use that port and will not start if it is already in use:

sed -i "s/8080/5000/g" /opt/jetty/current/etc/*.xml

The final touch is to declare Jolokia in Jetty configuration. Edit /opt/jetty/current/etc/webdefault.xmland add this block next to another servlet:

  <servlet>
    <servlet-name>jolokia-agent</servlet-name>
    <servlet-class>org.jolokia.http.AgentServlet</servlet-class>
    <init-param>
        <description>
            Class names (comma separated) of RequestDispatcher used in addition
            to the LocalRequestDispatcher
        </description>
        <param-name>dispatcherClasses</param-name>
        <param-value>org.jolokia.jsr160.Jsr160RequestDispatcher</param-value>
    </init-param>
  </servlet>
webdefault.xml

If you have authenticated JMX queries

I have spend way too many hours on this particular point, here's the trick: you need to copy your WebLogic wlclient.jar and wljmxclient.jar in Jetty libraries, otherwise you will get a org.omg.CORBA.NO_PERMISSIONerror. I believe these libraries are publicly available.

cp /tmp/wlclient.jar /tmp/wlkmxclient.jar /opt/jetty/current/lib/ext/

I have not tested if Jetty 9 handles correctly authenticated queries without this.

Start Jetty, start collecting

Just run service start jettyand it should fire up.
You can test Jolokia Proxy with a curl command such as (you can remove user and password, if not applicable):

curl -d '{"type" : "read", "mbean" : "java.lang:type=Memory", "attribute" : "HeapMemoryUsage", "path" : "used", "password":"xxxx", "user":"xxxx" }' -H "Content-Type: application/json" -X POST http://localhost:8080/jolokia/

You can now use Telegraf which supports Jolokia Proxy:

[[inputs.jolokia2_proxy]]
  url = "http://localhost:8080/jolokia"

  [[inputs.jolokia2_proxy.target]]
    url = "service:jmx:rmi:///jndi/rmi://server01:5100/jmxrmi"
  [[inputs.jolokia2_proxy.metric]]
    name = "test"
    mbean = "test.request:type=Monitoring,name=RequestStatus,visibility=public"
    paths = ["State"]
extract of telegraf.conf with dummy parameters

And that's it.