jmx/jstatd access to remote machine through an ssh tunnel

南楼画角 提交于 2019-12-05 01:03:18

JMX/RMI is hard to tunnel directly because if RMI. Basically the server creates an RMI stub definition which is armed with directions to connect back to the server from whence it came, but when you're tunelling, the stubs come down from the server, but their directions are all wrong, and they can't get there from here.

The waaay easiest way to resolve this is to ditch the RMI connector and use JMXMP. The underlying protocol is pure sockets so it's perfectly adapted for tunneling.

Use SSH tunnel with SOCKS proxy. See this post for more details.

I just did this two minutes ago ...

  • Remote server with jmx on port 8686 - this port is locked down
  • sshd_config on the remote box should have 'AllowTcpForwarding yes' or commented out
  • Open a putty ssh session with port forwarding local port 8686 (or whatever) to localhost:8686
  • Add a local jmx connection in VisualVm pointing at your local forwarded port
  • VisualVm automagically sees the jvm and starts monitoring

Here are the steps that worked for me:

  1. Launch an ejstatd in your remote host this way (in ejstatd folder): mvn exec:java -Dexec.args="-pr 2000 -ph 2001 -pv 2002" (used for "jstatd" type connection)
  2. Launch your Java application with those additional Java parameters: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=2003 -Dcom.sun.management.jmxremote.rmi.port=2003 (used for "JMX" type connection)
  3. Open an SSH session to the remote host tunneling those 4 ports (from 2000 to 2003 included). For example with OpenSSH client, you must add those parameters: -L2000:localhost:2000 -L2001:localhost:2001 -L2002:localhost:2002 -L2003:localhost:2003
  4. Launch JVisualVM
    1. Right-click on "Local" > "Add jstatd Connection..." > "Add Custom" and enter "2000" in "Port" selection;
    2. Right-click on "Local" > "Add JMX Connection..." and enter "localhost:2003" in "Connection" input, and check "Do not require SSL connection"
    3. Your Java process will appear twice: one from the "jstatd" connection type, and one from the "JMX" connection type.

Disclaimer: I'm the author of the open source ejstatd tool.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!