Monitoring open socket

Enrico Olivelli eolivelli at gmail.com
Sat May 4 01:04:24 PDT 2013


Thank you for your example.
I would like the standard API (ot at least som hotspot specific API, 
like com.sun.management.UnixOperatingSystemMXBean) to let my app 
self-describe its network activity

A JMX approach will be really simple to use, but other solutions will be 
welcome

The API Socket.setSocketImplFactory lets you to intercept every 
SocketImpl creation but actually it is not so useful
because there is no way to create the original impl provided by the 
platform (PlainSocketImpl is package protected, inside java.net package)

for example I would like to write my factory:

   public static class MonitorSocketImplFactory implements 
java.net.SocketImplFactory {

         @Override
         public SocketImpl createSocketImpl() {
             SocketImpl socketImpl = ....create platform soccketimpl.....
             return new MyMonitoredSocketImpl(socketImpl);
         }
     }

(another little issue: looking inside the jdk7 implementation 
java.net.Socket I see that sometimes Socket.factory is not called to 
create the SocketImpl (as for SocksSocketImpl))

IMHO It would be useful to give a "java.net.SystemSocketImplFactory" 
that essentially creates the standard SocketImpl for the platform

or in class java.net.Socket {
...
public static SocketImplFactory getSystemSocketImplFactory { return ... 
implementation specific class.... }
....
}

- Enrico


Il 03/05/2013 23:34, Thomas Darimont ha scritto:
> Hello Enrico,
>
> I know that this is not JDK 8 specific but I think it solves your problem:
>
> One way to do this in a portable way would be to use the byteman 
> instrumentation
> tool from JBoss: http://www.jboss.org/byteman
>
> Byteman provides a rule language for bytecode manipulation via a 
> custom javaagent
> and allows to instrument jdk classes.
>
> So if you want to monitor sockets you could write rules like this:
>
> RULE Socket monitor connect event
> CLASS ^java.net.Socket
> METHOD connect(SocketAddress, int)
> AT EXIT
> IF TRUE
> DO 
> de.tutorials.training.net.SimpleSocketInspector.INSTANCE.beginTracking($0)
> ENDRULE
>
> RULE Socket monitor close event
> CLASS ^java.net.Socket
> METHOD close()
> AT CALL close
> IF TRUE
> DO 
> de.tutorials.training.net.SimpleSocketInspector.INSTANCE.endTracking($0)
> ENDRULE
>
> $0 means the "this" reference.
>
> In this example I exposed the socket monitoring information as a jmx 
> mbean for inspection
> (within de.tutorials.training.net.SimpleSocketInspector.INSTANCE) 
> accessible via visualvm.
>
> I hacked a quick prototype for this that works quite well :)
>
> This allows you to track Socket connect and close "events". If you 
> want to track activity for the
> individual sockets - you could instrument the Socket.getOutputStream() 
> / .getInputStream() Methods
> and return an appropriate wrapper that overrides the read(byte[],int,int)
> and write(byte[],int,int) methods in which you could maintain the 
> "latest socket activity timestamp" information.
>
> HTH
>
> Best regards,
> Thomas
>
>
> 2013/5/3 Enrico Olivelli <eolivelli at gmail.com 
> <mailto:eolivelli at gmail.com>>
>
>     Il 02/05/2013 22:44, Alan Bateman ha scritto:
>
>         On 02/05/2013 20:35, Enrico Olivelli wrote:
>
>             Hi,
>             In jdk7 there is no way to ask the jvm for the list of
>             actually active
>             sockets, witout jni or any other native non-portable solution.
>             Is there any plan to add a jmx set of beans to monitor jvm
>             network
>             activity  in a portable manner?
>             Thanks
>             Enrico
>
>         I don't know which operating system you are on but the JDK has
>         a platform-specific management interface named
>         com.sun.management.UnixOperatingSystemMXBean that allows the
>         number of open file descriptors to be monitored. This isn't
>         enumerating the open sockets so it might not be want you want.
>         Personally I would use lsof or /proc to look at the target VM
>         for information like this.
>
>         -Alan.
>
>     For Unix deployments I use lsof too but is not a portable solution
>     and I don't like my JVM to start external processes to look inside
>     itself
>     As you said UnixOperatingSystemMXBean  is useful only to get the
>     number of open FD
>     I would like a standard facility to list all open sockets, at least:
>     - endpoint addresses (local address and port and remote address
>     and port)
>     - connection status (ESTABLISHED, TIME_WAIT....)
>     - timestamp of creation...
>     - timestamp of last activity ?
>
>     thanks
>     Enrico
>
>
>
>



More information about the jdk8-dev mailing list