title: jmx/jstatd access to remote machine through an ssh tunnel
body: <p>I'd like to use visualvm app through an ssh tunnel (to an EC2 machine) using jmx OR jstatd. How do I do this? Here is a list of what has been tried (and failed):
  (BTW: if visual vm is not appropriate, how do I find memory leaks on a remote machine?)</p>
  
  <h2>jstatd: attempt:</h2>
  
  <p>I set up the jstatd server on the EC2 machine (app was already running on it)
  Then I set up a tunnel mapping local port 3333 to remote port 1099
  In VisualVM I tried to connect using jstatd on port 3333
  ... none of the processes on EC2 showed up</p>
  
  <p>This link says that jstatd opens up another port:
  <a href="http://rukuro-blog.heroku.com/2011/06/30/monitoring-remote-java-applications-with-visualvm" rel="noreferrer">http://rukuro-blog.heroku.com/2011/06/30/monitoring-remote-java-applications-with-visualvm</a>
  ... so I tunneled that port from local host to remote. 
  I restarted visual vm ... still nothing</p>
  
  <p>When I tried listing processes running from EC2 using the folloing command:</p>
  
  <pre><code>jps -l -m -v rmi://localhost
  </code></pre>
  
  <p>... I got a list of processes </p>
  
  <p>When I listed it on my home machine using</p>
  
  <pre><code>jps -l -m -v rmi://localhost:3333
  </code></pre>
  
  <p>.... I got none ! So, is the rmi port not being tunnelled with jstatd port (3333) ... ?</p>
  
  <h2>the jmx attempt:</h2>
  
  <p>I started the application on the remote machine with the following command:</p>
  
  <pre><code>java -Dcom.sun.management.jmxremote.port=3333 \
       -Dcom.sun.management.jmxremote.ssl=false \
       -Dcom.sun.management.jmxremote.authenticate=false\
          -cp :post/* &lt;appName&gt;
  </code></pre>
  
  <p>... the app works so I tunnelled local port 3333 to remote 3333</p>
  
  <p>Then I tried setting up a jmx link to localhost:3333 from visual vm interface:
  ... it pops up an error saying: </p>
  
  <pre><code>cannot connect to localhost:3333 using service:jmx:rmi:///jndi/rmi://localhost:3333/jmxrmi
  </code></pre>
  
  <p>If I need to set up a link to the remove RMI server I'm not sure how to do it.</p>
  
  <h2>JMXMP attempt:</h2>
  
  <p>This holds promise but something is not quite right:</p>
  
  <p>First I added the jmx_remote jar to the classpath and started the application on the remote machine using the same command as in the JMX case I showed above. I found the jar at <a href="http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html" rel="noreferrer">oracle download link</a></p>
  
  <p>I setup up an ssh tunnel from local port 3333 to remote port 3333. Then I started up visualvm with the same jmx_remote file in the calsspath.</p>
  
  <pre><code>visualvm -cp:a ~/jmx/jmxremote_optional.jar
  </code></pre>
  
  <p>Then I tried to connect visualvm to the remote server with:</p>
  
  <pre><code>service:jmx:jmxmp://localhost:3333
  </code></pre>
  
  <p>Now visual vm seems to try to connect indefinitely. It shows "Adding service:jmx:jmxp://localhost:3333" in the status bar ... and continutes to do so till I shutdown the application on the remote end at which time it throws a popup saying that it couldnt' connect with the server.</p>
  
  <h2>On using SOCKS:</h2>
  
  <p>My attempts at using both JMX and jstatd failed. </p>
  
  <p>I'm not sure the SOCKS proxy is working, so here is how I tried to connect:</p>
  
  <pre><code>ssh -i ~/.ssh/starter.pem -v -D 9696 user@host
  </code></pre>
  
  <p>Just for completeness, I started the command on the other end with:</p>
  
  <pre><code>    java -Dcom.sun.management.jmxremote\
   -Dcom.sun.management.jmxremote.ssl=false\
   -Dcom.sun.management.jmxremote.authenticate=false\
   -Dcom.sun.management.jmxremote.port=3333 &lt;app&gt;
  </code></pre>
  
  <p>And on the local end I set up the visualvm connection as per this <a href="http://theholyjava.wordpress.com/2012/09/21/visualvm-monitoring-remote-jvm-over-ssh-jmx-or-not/" rel="noreferrer"><em>LINK to VisualVm help page</em></a>.</p>
  
  <p>With the JMX connection, I set the machine name and the port by right clicking the host on visualvm and fillng in the host port when it prompts me. At this point the status bar at the bottom shows visualvm is trying to connect to the remote machine and after a couple of minutes it fails.</p>
  
  <p>With jstatd connection, I expected the remote processes to show up automatically. This didn't happen ... there were not error messages or anything.</p>
  
  <p>On the socks side the following messages repeat cyclically:</p>
  
  <pre><code>debug1: Connection to port 9696 forwarding to socks port 0 requested.
  debug1: channel 2: new [dynamic-tcpip]
  channel 3: open failed: connect failed: Connection timed out
  debug1: channel 3: free: direct-tcpip: listening port 9696 for 50.16.35.69 port 3333, connect from 127.0.0.1 port 43909, nchannels 4
  debug1: Connection to port 9696 forwarding to socks port 0 requested.
  debug1: channel 3: new [dynamic-tcpip]
  channel 2: open failed: connect failed: Connection timed out
  debug1: channel 2: free: direct-tcpip: listening port 9696 for 50.16.35.69 port 1099, connect from 127.0.0.1 port 44644, nchannels 4
  </code></pre>
  
  <p>I wonder if this has to do with the socks channel. I have tunnelled single ports to the remote machine before, so I cant imagine it being a configuration issue for tunneling. I'm running ubuntu linux on both sides.</p>
  
  <p>Thanks for reading :)</p>