[SIGAR-4] switch to native version of NetStat.stat

This commit is contained in:
Doug MacEachern 2006-07-11 00:40:06 +00:00
parent 3c17c4974e
commit 00058b3de6
2 changed files with 7 additions and 148 deletions

View File

@ -824,7 +824,7 @@ JNIEXPORT jobjectArray SIGAR_JNIx(getNetConnectionList)
return connarray; return connarray;
} }
JNIEXPORT void SIGAR_JNI(NetStat_nstat) JNIEXPORT void SIGAR_JNI(NetStat_stat)
(JNIEnv *env, jobject obj, jobject sigar_obj, jint flags) (JNIEnv *env, jobject obj, jobject sigar_obj, jint flags)
{ {
int status; int status;
@ -843,6 +843,7 @@ JNIEXPORT void SIGAR_JNI(NetStat_nstat)
cls = JENV->GetObjectClass(env, obj); cls = JENV->GetObjectClass(env, obj);
JAVA_SIGAR_INIT_FIELDS_NETSTAT(cls);
JAVA_SIGAR_SET_FIELDS_NETSTAT(cls, obj, netstat); JAVA_SIGAR_SET_FIELDS_NETSTAT(cls, obj, netstat);
if (sizeof(tcp_states[0]) == sizeof(netstat.tcp_states[0])) { if (sizeof(tcp_states[0]) == sizeof(netstat.tcp_states[0])) {

View File

@ -1,25 +1,13 @@
package org.hyperic.sigar; package org.hyperic.sigar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class NetStat { public class NetStat {
private Sigar sigar; protected Sigar sigar;
private Map tcpInbound;
private Map tcpOutbound;
private int[] tcpStates;
private int tcpInboundTotal, tcpOutboundTotal;
//XXX not implemented on all platforms yet
private static final boolean hasNstat =
"true".equals(System.getProperty("sigar.netstat.native"));
private native void nstat(Sigar sigar, int flags) throws SigarException; protected int[] tcpStates;
protected int tcpInboundTotal, tcpOutboundTotal;
public NetStat() { public native void stat(Sigar sigar, int flags) throws SigarException;
this.sigar = new Sigar();
}
public NetStat(Sigar sigar) throws SigarException { public NetStat(Sigar sigar) throws SigarException {
this.sigar = sigar; this.sigar = sigar;
@ -28,107 +16,7 @@ public class NetStat {
NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT | NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT |
NetFlags.CONN_TCP; NetFlags.CONN_TCP;
if (hasNstat) { stat(sigar, flags);
nstat(sigar, flags);
}
else {
stat(flags);
}
}
public static int[] newTcpStateArray() {
int[] states = new int[NetFlags.TCP_UNKNOWN];
for (int i=0; i<states.length; i++) {
states[i] = 0;
}
return states;
}
public static class IpEntry {
int[] states = newTcpStateArray();
int count=0, inbound=0, outbound=0;
public String toString() {
return "count:" + this.count;
}
}
public void stat(int flags) throws SigarException {
this.tcpInbound = new HashMap();
this.tcpOutbound = new HashMap();
this.tcpInboundTotal = this.tcpOutboundTotal = 0;
this.tcpStates = newTcpStateArray();
NetConnection[] connections =
this.sigar.getNetConnectionList(flags);
for (int i=0; i<connections.length; i++) {
NetConnection conn = connections[i];
int state = conn.getState();
this.tcpStates[state]++;
//first pass, get listening port numbers
if (state == NetFlags.TCP_LISTEN) {
Long port = new Long(conn.getLocalPort());
Map addresses = (Map)this.tcpInbound.get(port);
if (addresses == null) {
addresses = new HashMap();
this.tcpInbound.put(port, addresses);
}
}
}
//second pass, get addresses connected to listening ports
for (int i=0; i<connections.length; i++) {
NetConnection conn = connections[i];
int state = conn.getState();
if (state == NetFlags.TCP_LISTEN) {
continue;
}
Long port = new Long(conn.getLocalPort());
Map addresses = (Map)this.tcpInbound.get(port);
String ip = conn.getRemoteAddress();
if (addresses == null) {
this.tcpOutboundTotal++;
ip = conn.getRemoteAddress();
port = new Long(conn.getRemotePort());
addresses = (Map)this.tcpOutbound.get(port);
if (addresses == null) {
addresses = new HashMap();
this.tcpOutbound.put(port, addresses);
}
}
else {
this.tcpInboundTotal++;
}
IpEntry entry = (IpEntry)addresses.get(ip);
if (entry == null) {
entry = new IpEntry();
addresses.put(ip, entry);
}
entry.count++;
entry.states[state]++;
}
}
/**
* Map listening tcp ports to connected remote addresses.
* key == Listening tcp port on the local machine.
* value == List of connected remote addresses.
*/
public Map getTcpInbound() {
return this.tcpInbound;
}
public Map getTcpOutbound() {
return this.tcpOutbound;
} }
public int getTcpInboundTotal() { public int getTcpInboundTotal() {
@ -195,34 +83,4 @@ public class NetStat {
public int getTcpBound() { public int getTcpBound() {
return this.tcpStates[NetFlags.TCP_BOUND]; return this.tcpStates[NetFlags.TCP_BOUND];
} }
private static void dumpConnections(Map map) {
for (Iterator it = map.entrySet().iterator();
it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
Long port = (Long)entry.getKey();
Map addresses = (Map)entry.getValue();
System.out.println(port + "=" + addresses);
}
}
public static void main(String[] args) throws Exception {
NetStat stat = new NetStat(new Sigar());
System.out.println(stat.getTcpInboundTotal() +
" Inbound TCP Connections...");
dumpConnections(stat.getTcpInbound());
System.out.println("\n" + stat.getTcpOutboundTotal() +
" Outbound TCP Connections...");
dumpConnections(stat.getTcpOutbound());
System.out.println("\nTCP States...");
int[] states = stat.getTcpStates();
for (int i=1; i<states.length; i++) {
System.out.println(NetConnection.getStateString(i) + "=" +
states[i]);
}
}
} }