diff --git a/README.md b/README.md index 28ddc90..852e3cc 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ ODCD (OpenTelemetry based Data Collector for Telemetry Data) is a collection of ## ### Requirements -- Data Collectors for Host **Java 8+** +- Data Collectors for Host **Java 11+** - Data Collectors for Databases **Java 8+** - Data Collectors for LLM **Java 11+** diff --git a/host/build.gradle b/host/build.gradle index 33fa3a9..c05af0d 100644 --- a/host/build.gradle +++ b/host/build.gradle @@ -4,7 +4,7 @@ plugins { } group = "com.instana.dc" -version = "0.1.5" +version = "0.1.6" repositories { mavenCentral() @@ -20,7 +20,9 @@ dependencies { implementation("io.opentelemetry:opentelemetry-exporter-sender-okhttp:1.34.1") implementation("io.opentelemetry.semconv:opentelemetry-semconv:1.23.1-alpha") implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.16.0-rc1") - implementation(files("libs/otel-dc-0.9.8.jar")) + implementation("org.snmp4j:snmp4j-agent:3.8.1") + implementation(files("libs/otel-dc-0.9.9.jar")) + implementation(files("libs/simp-snmp-0.1.0.jar")) testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1") diff --git a/host/config/config-simphost.yaml b/host/config/config-simphost.yaml new file mode 100644 index 0000000..d6826f2 --- /dev/null +++ b/host/config/config-simphost.yaml @@ -0,0 +1,8 @@ +host.system: simp_host + +instances: + - poll.interval: 30 + callback.interval: 20 + otel.backend.url: http://127.0.0.1:4317 + #otel.backend.using.http: true + #otel.backend.url: http://127.0.0.1:4318/v1/metrics diff --git a/host/config/config.yaml b/host/config/config.yaml index d6826f2..960b61b 100644 --- a/host/config/config.yaml +++ b/host/config/config.yaml @@ -1,8 +1,11 @@ -host.system: simp_host +host.system: snmp_host instances: - - poll.interval: 30 - callback.interval: 20 + - snmp.host: udp:9.112.252.102/161 + poll.interval: 25 + callback.interval: 30 otel.backend.url: http://127.0.0.1:4317 #otel.backend.using.http: true - #otel.backend.url: http://127.0.0.1:4318/v1/metrics + #otel.backend.url: http://9.112.252.66:4318/v1/metrics + host.name: stantest0.fyre.ibm.com + #os.type: linux diff --git a/host/libs/otel-dc-0.9.8.jar b/host/libs/otel-dc-0.9.9.jar similarity index 86% rename from host/libs/otel-dc-0.9.8.jar rename to host/libs/otel-dc-0.9.9.jar index 0195378..f3dc576 100644 Binary files a/host/libs/otel-dc-0.9.8.jar and b/host/libs/otel-dc-0.9.9.jar differ diff --git a/host/libs/simp-snmp-0.1.0.jar b/host/libs/simp-snmp-0.1.0.jar new file mode 100644 index 0000000..289dcd0 Binary files /dev/null and b/host/libs/simp-snmp-0.1.0.jar differ diff --git a/host/src/main/java/com/instana/dc/host/AbstractHostDc.java b/host/src/main/java/com/instana/dc/host/AbstractHostDc.java index 22f1b13..7e08cf8 100644 --- a/host/src/main/java/com/instana/dc/host/AbstractHostDc.java +++ b/host/src/main/java/com/instana/dc/host/AbstractHostDc.java @@ -30,6 +30,8 @@ public abstract class AbstractHostDc extends AbstractDc implements IDc { private final String serviceName; public final static String INSTRUMENTATION_SCOPE_PREFIX = "otelcol/hostmetricsreceiver/"; + public final static String DEFAULT_SERVICE_NAME = "otel-host"; + private final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); public ScheduledExecutorService getExec() { @@ -42,7 +44,7 @@ public AbstractHostDc(Map properties, String hostSystem) { callbackInterval = (Integer) properties.getOrDefault(CALLBACK_INTERVAL, DEFAULT_CALLBACK_INTERVAL); otelBackendUrl = (String) properties.get(OTEL_BACKEND_URL); otelUsingHttp = (Boolean) properties.getOrDefault(OTEL_BACKEND_USING_HTTP, Boolean.FALSE); - serviceName = (String) properties.get(OTEL_SERVICE_NAME); + serviceName = (String) properties.getOrDefault(OTEL_SERVICE_NAME, DEFAULT_SERVICE_NAME); } public String getServiceName() { diff --git a/host/src/main/java/com/instana/dc/host/HostDcRegistry.java b/host/src/main/java/com/instana/dc/host/HostDcRegistry.java index 313cdb7..0e74d82 100644 --- a/host/src/main/java/com/instana/dc/host/HostDcRegistry.java +++ b/host/src/main/java/com/instana/dc/host/HostDcRegistry.java @@ -4,10 +4,10 @@ */ package com.instana.dc.host; -import com.instana.dc.host.AbstractHostDc; import com.instana.dc.DcException; import com.instana.dc.host.impl.simphost.SimpHostDc; import com.instana.dc.host.impl.mqappliance.MqApplianceDc; +import com.instana.dc.host.impl.snmphost.SnmpHostDc; import java.util.HashMap; import java.util.Map; @@ -18,6 +18,7 @@ public class HostDcRegistry { private final Map> map = new HashMap>() {{ put("SIMP_HOST", SimpHostDc.class); put("MQ_APPLIANCE", MqApplianceDc.class); + put("SNMP_HOST", SnmpHostDc.class); }}; public Class findHostDc(String hostSystem) throws DcException { diff --git a/host/src/main/java/com/instana/dc/host/impl/simphost/SimpHostDc.java b/host/src/main/java/com/instana/dc/host/impl/simphost/SimpHostDc.java index 83f0bff..6876e03 100644 --- a/host/src/main/java/com/instana/dc/host/impl/simphost/SimpHostDc.java +++ b/host/src/main/java/com/instana/dc/host/impl/simphost/SimpHostDc.java @@ -4,6 +4,7 @@ */ package com.instana.dc.host.impl.simphost; +import com.instana.dc.DcUtil; import com.instana.dc.host.AbstractHostDc; import com.instana.dc.host.HostDcUtil; import io.opentelemetry.api.common.Attributes; @@ -15,10 +16,12 @@ import java.net.UnknownHostException; import java.util.List; import java.util.Map; +import java.util.logging.Level; import java.util.logging.Logger; import static com.instana.dc.DcUtil.mergeResourceAttributesFromEnv; import static com.instana.dc.host.HostDcUtil.*; +import static io.opentelemetry.api.common.AttributeKey.stringKey; public class SimpHostDc extends AbstractHostDc { private static final Logger logger = Logger.getLogger(SimpHostDc.class.getName()); @@ -56,7 +59,8 @@ public Resource getResourceAttributes() { resource = resource.merge( Resource.create(Attributes.of(ResourceAttributes.HOST_NAME, hostName, ResourceAttributes.OS_TYPE, "linux", - ResourceAttributes.HOST_ID, getHostId() + ResourceAttributes.HOST_ID, getHostId(), + stringKey(DcUtil.INSTANA_PLUGIN), "host" )) ); @@ -76,7 +80,7 @@ public void collectData() { getRawMetric(SYSTEM_CPU_LOAD5_NAME).setValue(loads.get(1)); getRawMetric(SYSTEM_CPU_LOAD15_NAME).setValue(loads.get(2)); } catch (Exception e) { - logger.severe("Cannot record loads: " + e.getMessage()); + logger.log(Level.SEVERE, "Cannot record loads", e); } } } diff --git a/host/src/main/java/com/instana/dc/host/impl/simphost/SimpHostUtil.java b/host/src/main/java/com/instana/dc/host/impl/simphost/SimpHostUtil.java index c3c75d0..c92ba4e 100644 --- a/host/src/main/java/com/instana/dc/host/impl/simphost/SimpHostUtil.java +++ b/host/src/main/java/com/instana/dc/host/impl/simphost/SimpHostUtil.java @@ -92,10 +92,11 @@ public String toString() { } } + private static Pattern cpuPattern = Pattern.compile("\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)"); + public static CpuTime getCpuTime() throws IOException { String line = HostDcUtil.readFileTextLine("/proc/stat"); - Pattern pattern = Pattern.compile("\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)\\D+(\\d+)"); - Matcher m = pattern.matcher(line); + Matcher m = cpuPattern.matcher(line); CpuTime cpuTime = new CpuTime(); if (m.find()) { @@ -135,13 +136,13 @@ public static List getCpuTimeResults() { interrupt.setKey("interrupt"); softirq.setKey("softirq"); - user.setAttribute("cpu", "cpu"); - nice.setAttribute("cpu", "cpu"); - system.setAttribute("cpu", "cpu"); - idle.setAttribute("cpu", "cpu"); - wait.setAttribute("cpu", "cpu"); - interrupt.setAttribute("cpu", "cpu"); - softirq.setAttribute("cpu", "cpu"); + user.setAttribute("cpu", "cpu0"); + nice.setAttribute("cpu", "cpu0"); + system.setAttribute("cpu", "cpu0"); + idle.setAttribute("cpu", "cpu0"); + wait.setAttribute("cpu", "cpu0"); + interrupt.setAttribute("cpu", "cpu0"); + softirq.setAttribute("cpu", "cpu0"); user.setAttribute("state", "user"); nice.setAttribute("state", "nice"); 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 new file mode 100644 index 0000000..b3e0064 --- /dev/null +++ b/host/src/main/java/com/instana/dc/host/impl/snmphost/SnmpHostDc.java @@ -0,0 +1,234 @@ +/* + * (c) Copyright IBM Corp. 2024 + * (c) Copyright Instana Inc. + */ +package com.instana.dc.host.impl.snmphost; + +import com.instana.dc.DcUtil; +import com.instana.dc.SimpleQueryResult; +import com.instana.dc.host.AbstractHostDc; +import com.instana.simpsnmp.SimpSnmp; +import com.instana.simpsnmp.SnmpValue; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.sdk.resources.Resource; +import io.opentelemetry.semconv.ResourceAttributes; +import org.snmp4j.smi.OID; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import static com.instana.dc.DcUtil.mergeResourceAttributesFromEnv; +import static com.instana.dc.host.HostDcUtil.*; +import static com.instana.dc.host.impl.snmphost.SnmpHostUtil.Oid; +import static io.opentelemetry.api.common.AttributeKey.stringKey; + +public class SnmpHostDc extends AbstractHostDc { + private static final Logger logger = Logger.getLogger(SnmpHostDc.class.getName()); + private final SimpSnmp simpSnmp; + private final String hostName; + private final String osType; + + public SnmpHostDc(Map properties, String hostSystem) throws IOException { + super(properties, hostSystem); + String snmpHost = (String) properties.getOrDefault(SnmpHostUtil.SNMP_HOST, "udp:127.0.0.1/161"); + simpSnmp = new SimpSnmp(snmpHost); + + Map result = simpSnmp.queryScalarOids(Oid.HOST_NAME, Oid.OS_TYPE); + String hostName0 = SnmpValue.getString(result, Oid.HOST_NAME, null); + String osType0 = parseOsType(SnmpValue.getString(result, Oid.OS_TYPE, null)); + hostName = (String) properties.getOrDefault(SnmpHostUtil.HOST_NAME, hostName0); + osType = (String) properties.getOrDefault(SnmpHostUtil.OS_TYPE, osType0); + } + + private String parseOsType(String osType0) { + if (osType0 == null) { + return "linux"; + } + return osType0.split(" ")[0].toLowerCase(); + } + + @Override + public Resource getResourceAttributes() { + Resource resource = Resource.getDefault().merge( + Resource.create( + Attributes.of( + ResourceAttributes.SERVICE_NAME, getServiceName(), + ResourceAttributes.SERVICE_INSTANCE_ID, hostName + ))); + + resource = resource.merge( + Resource.create( + Attributes.of(ResourceAttributes.HOST_NAME, hostName, + ResourceAttributes.OS_TYPE, osType, + ResourceAttributes.HOST_ID, hostName, + stringKey(DcUtil.INSTANA_PLUGIN), "host" + ))); + + return mergeResourceAttributesFromEnv(resource); + } + + private void queryScalarOids() throws IOException { + Map result = simpSnmp.queryScalarOids( + Oid.CPU_TIME__USER, + Oid.CPU_TIME__SYSTEM, + Oid.CPU_TIME__IDLE, + Oid.CPU_TIME__NICE, + Oid.CPU_LOAD_1M, + Oid.CPU_LOAD_5M, + Oid.CPU_LOAD_15M, + Oid.MEMORY_USAGE__TOTAL, + Oid.MEMORY_USAGE__FREE, + Oid.MEMORY_USAGE__BUFFERED, + Oid.MEMORY_USAGE__CACHED + ); + + getRawMetric(SYSTEM_CPU_TIME_NAME).setValue(SnmpHostUtil.getCpuTimeResults(result)); + + getRawMetric(SYSTEM_MEMORY_USAGE_NAME).setValue(SnmpHostUtil.getMemUsageResults(result)); + + if (result.containsKey(Oid.CPU_LOAD_1M)) { + getRawMetric(SYSTEM_CPU_LOAD1_NAME).setValue(result.get(Oid.CPU_LOAD_1M).toDouble()); + } + if (result.containsKey(Oid.CPU_LOAD_5M)) { + getRawMetric(SYSTEM_CPU_LOAD5_NAME).setValue(result.get(Oid.CPU_LOAD_5M).toDouble()); + } + if (result.containsKey(Oid.CPU_LOAD_15M)) { + getRawMetric(SYSTEM_CPU_LOAD15_NAME).setValue(result.get(Oid.CPU_LOAD_15M).toDouble()); + } + } + + private void queryDiskIo() throws IOException { + List> result = simpSnmp.queryColumnOids( + Oid.DISKDEVICE, Oid.DISK_IO__READ, Oid.DISK_IO__WRITE); + List output = new ArrayList(); + + for (Map result1 : result) { + String device = SnmpValue.getString(result1, Oid.DISKDEVICE, null); + if (device != null) { + long r = SnmpValue.getLong(result1, Oid.DISK_IO__READ, 0L); + long w = SnmpValue.getLong(result1, Oid.DISK_IO__WRITE, 0L); + output.add(new SimpleQueryResult(r).setKey(device + ":r") + .setAttribute("device", device).setAttribute("direction", "read") + ); + output.add(new SimpleQueryResult(w).setKey(device + ":w") + .setAttribute("device", device).setAttribute("direction", "write") + ); + } + } + + getRawMetric(SYSTEM_DISK_IO_NAME).setValue(output); + } + + 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(); + + for (Map result1 : result) { + String device = SnmpValue.getString(result1, Oid.FILESYSTEMDEVICE, null); + long unit = SnmpValue.getLong(result1, Oid.FILESYSTEM_USAGE__UNIT, 0L); + if (device != null && unit != 0) { + long used = SnmpValue.getLong(result1, Oid.FILESYSTEM_USAGE__USED, 0L); + long all = SnmpValue.getLong(result1, Oid.FILESYSTEM_USAGE__ALL, 0L); + long free = all - used; + output.add(new SimpleQueryResult(used * unit).setKey(device + ":u") + .setAttribute("device", device).setAttribute("mountpoint", device).setAttribute("state", "used") + .setAttribute("mode", "-").setAttribute("type", "ext4") + ); + output.add(new SimpleQueryResult(free * unit).setKey(device + ":f") + .setAttribute("device", device).setAttribute("mountpoint", device).setAttribute("state", "free") + .setAttribute("mode", "-").setAttribute("type", "ext4") + ); + } + } + + getRawMetric(SYSTEM_FILESYSTEM_USAGE_NAME).setValue(output); + } + + private void queryNetworkDropped_Error() throws IOException { + List> result = simpSnmp.queryColumnOids( + 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(); + + for (Map result1 : result) { + String device = SnmpValue.getString(result1, Oid.NETWORKDEVICE, null); + if (device != null) { + long dr = SnmpValue.getLong(result1, Oid.NETWORK_DROPPED_RECEIVE, 0L); + long dt = SnmpValue.getLong(result1, Oid.NETWORK_DROPPED_TRANSMIT, 0L); + long er = SnmpValue.getLong(result1, Oid.NETWORK_ERRORS_RECEIVE, 0L); + long et = SnmpValue.getLong(result1, Oid.NETWORK_ERRORS_TRANSMIT, 0L); + outputD.add(new SimpleQueryResult(dr).setKey(device + ":r") + .setAttribute("device", device).setAttribute("direction", "receive") + ); + outputD.add(new SimpleQueryResult(dt).setKey(device + ":t") + .setAttribute("device", device).setAttribute("direction", "transmit") + ); + outputE.add(new SimpleQueryResult(er).setKey(device + ":r") + .setAttribute("device", device).setAttribute("direction", "receive") + ); + outputE.add(new SimpleQueryResult(et).setKey(device + ":t") + .setAttribute("device", device).setAttribute("direction", "transmit") + ); + } + } + + getRawMetric(SYSTEM_NETWORK_DROPPED_NAME).setValue(outputD); + getRawMetric(SYSTEM_NETWORK_ERRORS_NAME).setValue(outputE); + } + + private void queryNetworkIo_Packets() throws IOException { + List> result = simpSnmp.queryColumnOids( + 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(); + + for (Map result1 : result) { + String device = SnmpValue.getString(result1, Oid.NETWORKDEVICE, null); + if (device != null) { + long ir = SnmpValue.getLong(result1, Oid.NETWORK_IO_RECEIVE, 0L); + long it = SnmpValue.getLong(result1, Oid.NETWORK_IO_TRANSMIT, 0L); + long pr = SnmpValue.getLong(result1, Oid.NETWORK_PACKAGES_RECEIVE, 0L); + long pt = SnmpValue.getLong(result1, Oid.NETWORK_PACKAGES_TRANSMIT, 0L); + outputI.add(new SimpleQueryResult(ir).setKey(device + ":r") + .setAttribute("device", device).setAttribute("direction", "receive") + ); + outputI.add(new SimpleQueryResult(it).setKey(device + ":t") + .setAttribute("device", device).setAttribute("direction", "transmit") + ); + outputP.add(new SimpleQueryResult(pr).setKey(device + ":r") + .setAttribute("device", device).setAttribute("direction", "receive") + ); + outputP.add(new SimpleQueryResult(pt).setKey(device + ":t") + .setAttribute("device", device).setAttribute("direction", "transmit") + ); + } + } + + getRawMetric(SYSTEM_NETWORK_IO_NAME).setValue(outputI); + getRawMetric(SYSTEM_NETWORK_PACKETS_NAME).setValue(outputP); + } + + @Override + public void collectData() { + logger.info("Start to collect metrics"); + try { + queryScalarOids(); + queryDiskIo(); + queryFileSystemUsage(); + queryNetworkDropped_Error(); + queryNetworkIo_Packets(); + } 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 new file mode 100644 index 0000000..3e0a584 --- /dev/null +++ b/host/src/main/java/com/instana/dc/host/impl/snmphost/SnmpHostUtil.java @@ -0,0 +1,131 @@ +/* + * (c) Copyright IBM Corp. 2024 + * (c) Copyright Instana Inc. + */ +package com.instana.dc.host.impl.snmphost; + +import com.instana.dc.SimpleQueryResult; +import com.instana.dc.host.HostDcUtil; +import com.instana.simpsnmp.SnmpValue; +import org.snmp4j.smi.OID; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class SnmpHostUtil { + private static final Logger logger = Logger.getLogger(SnmpHostUtil.class.getName()); + + public static class Oid { + //scalar_oids: + public static final OID CPU_TIME__USER = new OID(".1.3.6.1.4.1.2021.11.50.0"); + public static final OID CPU_TIME__SYSTEM = new OID(".1.3.6.1.4.1.2021.11.52.0"); + public static final OID CPU_TIME__IDLE = new OID(".1.3.6.1.4.1.2021.11.53.0"); + public static final OID CPU_TIME__NICE = new OID(".1.3.6.1.4.1.2021.11.51.0"); + + public static final OID CPU_LOAD_1M = new OID(".1.3.6.1.4.1.2021.10.1.6.1"); + public static final OID CPU_LOAD_5M = new OID(".1.3.6.1.4.1.2021.10.1.6.2"); + public static final OID CPU_LOAD_15M = new OID(".1.3.6.1.4.1.2021.10.1.6.3"); + + public static final OID MEMORY_USAGE__TOTAL = new OID(".1.3.6.1.4.1.2021.4.5.0"); + public static final OID MEMORY_USAGE__FREE = new OID(".1.3.6.1.4.1.2021.4.6.0"); + public static final OID MEMORY_USAGE__BUFFERED = new OID(".1.3.6.1.4.1.2021.4.14.0"); + public static final OID MEMORY_USAGE__CACHED = new OID(".1.3.6.1.4.1.2021.4.15.0"); + + public static final OID HOST_NAME = new OID("1.3.6.1.2.1.1.5.0"); + public static final OID OS_TYPE = new OID("1.3.6.1.2.1.1.1.0"); + + + //column_oids: + public static final OID DISK_IO__READ = new OID(".1.3.6.1.4.1.2021.13.15.1.1.3"); + public static final OID DISK_IO__WRITE = new OID(".1.3.6.1.4.1.2021.13.15.1.1.4"); + public static final OID DISKDEVICE = new OID(".1.3.6.1.4.1.2021.13.15.1.1.2"); + + public static final OID FILESYSTEMDEVICE = new OID(".1.3.6.1.2.1.25.2.3.1.3"); + public static final OID FILESYSTEM_USAGE__USED = new OID(".1.3.6.1.2.1.25.2.3.1.6"); + public static final OID FILESYSTEM_USAGE__ALL = new OID(".1.3.6.1.2.1.25.2.3.1.5"); + public static final OID FILESYSTEM_USAGE__UNIT = new OID(".1.3.6.1.2.1.25.2.3.1.4"); + + public static final OID NETWORKDEVICE = new OID(".1.3.6.1.2.1.2.2.1.2"); + public static final OID NETWORK_DROPPED_RECEIVE = new OID(".1.3.6.1.2.1.2.2.1.13"); + public static final OID NETWORK_DROPPED_TRANSMIT = new OID(".1.3.6.1.2.1.2.2.1.19"); + public static final OID NETWORK_ERRORS_RECEIVE = new OID(".1.3.6.1.2.1.2.2.1.14"); + public static final OID NETWORK_ERRORS_TRANSMIT = new OID(".1.3.6.1.2.1.2.2.1.20"); + public static final OID NETWORK_IO_RECEIVE = new OID(".1.3.6.1.2.1.2.2.1.10"); + 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"); + } + + //Parameters: + public static final String SNMP_HOST = "snmp.host"; + public static final String HOST_NAME = "host.name"; + public static final String OS_TYPE = "os.type"; + + + public static List getCpuTimeResults(Map result) { + SimpleQueryResult user = null; + SimpleQueryResult system = null; + SimpleQueryResult idle = null; + SimpleQueryResult nice = null; + List output = new ArrayList(4); + + if (result.containsKey(Oid.CPU_TIME__USER)) { + user = new SimpleQueryResult(result.get(Oid.CPU_TIME__USER).toDouble()); + user.setKey("user"); + user.setAttribute("cpu", "cpu0").setAttribute("state", "user"); + output.add(user); + } + if (result.containsKey(Oid.CPU_TIME__SYSTEM)) { + system = new SimpleQueryResult(result.get(Oid.CPU_TIME__SYSTEM).toDouble()); + system.setKey("system"); + system.setAttribute("cpu", "cpu0").setAttribute("state", "system"); + output.add(system); + } + if (result.containsKey(Oid.CPU_TIME__IDLE)) { + idle = new SimpleQueryResult(result.get(Oid.CPU_TIME__IDLE).toDouble()); + idle.setKey("idle"); + idle.setAttribute("cpu", "cpu0").setAttribute("state", "idle"); + output.add(idle); + } + if (result.containsKey(Oid.CPU_TIME__NICE)) { + nice = new SimpleQueryResult(result.get(Oid.CPU_TIME__NICE).toDouble()); + nice.setKey("nice"); + nice.setAttribute("cpu", "cpu0").setAttribute("state", "nice"); + output.add(nice); + } + + if (output.isEmpty()) { + return null; + } + return output; + } + + + public static List getMemUsageResults(Map result) { + List output = new ArrayList(4); + if (!result.containsKey(Oid.MEMORY_USAGE__TOTAL)) { + return null; + } + long total = SnmpValue.getLong(result, Oid.MEMORY_USAGE__TOTAL, 0L); + if (total <= 0) { + logger.log(Level.SEVERE, "Invalid value of total memory: " + total); + return null; + } + long free = SnmpValue.getLong(result, Oid.MEMORY_USAGE__FREE, 0L); + long cached = SnmpValue.getLong(result, Oid.MEMORY_USAGE__CACHED, 0L); + long buffered = SnmpValue.getLong(result, Oid.MEMORY_USAGE__BUFFERED, 0L); + long used = total - free - cached - buffered; + + output.add( new SimpleQueryResult(used * 1024).setKey("used").setAttribute("state", "used") ); + output.add( new SimpleQueryResult(free * 1024).setKey("free").setAttribute("state", "free") ); + output.add( new SimpleQueryResult(buffered * 1024).setKey("buffered").setAttribute("state", "buffered") ); + output.add( new SimpleQueryResult(cached * 1024).setKey("cached").setAttribute("state", "cached") ); + + return output; + } + +} \ No newline at end of file diff --git a/internal/otel-dc/build.gradle b/internal/otel-dc/build.gradle index 7c0d60e..9cdcb90 100644 --- a/internal/otel-dc/build.gradle +++ b/internal/otel-dc/build.gradle @@ -4,7 +4,7 @@ plugins { } group = "com.instana.dc" -version = "0.9.8" +version = "0.9.9" repositories { mavenCentral() diff --git a/internal/otel-dc/src/main/java/com/instana/dc/SimpleQueryResult.java b/internal/otel-dc/src/main/java/com/instana/dc/SimpleQueryResult.java index 7631917..d9c73a3 100644 --- a/internal/otel-dc/src/main/java/com/instana/dc/SimpleQueryResult.java +++ b/internal/otel-dc/src/main/java/com/instana/dc/SimpleQueryResult.java @@ -24,8 +24,9 @@ public String getKey() { return key; } - public void setKey(String key) { + public SimpleQueryResult setKey(String key) { this.key = key; + return this; } public Map getAttributes() { diff --git a/internal/simp-snmp/.gitignore b/internal/simp-snmp/.gitignore new file mode 100644 index 0000000..b63da45 --- /dev/null +++ b/internal/simp-snmp/.gitignore @@ -0,0 +1,42 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/internal/simp-snmp/build.gradle b/internal/simp-snmp/build.gradle new file mode 100644 index 0000000..fb88c2e --- /dev/null +++ b/internal/simp-snmp/build.gradle @@ -0,0 +1,37 @@ +plugins { + id 'java-library' + id 'maven-publish' +} + +group = "com.instana.dc" +version = "0.1.0" + +repositories { + mavenCentral() +} + +dependencies { + api("org.snmp4j:snmp4j-agent:3.8.1") + + testImplementation platform('org.junit:junit-bom:5.9.1') + testImplementation 'org.junit.jupiter:junit-jupiter' +} + +test { + useJUnitPlatform() +} + +tasks.named('jar') { + manifest { + attributes('Implementation-Title': project.name, + 'Implementation-Version': project.version) + } +} + +publishing { + publications { + maven(MavenPublication) { + from components.java + } + } +} diff --git a/internal/simp-snmp/gradle/wrapper/gradle-wrapper.jar b/internal/simp-snmp/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..249e583 Binary files /dev/null and b/internal/simp-snmp/gradle/wrapper/gradle-wrapper.jar differ diff --git a/internal/simp-snmp/gradle/wrapper/gradle-wrapper.properties b/internal/simp-snmp/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..3e3d4ab --- /dev/null +++ b/internal/simp-snmp/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Fri Dec 22 14:40:19 CST 2023 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/internal/simp-snmp/gradlew b/internal/simp-snmp/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/internal/simp-snmp/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/internal/simp-snmp/gradlew.bat b/internal/simp-snmp/gradlew.bat new file mode 100644 index 0000000..ac1b06f --- /dev/null +++ b/internal/simp-snmp/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/internal/simp-snmp/settings.gradle b/internal/simp-snmp/settings.gradle new file mode 100644 index 0000000..b227275 --- /dev/null +++ b/internal/simp-snmp/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'simp-snmp' + diff --git a/internal/simp-snmp/src/main/java/com/instana/simpsnmp/SimpSnmp.java b/internal/simp-snmp/src/main/java/com/instana/simpsnmp/SimpSnmp.java new file mode 100644 index 0000000..11118f6 --- /dev/null +++ b/internal/simp-snmp/src/main/java/com/instana/simpsnmp/SimpSnmp.java @@ -0,0 +1,162 @@ +/* + * (c) Copyright IBM Corp. 2024 + * (c) Copyright Instana Inc. + */ +package com.instana.simpsnmp; + +import org.snmp4j.CommunityTarget; +import org.snmp4j.PDU; +import org.snmp4j.Snmp; +import org.snmp4j.TransportMapping; +import org.snmp4j.event.ResponseEvent; +import org.snmp4j.mp.SnmpConstants; +import org.snmp4j.smi.*; +import org.snmp4j.transport.DefaultUdpTransportMapping; +import org.snmp4j.util.DefaultPDUFactory; +import org.snmp4j.util.TableEvent; +import org.snmp4j.util.TableUtils; + +import java.io.Closeable; +import java.io.IOException; +import java.util.*; +import java.util.stream.Collectors; + +/** + * Simple SNMP agent + */ +public class SimpSnmp implements Closeable { + private final String endpoint; + private final String community; + private final int retries; + private final int timeout; + private final int version; + + private final CommunityTarget
myTarget; + private final TransportMapping transport; + private final Snmp protocol; + + public SimpSnmp(String endpoint, String community, Integer retries, Integer timeout, Integer version) throws IOException { + this.endpoint = endpoint; + this.community = community != null ? community : "public"; + this.retries = retries != null ? retries : 3; + this.timeout = timeout != null ? timeout : 600; + this.version = version != null ? version : SnmpConstants.version2c; + + myTarget = new CommunityTarget<>(); + Address deviceAdd = GenericAddress.parse(this.endpoint); + myTarget.setAddress(deviceAdd); + myTarget.setCommunity(new OctetString(this.community)); + myTarget.setRetries(this.retries); + myTarget.setTimeout(this.timeout); + myTarget.setVersion(this.version); + + transport = new DefaultUdpTransportMapping(); + transport.listen(); + protocol = new Snmp(transport); + } + + public SimpSnmp(String endpoint, String community) throws IOException { + this(endpoint, community, null, null, null); + } + + public SimpSnmp(String endpoint) throws IOException { + this(endpoint, null, null, null, null); + } + + public String getEndpoint() { + return endpoint; + } + + public String getCommunity() { + return community; + } + + public int getRetries() { + return retries; + } + + public int getTimeout() { + return timeout; + } + + public int getVersion() { + return version; + } + + public TransportMapping getTransport() { + return transport; + } + + public Snmp getProtocol() { + return protocol; + } + + public void close() throws IOException { + transport.close(); + } + + public Map queryScalarOids(List oids) throws IOException { + PDU request = new PDU(); + request.setType(PDU.GET); + for (OID oid : oids) { + request.add(new VariableBinding(oid)); + } + ResponseEvent
responseEvent = protocol.send(request, myTarget); + PDU response = responseEvent.getResponse(); + Map result = new HashMap<>(); + if (response != null) { + for (int i = 0; i < response.size(); i++) { + VariableBinding vb = response.get(i); + SnmpValue snmpValue = SnmpValue.getValue(vb); + if (snmpValue != null) { + result.put(snmpValue.getOid(), snmpValue); + } + } + } + return result; + } + + public Map queryScalarOids(OID... oids) throws IOException { + return queryScalarOids(Arrays.stream(oids).collect(Collectors.toList())); + } + + private static OID findParentOid(List oids, OID oid1) { + for (OID oid : oids) { + if (oid1.startsWith(oid)) { + return oid; + } + } + return oid1; + } + + public List> queryColumnOids(List oids) throws IOException { + TableUtils tableUtils = new TableUtils(protocol, new DefaultPDUFactory(PDU.GETBULK)); + + List events = tableUtils.getTable(myTarget, oids.toArray(OID[]::new), null, null); + + List> result = new ArrayList<>(); + for (TableEvent event : events) { + if (event.isError()) { + System.err.println("SNMP Error: " + event.getErrorMessage()); + } else { + VariableBinding[] vbs = event.getColumns(); + if (vbs != null) { + Map result1 = new HashMap<>(); + for (VariableBinding vb : vbs) { + SnmpValue snmpValue = SnmpValue.getValue(vb); + if (snmpValue != null) { + result1.put(findParentOid(oids, snmpValue.getOid()), snmpValue); + } + } + result.add(result1); + } + } + } + return result; + } + + public List> queryColumnOids(OID... oids) throws IOException { + return queryColumnOids(Arrays.stream(oids).collect(Collectors.toList())); + } + +} \ No newline at end of file diff --git a/internal/simp-snmp/src/main/java/com/instana/simpsnmp/SnmpValue.java b/internal/simp-snmp/src/main/java/com/instana/simpsnmp/SnmpValue.java new file mode 100644 index 0000000..46822ca --- /dev/null +++ b/internal/simp-snmp/src/main/java/com/instana/simpsnmp/SnmpValue.java @@ -0,0 +1,149 @@ +/* + * (c) Copyright IBM Corp. 2024 + * (c) Copyright Instana Inc. + */ +package com.instana.simpsnmp; + +import org.snmp4j.smi.OID; +import org.snmp4j.smi.Variable; +import org.snmp4j.smi.VariableBinding; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Map; + +/** + * SNMP value + * + */ +public class SnmpValue { + private final int type; + private final long longValue; + + private final double doubleValue; + + private final String stringValue; + + public static final int TYPE_LONG = 1; + public static final int TYPE_DOUBLE = 2; + public static final int TYPE_STRING = 3; + + private OID oid = null; + + public SnmpValue(long longValue) { + this.type = TYPE_LONG; + this.longValue = longValue; + this.doubleValue = longValue; + this.stringValue = String.valueOf(longValue); + } + + public SnmpValue(double doubleValue) { + this.type = TYPE_DOUBLE; + this.doubleValue = doubleValue; + this.longValue = (long) doubleValue; + this.stringValue = String.valueOf(doubleValue); + } + + public SnmpValue(String stringValue, boolean parse) { + this.type = TYPE_STRING; + this.stringValue = stringValue; + if (parse) { + this.longValue = Long.parseLong(stringValue); + this.doubleValue = Double.parseDouble(stringValue); + } else { + this.longValue = 0; + this.doubleValue = 0; + } + } + + public SnmpValue(String stringValue) { + this(stringValue, false); + } + + public int getType() { + return type; + } + + public long toLong() { + return longValue; + } + + public double toDouble() { + return doubleValue; + } + + public String toString() { + return stringValue; + } + + public OID getOid() { + return oid; + } + + public SnmpValue setOid(OID oid) { + this.oid = oid; + return this; + } + + private static double opaqueHexToFloat(String hexString) { + String removeColonString = hexString.replaceAll("[:\\s]", ""); + // Remove the prefix '9f78' + String cleanHexString = removeColonString.substring(6); + + // Convert hex string to byte array + byte[] bytes = new byte[cleanHexString.length() / 2]; + for (int i = 0; i < bytes.length; i++) { + int index = i * 2; + int j = Integer.parseInt(cleanHexString.substring(index, index + 2), 16); + bytes[i] = (byte) j; + } + + // Convert byte array to float + ByteBuffer buffer = ByteBuffer.wrap(bytes); + buffer.order(ByteOrder.BIG_ENDIAN); // Ensure big-endian order + return buffer.getFloat(); + } + + public static SnmpValue getValue(VariableBinding vb) { + Variable v = vb.getVariable(); + if (v == null) { + return null; + } + String type = v.getSyntaxString(); + SnmpValue snmpValue; + if ("Opaque".equals(type)) { + snmpValue= new SnmpValue(opaqueHexToFloat(v.toString())); + } else if ("OCTET STRING".equals(type)) { + snmpValue= new SnmpValue(v.toString()); + } else { + snmpValue= new SnmpValue(v.toLong()); + } + return snmpValue.setOid(new OID(vb.getOid())); + } + + public static String getString(Map map, OID key, String defaultValue) { + SnmpValue result = map.get(key); + if (result != null) { + return result.toString(); + } + return defaultValue; + } + + public static Double getDouble(Map map, OID key, Double defaultValue) { + SnmpValue result = map.get(key); + if (result != null) { + return result.toDouble(); + } + return defaultValue; + } + + public static Long getLong(Map map, OID key, Long defaultValue) { + SnmpValue result = map.get(key); + if (result != null) { + return result.toLong(); + } + return defaultValue; + } +} + + diff --git a/internal/simp-snmp/src/test/java/com/instana/simpsnmp/Oid.java b/internal/simp-snmp/src/test/java/com/instana/simpsnmp/Oid.java new file mode 100644 index 0000000..3e87864 --- /dev/null +++ b/internal/simp-snmp/src/test/java/com/instana/simpsnmp/Oid.java @@ -0,0 +1,41 @@ +package com.instana.simpsnmp; + +import org.snmp4j.smi.OID; + +public class Oid { + //scalar_oids: + public static final OID CPU_TIME__USER = new OID(".1.3.6.1.4.1.2021.11.50.0"); + public static final OID CPU_TIME__SYSTEM = new OID(".1.3.6.1.4.1.2021.11.52.0"); + public static final OID CPU_TIME__IDLE = new OID(".1.3.6.1.4.1.2021.11.53.0"); + public static final OID CPU_TIME__NICE = new OID(".1.3.6.1.4.1.2021.11.51.0"); + public static final OID CPU_LOAD_1M = new OID(".1.3.6.1.4.1.2021.10.1.6.1"); + public static final OID CPU_LOAD_5M = new OID(".1.3.6.1.4.1.2021.10.1.6.2"); + public static final OID CPU_LOAD_15M = new OID(".1.3.6.1.4.1.2021.10.1.6.3"); + public static final OID MEMORY_USAGE__TOTAL = new OID(".1.3.6.1.4.1.2021.4.5.0"); + public static final OID MEMORY_USAGE__FREE = new OID(".1.3.6.1.4.1.2021.4.6.0"); + public static final OID MEMORY_USAGE__BUFFERED = new OID(".1.3.6.1.4.1.2021.4.14.0"); + public static final OID MEMORY_USAGE__CACHED = new OID(".1.3.6.1.4.1.2021.4.15.0"); + public static final OID HOST_NAME = new OID("1.3.6.1.2.1.1.5.0"); + public static final OID OS_TYPE = new OID("1.3.6.1.2.1.1.1.0"); + + + //column_oids: + public static final OID DISK_IO__READ = new OID(".1.3.6.1.4.1.2021.13.15.1.1.3"); + public static final OID DISK_IO__WRITE = new OID(".1.3.6.1.4.1.2021.13.15.1.1.4"); + public static final OID DISKDEVICE = new OID(".1.3.6.1.4.1.2021.13.15.1.1.2"); + + public static final OID FILESYSTEMDEVICE = new OID(".1.3.6.1.2.1.25.2.3.1.3"); + public static final OID FILESYSTEM_USAGE__USED = new OID(".1.3.6.1.2.1.25.2.3.1.6"); + public static final OID FILESYSTEM_USAGE__ALL = new OID(".1.3.6.1.2.1.25.2.3.1.5"); + public static final OID FILESYSTEM_USAGE__UNIT = new OID(".1.3.6.1.2.1.25.2.3.1.4"); + + public static final OID NETWORKDEVICE = new OID(".1.3.6.1.2.1.2.2.1.2"); + public static final OID NETWORK_DROPPED_RECEIVE = new OID(".1.3.6.1.2.1.2.2.1.13"); + public static final OID NETWORK_DROPPED_TRANSMIT = new OID(".1.3.6.1.2.1.2.2.1.19"); + public static final OID NETWORK_ERRORS_RECEIVE = new OID(".1.3.6.1.2.1.2.2.1.14"); + public static final OID NETWORK_ERRORS_TRANSMIT = new OID(".1.3.6.1.2.1.2.2.1.20"); + public static final OID NETWORK_IO_RECEIVE = new OID(".1.3.6.1.2.1.2.2.1.10"); + 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"); +} diff --git a/internal/simp-snmp/src/test/java/com/instana/simpsnmp/SimpSnmpTest.java b/internal/simp-snmp/src/test/java/com/instana/simpsnmp/SimpSnmpTest.java new file mode 100644 index 0000000..624125a --- /dev/null +++ b/internal/simp-snmp/src/test/java/com/instana/simpsnmp/SimpSnmpTest.java @@ -0,0 +1,122 @@ +/* + * (c) Copyright IBM Corp. 2024 + * (c) Copyright Instana Inc. + */ +package com.instana.simpsnmp; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.snmp4j.smi.OID; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +// By its nature, this is an integration tests. +public class SimpSnmpTest { + private static final String FILE_SNMP_HOST = "/tmp/snmphost"; + private static SimpSnmp simpSnmp = null; + + private static void noHostErr() { + System.err.println("cannot access file in " + FILE_SNMP_HOST); + } + + @BeforeAll + public static void setupSnmpServer() { + List lines = Collections.emptyList(); + try { + String snmpHost; + lines = Files.readAllLines(Paths.get(FILE_SNMP_HOST), StandardCharsets.UTF_8); + snmpHost = lines.get(0).trim(); + System.out.println("snmpHost: " + snmpHost); + simpSnmp = new SimpSnmp(snmpHost); + } catch (Exception e) { + noHostErr(); + } + } + + @Test + void testQueryScalarOids() throws Exception { + if (simpSnmp == null) { + noHostErr(); + return; + } + Map result = simpSnmp.queryScalarOids(Oid.HOST_NAME, Oid.OS_TYPE, + Oid.CPU_TIME__USER, Oid.CPU_LOAD_1M, Oid.MEMORY_USAGE__TOTAL); + assertEquals(result.size(), 5); + System.out.println("=== testQueryScalarOids ==="); + result.forEach((key, value) -> System.out.println(key + ":" + value)); + } + + @Test + void testQueryColumnOids1() throws Exception { + if (simpSnmp == null) { + noHostErr(); + return; + } + List> result = simpSnmp.queryColumnOids(Oid.DISKDEVICE, Oid.DISK_IO__READ, Oid.DISK_IO__WRITE); + assertNotEquals(result.size(), 0); + System.out.println("=== testQueryColumnOids1 ==="); + for (Map result1 : result) { + System.out.println("------>>"); + result1.forEach((key, value) -> System.out.println(key + ":" + value)); + } + } + + @Test + void testQueryColumnOids2() throws Exception { + if (simpSnmp == null) { + noHostErr(); + return; + } + List> result = simpSnmp.queryColumnOids( + Oid.FILESYSTEMDEVICE, Oid.FILESYSTEM_USAGE__USED, Oid.FILESYSTEM_USAGE__ALL, Oid.FILESYSTEM_USAGE__UNIT); + assertNotEquals(result.size(), 0); + System.out.println("=== testQueryColumnOids2 ==="); + for (Map result1 : result) { + System.out.println("------>>"); + result1.forEach((key, value) -> System.out.println(key + ":" + value)); + } + } + + @Test + void testQueryColumnOids3() throws Exception { + if (simpSnmp == null) { + noHostErr(); + return; + } + List> result = simpSnmp.queryColumnOids(Oid.NETWORKDEVICE, + Oid.NETWORK_DROPPED_RECEIVE, Oid.NETWORK_DROPPED_TRANSMIT, + Oid.NETWORK_ERRORS_RECEIVE, Oid.NETWORK_ERRORS_TRANSMIT); + assertNotEquals(result.size(), 0); + System.out.println("=== testQueryColumnOids3 ==="); + for (Map result1 : result) { + System.out.println("------>>"); + result1.forEach((key, value) -> System.out.println(key + ":" + value)); + } + } + + @Test + void testQueryColumnOids4() throws Exception { + if (simpSnmp == null) { + noHostErr(); + return; + } + List> result = simpSnmp.queryColumnOids(Oid.NETWORKDEVICE, + Oid.NETWORK_IO_RECEIVE, Oid.NETWORK_IO_TRANSMIT, + Oid.NETWORK_PACKAGES_RECEIVE, Oid.NETWORK_PACKAGES_TRANSMIT); + assertNotEquals(result.size(), 0); + System.out.println("=== testQueryColumnOids4 ==="); + for (Map result1 : result) { + System.out.println("------>>"); + result1.forEach((key, value) -> System.out.println(key + ":" + value)); + } + } + +} diff --git a/llm/build.gradle b/llm/build.gradle index 5515c02..b9c9e8a 100644 --- a/llm/build.gradle +++ b/llm/build.gradle @@ -4,7 +4,7 @@ plugins { } group = "com.instana.dc" -version = "1.0.1" +version = "1.0.2" sourceCompatibility = 11 targetCompatibility = 11 @@ -27,7 +27,7 @@ dependencies { implementation("com.google.protobuf:protobuf-java-util:3.23.4") implementation("com.linecorp.armeria:armeria:1.27.3") implementation("com.linecorp.armeria:armeria-grpc:1.27.3") - implementation(files("libs/otel-dc-0.9.8.jar")) + implementation(files("libs/otel-dc-0.9.9.jar")) testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1") diff --git a/llm/libs/otel-dc-0.9.8.jar b/llm/libs/otel-dc-0.9.9.jar similarity index 86% rename from llm/libs/otel-dc-0.9.8.jar rename to llm/libs/otel-dc-0.9.9.jar index 0195378..f3dc576 100644 Binary files a/llm/libs/otel-dc-0.9.8.jar and b/llm/libs/otel-dc-0.9.9.jar differ diff --git a/rdb/build.gradle b/rdb/build.gradle index a048ad9..4e9d8b2 100644 --- a/rdb/build.gradle +++ b/rdb/build.gradle @@ -4,7 +4,7 @@ plugins { } group = "com.instana.dc" -version = "0.5.3" +version = "0.5.4" repositories { mavenCentral() @@ -20,7 +20,7 @@ dependencies { implementation("io.opentelemetry:opentelemetry-exporter-sender-okhttp:1.34.1") implementation("io.opentelemetry.semconv:opentelemetry-semconv:1.23.1-alpha") implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.16.0-rc1") - implementation(files("libs/otel-dc-0.9.8.jar")) + implementation(files("libs/otel-dc-0.9.9.jar")) implementation(files("libs/ngdbc-2.4.64.jar")) implementation("org.apache.commons:commons-dbcp2:2.11.0") diff --git a/rdb/config/config.yaml b/rdb/config/config.yaml index 74cac8e..37778e4 100644 --- a/rdb/config/config.yaml +++ b/rdb/config/config.yaml @@ -11,8 +11,8 @@ instances: db.name: myDB1 #Data collector properties: - poll.interval: 30 - callback.interval: 20 + poll.interval: 25 + callback.interval: 30 otel.backend.url: http://localhost:4317 #otel.backend.using.http: true #otel.backend.url: https://host.docker.internal:8991/v1/metrics @@ -29,8 +29,8 @@ instances: db.name: myDB2 #Data collector properties: - poll.interval: 30 - callback.interval: 20 + poll.interval: 25 + callback.interval: 30 otel.backend.url: http://localhost:4317 #otel.backend.using.http: true #otel.backend.url: https://host.docker.internal:8991/v1/metrics diff --git a/rdb/libs/otel-dc-0.9.8.jar b/rdb/libs/otel-dc-0.9.9.jar similarity index 86% rename from rdb/libs/otel-dc-0.9.8.jar rename to rdb/libs/otel-dc-0.9.9.jar index 0195378..f3dc576 100644 Binary files a/rdb/libs/otel-dc-0.9.8.jar and b/rdb/libs/otel-dc-0.9.9.jar differ