Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/main/java/org/metricshub/snmp/client/OfflineSnmpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
Expand All @@ -51,8 +52,27 @@ private static final class SnmpValue {
}
}

// Maps the OIDs to their corresponding SnmpValue (type + value).
private final NavigableMap<String, SnmpValue> oidValues = new TreeMap<>();
/**
* Compares OIDs arc by arc, numerically: {@code 1.2} &lt; {@code 1.2.1} &lt; {@code 1.10}.
* Numeric arcs compare by length first, then by digits, which avoids parsing arcs larger than an int.
*/
static final Comparator<String> OID_COMPARATOR = (a, b) -> {
final String[] x = a.split("\\.");
final String[] y = b.split("\\.");
for (int i = 0; i < Math.min(x.length, y.length); i++) {
int c = Integer.compare(x[i].length(), y[i].length());
if (c == 0) {
c = x[i].compareTo(y[i]);
}
if (c != 0) {
return c;
}
}
return Integer.compare(x.length, y.length);
};

// Maps the OIDs, in OID order, to their corresponding SnmpValue (type + value).
private final NavigableMap<String, SnmpValue> oidValues = new TreeMap<>(OID_COMPARATOR);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -204,4 +205,32 @@ void testTableFromThirdFile() {
assertEquals("second", row2.get(1));
assertEquals("2", row2.get(2));
}

@Test
void testOidOrderIsNumeric() throws IOException {
final Path dir = Files.createTempDirectory("snmpOidOrder");
final Path file = dir.resolve("order.walk");
// Column 2 has 3 rows, column 10 only 1: string order would put column 10 first.
final String dump =
"1.3.6.1.4.1.9.1.10.2\tASN_OCTET_STR\tfw\n" +
"1.3.6.1.4.1.9.1.2.1\tASN_OCTET_STR\ta\n" +
"1.3.6.1.4.1.9.1.2.2\tASN_OCTET_STR\tb\n" +
"1.3.6.1.4.1.9.1.2.10\tASN_OCTET_STR\tc\n";
Files.write(file, dump.getBytes(StandardCharsets.UTF_8));
final OfflineSnmpClient c = new OfflineSnmpClient(dir);
Files.delete(file);
Files.delete(dir);

assertTrue(c.getNext("1.3.6.1.4.1.9.1").startsWith("1.3.6.1.4.1.9.1.2.1\t"));
assertTrue(c.getNext("1.3.6.1.4.1.9.1.2.2").startsWith("1.3.6.1.4.1.9.1.2.10\t"));
assertTrue(c.getNext("1.3.6.1.4.1.9.1.2.10").startsWith("1.3.6.1.4.1.9.1.10.2\t"));

final List<List<String>> rows = c.table("1.3.6.1.4.1.9.1", new String[] { "ID", "2", "10" });
assertEquals(3, rows.size());
assertEquals(Arrays.asList("1", "a", ""), rows.get(0));
assertEquals(Arrays.asList("2", "b", "fw"), rows.get(1));
assertEquals(Arrays.asList("10", "c", ""), rows.get(2));

assertTrue(c.walk("1.3.6.1.4.1.9.1").startsWith("1.3.6.1.4.1.9.1.2.1\t"));
}
}
Loading