Add net_stat_port function to provide metrics on specific port+address

This commit is contained in:
Doug MacEachern committed 2007-04-06 05:05:37 +00:00
1 parent 808a7deeba
commit 86deb1a2d9
9 files changed
+263 -8

No files matched your search

+6
View File
@@ -847,6 +847,12 @@ my %classes = (
{
name => 'tcp_outbound_total', type => 'Int',
},
{
name => 'all_inbound_total', type => 'Int',
},
{
name => 'all_outbound_total', type => 'Int',
},
],
ResourceLimit => [
{
+37 -4
View File
@@ -851,8 +851,31 @@ JNIEXPORT jobjectArray SIGAR_JNIx(getNetConnectionList)
return connarray;
}
static int jbyteArray_to_sigar_net_address(JNIEnv *env, jbyteArray jaddress,
sigar_net_address_t *address)
{
jsize len = JENV->GetArrayLength(env, jaddress);
JENV->GetByteArrayRegion(env, jaddress, 0, len,
(jbyte *)&address->addr.in6);
switch (len) {
case 4:
address->family = SIGAR_AF_INET;
break;
case 4*4:
address->family = SIGAR_AF_INET6;
break;
default:
return EINVAL;
}
return SIGAR_OK;
}
JNIEXPORT void SIGAR_JNI(NetStat_stat)
(JNIEnv *env, jobject obj, jobject sigar_obj, jint flags)
(JNIEnv *env, jobject obj, jobject sigar_obj, jint flags,
jbyteArray jaddress, jlong port)
{
int status;
sigar_net_stat_t netstat;
@@ -860,9 +883,21 @@ JNIEXPORT void SIGAR_JNI(NetStat_stat)
jfieldID id;
jintArray states;
jint tcp_states[SIGAR_TCP_UNKNOWN];
sigar_net_address_t address;
jboolean has_port = (port != -1);
dSIGAR_VOID;
status = sigar_net_stat_get(sigar, &netstat, flags);
if (has_port) {
status = jbyteArray_to_sigar_net_address(env, jaddress, &address);
if (status == SIGAR_OK) {
status = sigar_net_stat_port_get(sigar, &netstat, flags,
&address, port);
}
}
else {
status = sigar_net_stat_get(sigar, &netstat, flags);
}
if (status != SIGAR_OK) {
sigar_throw_error(env, jsigar, status);
return;
@@ -891,8 +926,6 @@ JNIEXPORT void SIGAR_JNI(NetStat_stat)
id = JENV->GetFieldID(env, cls, "tcpStates", "[I");
JENV->SetObjectField(env, obj, id, states);
return;
}
JNIEXPORT jstring SIGAR_JNI(NetConnection_getTypeString)
@@ -20,12 +20,14 @@ package org.hyperic.sigar;
public class NetStat implements java.io.Serializable {
private static final long serialVersionUID = 02242007L;
private static final long serialVersionUID = 04052007L;
protected int[] tcpStates;
protected int tcpInboundTotal, tcpOutboundTotal;
protected int allInboundTotal, allOutboundTotal;
public native void stat(Sigar sigar, int flags) throws SigarException;
native void stat(Sigar sigar, int flags,
byte[] address, long port) throws SigarException;
public NetStat() { }
@@ -34,7 +36,17 @@ public class NetStat implements java.io.Serializable {
NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT |
NetFlags.CONN_TCP;
stat(sigar, flags);
stat(sigar, flags, null, -1);
}
public void stat(Sigar sigar,
byte[] address, long port)
throws SigarException {
int flags =
NetFlags.CONN_CLIENT | NetFlags.CONN_TCP;
stat(sigar, flags, address, port);
}
public int getTcpInboundTotal() {
@@ -45,6 +57,14 @@ public class NetStat implements java.io.Serializable {
return this.tcpOutboundTotal;
}
public int getAllInboundTotal() {
return this.allInboundTotal;
}
public int getAllOutboundTotal() {
return this.allOutboundTotal;
}
public int[] getTcpStates() {
return this.tcpStates;
}
@@ -799,7 +799,14 @@ public class Sigar implements SigarProxy {
netstat.stat(this);
return netstat;
}
public NetStat getNetStat(byte[] address, long port)
throws SigarException {
NetStat netstat = new NetStat();
netstat.stat(this, address, port);
return netstat;
}
public native Who[] getWhoList()
throws SigarException;
@@ -55,6 +55,7 @@ public class SigarTestRunner extends SigarCommandBase {
TestNetInfo.class,
TestNetRoute.class,
TestNetStat.class,
TestNetStatPort.class,
TestProcArgs.class,
TestProcEnv.class,
TestProcExe.class,
@@ -0,0 +1,74 @@
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.sigar.test;
import java.net.InetAddress;
import org.hyperic.sigar.NetConnection;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarNotImplementedException;
public class TestNetStatPort extends SigarTestCase {
public TestNetStatPort(String name) {
super(name);
}
private void netstat(Sigar sigar, String addr, long port) throws Exception {
InetAddress address = InetAddress.getByName(addr);
traceln("");
traceln("using address=" + address + ":" + port);
NetStat netstat;
try {
netstat = sigar.getNetStat(address.getAddress(), port);
} catch (SigarNotImplementedException e) {
return;
}
assertGtEqZeroTrace("AllOutbound", netstat.getAllOutboundTotal());
assertGtEqZeroTrace("Outbound", netstat.getTcpOutboundTotal());
assertGtEqZeroTrace("Inbound", netstat.getTcpInboundTotal());
assertGtEqZeroTrace("AllInbound", netstat.getAllInboundTotal());
int[] states = netstat.getTcpStates();
for (int i=0; i<NetFlags.TCP_UNKNOWN; i++) {
assertGtEqZeroTrace(NetConnection.getStateString(i), states[i]);
}
}
public void testCreate() throws Exception {
Sigar sigar = getSigar();
NetInterfaceConfig ifconfig =
sigar.getNetInterfaceConfig(null);
String[] addrs = {
ifconfig.getAddress(),
"0:0:0:0:0:0:0:1",
};
for (int i=0; i<addrs.length; i++) {
netstat(sigar, addrs[i], 22);
}
}
}