Skip to content

Commit

Permalink
Support metric system.network.connections for snmphost (otel-host imp…
Browse files Browse the repository at this point in the history
…lementation) (#47)
  • Loading branch information
Rui Liu committed Jul 3, 2024
1 parent c367387 commit f060a65
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion host/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.instana.dc"
version = "0.2.1"
version = "0.2.2"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,7 +117,7 @@ private void queryScalarOids() throws IOException {
private void queryDiskIo() throws IOException {
List<Map<OID, SnmpValue>> result = simpSnmp.queryColumnOids(
Oid.DISKDEVICE, Oid.DISK_IO__READ, Oid.DISK_IO__WRITE);
List<SimpleQueryResult> output = new ArrayList<SimpleQueryResult>();
List<SimpleQueryResult> output = new ArrayList<>();

for (Map<OID, SnmpValue> result1 : result) {
String device = SnmpValue.getString(result1, Oid.DISKDEVICE, null);
Expand All @@ -139,7 +140,7 @@ private void queryFileSystemUsage() throws IOException {
List<Map<OID, SnmpValue>> result = simpSnmp.queryColumnOids(
Oid.FILESYSTEMDEVICE, Oid.FILESYSTEM_USAGE__USED,
Oid.FILESYSTEM_USAGE__ALL, Oid.FILESYSTEM_USAGE__UNIT);
List<SimpleQueryResult> output = new ArrayList<SimpleQueryResult>();
List<SimpleQueryResult> output = new ArrayList<>();

int no = 0;
for (Map<OID, SnmpValue> result1 : result) {
Expand Down Expand Up @@ -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<SimpleQueryResult> outputD = new ArrayList<SimpleQueryResult>();
List<SimpleQueryResult> outputE = new ArrayList<SimpleQueryResult>();
List<SimpleQueryResult> outputD = new ArrayList<>();
List<SimpleQueryResult> outputE = new ArrayList<>();

for (Map<OID, SnmpValue> result1 : result) {
String device = SnmpValue.getString(result1, Oid.NETWORKDEVICE, null);
Expand Down Expand Up @@ -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<SimpleQueryResult> outputI = new ArrayList<SimpleQueryResult>();
List<SimpleQueryResult> outputP = new ArrayList<SimpleQueryResult>();
List<SimpleQueryResult> outputI = new ArrayList<>();
List<SimpleQueryResult> outputP = new ArrayList<>();

for (Map<OID, SnmpValue> result1 : result) {
String device = SnmpValue.getString(result1, Oid.NETWORKDEVICE, null);
Expand Down Expand Up @@ -232,6 +233,41 @@ private void queryNetworkIo_Packets() throws IOException {
getRawMetric(SYSTEM_NETWORK_PACKETS_NAME).setValue(outputP);
}

private static void createTcpConnResult(List<SimpleQueryResult> output, Map<Long, Long> 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<Map<OID, SnmpValue>> result = simpSnmp.queryColumnOids(Oid.NETWORK_CONNECTION);
List<SimpleQueryResult> output = new ArrayList<>();
Map<Long, Long> tcpConns = new HashMap<>();
for (Map<OID, SnmpValue> 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");
Expand All @@ -241,6 +277,7 @@ public void collectData() {
queryFileSystemUsage();
queryNetworkDropped_Error();
queryNetworkIo_Packets();
queryTcpConnection();
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to collectData", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f060a65

Please sign in to comment.