From f060a656aabbcfc78f6155b478836da6b9ff6352 Mon Sep 17 00:00:00 2001 From: Rui Liu Date: Wed, 3 Jul 2024 18:37:23 +0800 Subject: [PATCH] Support metric system.network.connections for snmphost (otel-host implementation) (#47) --- host/build.gradle | 2 +- .../dc/host/impl/snmphost/SnmpHostDc.java | 49 ++++++++++++++++--- .../dc/host/impl/snmphost/SnmpHostUtil.java | 2 + 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/host/build.gradle b/host/build.gradle index 84f3fa2..6664187 100644 --- a/host/build.gradle +++ b/host/build.gradle @@ -4,7 +4,7 @@ plugins { } group = "com.instana.dc" -version = "0.2.1" +version = "0.2.2" repositories { mavenCentral() diff --git a/host/src/main/java/com/instana/dc/host/impl/snmphost/SnmpHostDc.java b/host/src/main/java/com/instana/dc/host/impl/snmphost/SnmpHostDc.java index 5456c21..be3c2ce 100644 --- a/host/src/main/java/com/instana/dc/host/impl/snmphost/SnmpHostDc.java +++ b/host/src/main/java/com/instana/dc/host/impl/snmphost/SnmpHostDc.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; @@ -116,7 +117,7 @@ private void queryScalarOids() throws IOException { private void queryDiskIo() throws IOException { List> result = simpSnmp.queryColumnOids( Oid.DISKDEVICE, Oid.DISK_IO__READ, Oid.DISK_IO__WRITE); - List output = new ArrayList(); + List output = new ArrayList<>(); for (Map result1 : result) { String device = SnmpValue.getString(result1, Oid.DISKDEVICE, null); @@ -139,7 +140,7 @@ private void queryFileSystemUsage() throws IOException { List> result = simpSnmp.queryColumnOids( Oid.FILESYSTEMDEVICE, Oid.FILESYSTEM_USAGE__USED, Oid.FILESYSTEM_USAGE__ALL, Oid.FILESYSTEM_USAGE__UNIT); - List output = new ArrayList(); + List output = new ArrayList<>(); int no = 0; for (Map result1 : result) { @@ -169,8 +170,8 @@ private void queryNetworkDropped_Error() throws IOException { Oid.NETWORKDEVICE, Oid.NETWORK_DROPPED_RECEIVE, Oid.NETWORK_DROPPED_TRANSMIT, Oid.NETWORK_ERRORS_RECEIVE, Oid.NETWORK_ERRORS_TRANSMIT); - List outputD = new ArrayList(); - List outputE = new ArrayList(); + List outputD = new ArrayList<>(); + List outputE = new ArrayList<>(); for (Map result1 : result) { String device = SnmpValue.getString(result1, Oid.NETWORKDEVICE, null); @@ -203,8 +204,8 @@ private void queryNetworkIo_Packets() throws IOException { Oid.NETWORKDEVICE, Oid.NETWORK_IO_RECEIVE, Oid.NETWORK_IO_TRANSMIT, Oid.NETWORK_PACKAGES_RECEIVE, Oid.NETWORK_PACKAGES_TRANSMIT); - List outputI = new ArrayList(); - List outputP = new ArrayList(); + List outputI = new ArrayList<>(); + List outputP = new ArrayList<>(); for (Map result1 : result) { String device = SnmpValue.getString(result1, Oid.NETWORKDEVICE, null); @@ -232,6 +233,41 @@ private void queryNetworkIo_Packets() throws IOException { getRawMetric(SYSTEM_NETWORK_PACKETS_NAME).setValue(outputP); } + private static void createTcpConnResult(List output, Map tcpConns, long key, String name) { + Long num = tcpConns.get(key); + if (num == null) { + num = 0L; + } + output.add(new SimpleQueryResult(num).setKey(name) + .setAttribute("protocol", "tcp").setAttribute("state", name)); + } + + private void queryTcpConnection() throws IOException { + List> result = simpSnmp.queryColumnOids(Oid.NETWORK_CONNECTION); + List output = new ArrayList<>(); + Map tcpConns = new HashMap<>(); + for (Map result1 : result) { + result1.forEach((oid, snmpValue) -> { + tcpConns.merge(snmpValue.toLong(), 1L, Long::sum); + }); + } + + createTcpConnResult(output, tcpConns, 1, "CLOSE"); + createTcpConnResult(output, tcpConns, 2, "LISTEN"); + createTcpConnResult(output, tcpConns, 3, "SYN_SENT"); + createTcpConnResult(output, tcpConns, 4, "SYN_RECV"); + createTcpConnResult(output, tcpConns, 5, "ESTABLISHED"); + createTcpConnResult(output, tcpConns, 6, "FIN_WAIT_1"); + createTcpConnResult(output, tcpConns, 7, "FIN_WAIT_2"); + createTcpConnResult(output, tcpConns, 8, "CLOSE_WAIT"); + createTcpConnResult(output, tcpConns, 9, "LAST_ACK"); + createTcpConnResult(output, tcpConns, 10, "CLOSING"); + createTcpConnResult(output, tcpConns, 11, "TIME_WAIT"); + createTcpConnResult(output, tcpConns, 12, "DELETE"); + + getRawMetric(SYSTEM_NETWORK_CONNECTIONS_NAME).setValue(output); + } + @Override public void collectData() { logger.info("Start to collect metrics"); @@ -241,6 +277,7 @@ public void collectData() { queryFileSystemUsage(); queryNetworkDropped_Error(); queryNetworkIo_Packets(); + queryTcpConnection(); } catch (IOException e) { logger.log(Level.SEVERE, "Failed to collectData", e); } diff --git a/host/src/main/java/com/instana/dc/host/impl/snmphost/SnmpHostUtil.java b/host/src/main/java/com/instana/dc/host/impl/snmphost/SnmpHostUtil.java index 3e0a584..c2040a2 100644 --- a/host/src/main/java/com/instana/dc/host/impl/snmphost/SnmpHostUtil.java +++ b/host/src/main/java/com/instana/dc/host/impl/snmphost/SnmpHostUtil.java @@ -58,6 +58,8 @@ public static class Oid { public static final OID NETWORK_IO_TRANSMIT = new OID(".1.3.6.1.2.1.2.2.1.16"); public static final OID NETWORK_PACKAGES_RECEIVE = new OID(".1.3.6.1.2.1.2.2.1.11"); public static final OID NETWORK_PACKAGES_TRANSMIT = new OID(".1.3.6.1.2.1.2.2.1.17"); + + public static final OID NETWORK_CONNECTION = new OID(".1.3.6.1.2.1.6.19.1.7"); } //Parameters: