modules/core/native-system/src/main/java/org/rhq/core/system/JavaSystemInfo.java | 7 + modules/core/native-system/src/main/java/org/rhq/core/system/NativeSystemInfo.java | 25 +++-- modules/core/native-system/src/main/java/org/rhq/core/system/SystemInfo.java | 26 +++++ modules/core/native-system/src/test/java/org/rhq/core/system/NativeSystemInfoTest.java | 44 ++++++++++ modules/plugins/script/src/main/resources/META-INF/rhq-plugin.xml | 2 5 files changed, 92 insertions(+), 12 deletions(-)
New commits: commit d9ddab831a2fe6fbca26a5f55bbf884da2547159 Author: John Mazzitelli mazz@redhat.com Date: Tue Dec 29 15:17:11 2009 -0500
add the ability of our systeminfo native API to get network connection data
diff --git a/modules/core/native-system/src/main/java/org/rhq/core/system/JavaSystemInfo.java b/modules/core/native-system/src/main/java/org/rhq/core/system/JavaSystemInfo.java index 7cb635b..61ede0c 100644 --- a/modules/core/native-system/src/main/java/org/rhq/core/system/JavaSystemInfo.java +++ b/modules/core/native-system/src/main/java/org/rhq/core/system/JavaSystemInfo.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Locale;
import org.hyperic.sigar.Mem; +import org.hyperic.sigar.NetConnection; import org.hyperic.sigar.Swap;
import org.rhq.core.util.exec.ProcessExecutor; @@ -277,13 +278,17 @@ public class JavaSystemInfo implements SystemInfo { }
public NetworkAdapterStats getNetworkAdapterStats(String interfaceName) { - throw getUnsupportedException("Cannot get network stats"); + throw getUnsupportedException("Cannot get network adapter stats"); }
public NetworkStats getNetworkStats(String addressName, int port) { throw getUnsupportedException("Cannot get network stats"); }
+ public List<NetConnection> getNetworkConnections(String addressName, int port) { + throw getUnsupportedException("Cannot get network connections"); + } + /** * Constructor for {@link JavaSystemInfo} with package scope so only the {@link SystemInfoFactory} can instantiate * this object. diff --git a/modules/core/native-system/src/main/java/org/rhq/core/system/NativeSystemInfo.java b/modules/core/native-system/src/main/java/org/rhq/core/system/NativeSystemInfo.java index 1665769..3c08b51 100644 --- a/modules/core/native-system/src/main/java/org/rhq/core/system/NativeSystemInfo.java +++ b/modules/core/native-system/src/main/java/org/rhq/core/system/NativeSystemInfo.java @@ -159,24 +159,29 @@ public class NativeSystemInfo implements SystemInfo { }
public NetworkStats getNetworkStats(String addressName, int port) { + List<NetConnection> matches = getNetworkConnections(addressName, port); + NetworkStats stats = new NetworkStats(matches.toArray(new NetConnection[matches.size()])); + return stats; + } + + public List<NetConnection> getNetworkConnections(String addressName, int port) { try { int flags = NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT | NetFlags.CONN_TCP; NetConnection[] conns = sigar.getNetConnectionList(flags);
- InetAddress matchAddress = InetAddress.getByName(addressName); + InetAddress matchAddress = (addressName != null) ? InetAddress.getByName(addressName) : null;
- List<NetConnection> matches = new ArrayList<NetConnection>(); + List<NetConnection> list = new ArrayList<NetConnection>(); for (NetConnection conn : conns) { - - InetAddress connAddress = InetAddress.getByName(conn.getLocalAddress()); - if (conn.getLocalPort() == port && matchAddress.equals(connAddress)) { - matches.add(conn); + if (port > 0 && (conn.getLocalPort() != port)) { + continue; // does not match the port we are looking for } + if (matchAddress != null && !matchAddress.equals(InetAddress.getByName(conn.getLocalAddress()))) { + continue; // does not match the address we are looking for + } + list.add(conn); // matches our criteria, add it to the list to be returned to the caller } - - NetworkStats stats = new NetworkStats(matches.toArray(new NetConnection[matches.size()])); - - return stats; + return list; } catch (SigarException e) { throw new SystemInfoException(e); } catch (UnknownHostException e) { diff --git a/modules/core/native-system/src/main/java/org/rhq/core/system/SystemInfo.java b/modules/core/native-system/src/main/java/org/rhq/core/system/SystemInfo.java index 007e444..c561b4f 100644 --- a/modules/core/native-system/src/main/java/org/rhq/core/system/SystemInfo.java +++ b/modules/core/native-system/src/main/java/org/rhq/core/system/SystemInfo.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.util.List;
import org.hyperic.sigar.Mem; +import org.hyperic.sigar.NetConnection; import org.hyperic.sigar.Swap;
/** @@ -209,8 +210,33 @@ public interface SystemInfo { */ FileSystemInfo getFileSystem(String path);
+ /** + * Returns network adapter measurements for the named network adapter interface. + * @param interfaceName + * @return statistics for the named adapter interface + */ NetworkAdapterStats getNetworkAdapterStats(String interfaceName);
+ /** + * Returns network stats for connections that match the given address and port. + * See {@link #getNetworkConnections(String, int)} for the semantics of the parameters. + * + * @param addressName + * @param port + * @return stats for the connections that are found + * + * @see #getNetworkConnections(String, int) + */ NetworkStats getNetworkStats(String addressName, int port);
+ /** + * Returns information on all known network connections from the given address/port. + * If address is <code>null</code>, connections from all local addresses will be returned. + * If port is <code>0</code>, then connections on all local ports will be returned. + * + * @param addressName if not <code>null</code>, the returned connections are from this local address only + * @param port if not <code>0</code>, the returned connections are from this local port only + * @return the matched connections + */ + List<NetConnection> getNetworkConnections(String addressName, int port); } \ No newline at end of file diff --git a/modules/core/native-system/src/test/java/org/rhq/core/system/NativeSystemInfoTest.java b/modules/core/native-system/src/test/java/org/rhq/core/system/NativeSystemInfoTest.java index 97d5d31..88ed3a8 100644 --- a/modules/core/native-system/src/test/java/org/rhq/core/system/NativeSystemInfoTest.java +++ b/modules/core/native-system/src/test/java/org/rhq/core/system/NativeSystemInfoTest.java @@ -35,6 +35,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit;
import org.hyperic.sigar.Mem; +import org.hyperic.sigar.NetConnection; import org.hyperic.sigar.ProcMem; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Swap; @@ -49,6 +50,8 @@ import org.testng.annotations.Test; */ @Test(groups = "native-system") public class NativeSystemInfoTest { + private static final boolean ENABLED = true; + @AfterMethod @BeforeMethod public void terminateNativeLibrary() { @@ -59,8 +62,36 @@ public class NativeSystemInfoTest { }
/** + * Tests getting network connection information. + */ + @Test(enabled = ENABLED) + public void testGetNetworkConnections() { + SystemInfo sysinfo = SystemInfoFactory.createSystemInfo(); + + if (!sysinfo.isNative()) { + System.out.println("~~~ Native library is not available - skipping testGetNetworkConnections"); + return; + } + + List<NetConnection> allConnections = sysinfo.getNetworkConnections(null, 0); + assert allConnections != null; + if (allConnections.size() > 0) { + String localAddress = allConnections.get(0).getLocalAddress(); + int localPort = (int) allConnections.get(0).getLocalPort(); + List<NetConnection> filtered = sysinfo.getNetworkConnections(localAddress, localPort); + assert filtered != null; + assert filtered.size() > 0 : "connection should not be missing - did it close that fast since we last seen it?"; + assert localAddress.equals(filtered.get(0).getLocalAddress()); + assert localPort == (int) filtered.get(0).getLocalPort(); + } else { + System.out.println("testGetNetworkConnections: no active connections - will not perform filtering tests"); + } + } + + /** * Tests getting network adapter information. */ + @Test(enabled = ENABLED) public void testGetNetworkAdapterInfo() { SystemInfo sysinfo = SystemInfoFactory.createSystemInfo();
@@ -103,6 +134,8 @@ public class NativeSystemInfoTest { assert addr.getHostAddress() != null; }
+ assert sysinfo.getNetworkAdapterStats(adapter.getName()) != null; + System.out.println("Network adapter found: " + adapter); } } @@ -112,6 +145,7 @@ public class NativeSystemInfoTest { * * @throws Exception */ + @Test(enabled = ENABLED) public void testGetAllServices() throws Exception { final SystemInfo sysinfo = SystemInfoFactory.createSystemInfo();
@@ -133,6 +167,7 @@ public class NativeSystemInfoTest { assert allServices.size() > 0; }
+ @Test(enabled = ENABLED) public void testNativeMemory() throws Exception { Sigar sigar = new Sigar(); try { @@ -170,6 +205,7 @@ public class NativeSystemInfoTest { /** * Tests getting process memory usage information. */ + @Test(enabled = ENABLED) public void testProcessMemory() { final SystemInfo sysinfo = SystemInfoFactory.createSystemInfo();
@@ -194,6 +230,7 @@ public class NativeSystemInfoTest { * * @throws Exception */ + @Test(enabled = ENABLED) public void testCreateAlotOfNativeObjects() throws Exception { System.out.println("Creating alot of native Who objects"); for (int i = 0; i < 1000; i++) { @@ -228,6 +265,7 @@ public class NativeSystemInfoTest { * * @throws Exception */ + @Test(enabled = ENABLED) public void testShutdownInitialize() throws Exception { Sigar sigar = null; try { @@ -246,6 +284,7 @@ public class NativeSystemInfoTest { /** * Test the ability to disable the native library and then reenable it. */ + @Test(enabled = ENABLED) public void testDisable() { SystemInfoFactory.disableNativeSystemInfo(); assert SystemInfoFactory.isNativeSystemInfoDisabled() : "Should have been disabled"; @@ -262,6 +301,7 @@ public class NativeSystemInfoTest { /** * Tests the "non-native" Java system info. */ + @Test(enabled = ENABLED) public void testJava() { processJavaSystemInfo(new JavaSystemInfo()); } @@ -271,6 +311,7 @@ public class NativeSystemInfoTest { * * @throws Exception */ + @Test(enabled = ENABLED) public void testNativeConcurrency() throws Exception { final SystemInfo sysinfo = SystemInfoFactory.createSystemInfo();
@@ -377,6 +418,7 @@ public class NativeSystemInfoTest { * Tests getting stuff if there is no native library available (only can test if this is running on a platform that * doesn't have the native libraries in java.library.path) */ + @Test(enabled = ENABLED) public void testNonNative() { if (SystemInfoFactory.isNativeSystemInfoAvailable()) { System.out.println("~~~ Native library is available - cannot test the fallback-to-Java feature"); @@ -390,6 +432,7 @@ public class NativeSystemInfoTest { /** * Test for memory leaks in the native layer. */ + @Test(enabled = ENABLED) public void testMemoryLeak() { final SystemInfo sysinfo = SystemInfoFactory.createSystemInfo();
@@ -442,6 +485,7 @@ public class NativeSystemInfoTest { * * @throws Exception */ + @Test(enabled = ENABLED) public void testNativeConcurrencyExec() throws Exception { final SystemInfo sysinfo = SystemInfoFactory.createSystemInfo();
commit 37cae6f5c2729f14edb0d3a7d0d03269c64b1bd8 Author: John Mazzitelli mazz@redhat.com Date: Tue Dec 29 13:20:08 2009 -0500
need to use longString type so the output can be shown in a multi-line gui component
diff --git a/modules/plugins/script/src/main/resources/META-INF/rhq-plugin.xml b/modules/plugins/script/src/main/resources/META-INF/rhq-plugin.xml index f3afbfe..016985e 100644 --- a/modules/plugins/script/src/main/resources/META-INF/rhq-plugin.xml +++ b/modules/plugins/script/src/main/resources/META-INF/rhq-plugin.xml @@ -60,7 +60,7 @@ </parameters> <results> <c:simple-property name="exitCode" type="integer" required="true"/> - <c:simple-property name="output" required="false" /> + <c:simple-property name="output" type="longString" required="false" /> </results> </operation>
rhq-commits@lists.fedorahosted.org