From ec37ebacf82de9149702da37e63bd3c98ab73c1f Mon Sep 17 00:00:00 2001 From: Elyes Cherfa Date: Tue, 3 Dec 2024 01:16:02 +0100 Subject: [PATCH] Issue #424: Add CLI options for specific query execution - SNMP * Refactored tests to make them more concise. * Refactored executeQuery on SNMP Extension to make it more concise. * Reformatted the code. * Tested the client. --- .../metricshub/cli/snmp/SnmpCliTest.java | 58 +++------------- .../extension/snmp/SnmpExtension.java | 67 +++++++------------ .../extension/snmp/SnmpExtensionTest.java | 7 +- 3 files changed, 37 insertions(+), 95 deletions(-) diff --git a/metricshub-agent/src/test/java/org/sentrysoftware/metricshub/cli/snmp/SnmpCliTest.java b/metricshub-agent/src/test/java/org/sentrysoftware/metricshub/cli/snmp/SnmpCliTest.java index 8f670cb08..79a25a1c5 100644 --- a/metricshub-agent/src/test/java/org/sentrysoftware/metricshub/cli/snmp/SnmpCliTest.java +++ b/metricshub-agent/src/test/java/org/sentrysoftware/metricshub/cli/snmp/SnmpCliTest.java @@ -15,7 +15,7 @@ class SnmpCliTest { SnmpCli snmpCli; CommandLine commandLine; - static String SNMP_OID = "1.3.6.1.4.1.674.10892.5.5.1.20.130.4"; + static String SNMP_OID = "1.3.6.1.4.1.64.58.5.5.1.20.10.4"; static String SNMP_VERSION = "v2c"; static String SNMP_COMMUNITY = "public"; @@ -24,48 +24,10 @@ void initCli() { commandLine = new CommandLine(snmpCli); } - void initSnmpGet() { - initCli(); - - commandLine.execute( - "hostname", - "--snmp-get", - SNMP_OID, - "--snmp", - SNMP_VERSION, - "--community", - SNMP_COMMUNITY, - "--retry", - "1000", - "--retry", - "6000" - ); - } - - void initSnmpGetNext() { - initCli(); - + void execute(String snmpMethod) { commandLine.execute( "hostname", - "--snmp-getnext", - SNMP_OID, - "--snmp", - SNMP_VERSION, - "--community", - SNMP_COMMUNITY, - "--retry", - "1000", - "--retry", - "6000" - ); - } - - void initSnmpWalk() { - initCli(); - - commandLine.execute( - "hostname", - "--snmp-walk", + snmpMethod, SNMP_OID, "--snmp", SNMP_VERSION, @@ -80,27 +42,29 @@ void initSnmpWalk() { @Test void testExecute() { - initSnmpGet(); + initCli(); + execute("--snmp-get"); assertEquals(SNMP_OID, snmpCli.get); - initSnmpGetNext(); + execute("--snmp-getnext"); assertEquals(SNMP_OID, snmpCli.getNext); - initSnmpWalk(); + execute("--snmp-walk"); assertEquals(SNMP_OID, snmpCli.walk); } @Test void testGetQuery() { - initSnmpGet(); + initCli(); + execute("--snmp-get"); JsonNode snmpQuery = snmpCli.getQuery(); assertEquals("get", snmpQuery.get("action").asText()); assertEquals(SNMP_OID, snmpQuery.get("oid").asText()); - initSnmpGetNext(); + execute("--snmp-getnext"); snmpQuery = snmpCli.getQuery(); assertEquals("getNext", snmpQuery.get("action").asText()); assertEquals(SNMP_OID, snmpQuery.get("oid").asText()); - initSnmpWalk(); + execute("--snmp-walk"); snmpQuery = snmpCli.getQuery(); assertEquals("walk", snmpQuery.get("action").asText()); assertEquals(SNMP_OID, snmpQuery.get("oid").asText()); diff --git a/metricshub-snmp-extension/src/main/java/org/sentrysoftware/metricshub/extension/snmp/SnmpExtension.java b/metricshub-snmp-extension/src/main/java/org/sentrysoftware/metricshub/extension/snmp/SnmpExtension.java index cdc70c995..3fb51036c 100644 --- a/metricshub-snmp-extension/src/main/java/org/sentrysoftware/metricshub/extension/snmp/SnmpExtension.java +++ b/metricshub-snmp-extension/src/main/java/org/sentrysoftware/metricshub/extension/snmp/SnmpExtension.java @@ -109,55 +109,34 @@ public String executeQuery(IConfiguration configuration, JsonNode query, PrintWr final String hostname = configuration.getHostname(); String result = "Failed Executing SNMP query"; final String action = query.get("action").asText(); - final String oId = query.get("oid").asText(); - final String exceptionMessage = "Hostname {} - Error while executing SNMP {} query. Message: {}"; - printWriter.println("Executing query from SNMP Extension"); + // Printing the SNMP request + printWriter.println(String.format("Hostname %s - Executing SNMP %s query:", hostname, action)); + printWriter.println(String.format("OID: %s", oId)); printWriter.flush(); - switch (action) { - case GET: - try { - displayQuery(printWriter, hostname, GET, oId); - result = snmpRequestExecutor.executeSNMPGet(oId, snmpConfiguration, hostname, false); - displayQueryResult(printWriter, result); - } catch (Exception e) { - log.debug(exceptionMessage, hostname, GET, e); - } - break; - case GET_NEXT: - try { - displayQuery(printWriter, hostname, GET_NEXT, oId); - result = snmpRequestExecutor.executeSNMPGetNext(oId, snmpConfiguration, hostname, false); - displayQueryResult(printWriter, result); - } catch (Exception e) { - log.debug(exceptionMessage, hostname, GET_NEXT, e); - } - break; - case WALK: - try { - displayQuery(printWriter, hostname, WALK, oId); - result = snmpRequestExecutor.executeSNMPWalk(oId, snmpConfiguration, hostname, false); - displayQueryResult(printWriter, result); - } catch (Exception e) { - log.debug(exceptionMessage, hostname, WALK, e); - } - break; - default: - throw new IllegalArgumentException("Hostname {} - Invalid SNMP Operation"); - } - return result; - } + try { + if (action.equals(GET)) { + result = snmpRequestExecutor.executeSNMPGet(oId, snmpConfiguration, hostname, false); + } else if (action.equals(GET_NEXT)) { + result = snmpRequestExecutor.executeSNMPGetNext(oId, snmpConfiguration, hostname, false); + } else if (action.equals(WALK)) { + result = snmpRequestExecutor.executeSNMPWalk(oId, snmpConfiguration, hostname, false); + } else { + throw new IllegalArgumentException(String.format("Hostname %s - Invalid SNMP Operation", hostname)); + } - public void displayQuery(final PrintWriter printWriter, final String hostname, final String query, final String oId) { - printWriter.println(String.format("Hostname %s - Executing SNMP %s query:\n", hostname, query)); - printWriter.println(String.format("OID: %s", oId)); - printWriter.flush(); - } + // Printing the result on the CLI + printWriter.print("\n\u001B[34m"); + printWriter.print("Result: "); + printWriter.print("\u001B[0m"); + printWriter.print(result); + printWriter.flush(); + } catch (Exception e) { + log.debug("Hostname {} - Error while executing SNMP {} query. Message: {}", hostname, action, e); + } - public void displayQueryResult(final PrintWriter printWriter, final String result) { - printWriter.println(String.format("Result: %s", result)); - printWriter.flush(); + return result; } } diff --git a/metricshub-snmp-extension/src/test/java/org/sentrysoftware/metricshub/extension/snmp/SnmpExtensionTest.java b/metricshub-snmp-extension/src/test/java/org/sentrysoftware/metricshub/extension/snmp/SnmpExtensionTest.java index 0eaa72a6b..8f661dd72 100644 --- a/metricshub-snmp-extension/src/test/java/org/sentrysoftware/metricshub/extension/snmp/SnmpExtensionTest.java +++ b/metricshub-snmp-extension/src/test/java/org/sentrysoftware/metricshub/extension/snmp/SnmpExtensionTest.java @@ -3,7 +3,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -673,9 +672,9 @@ void testThrowsExceptionQuery() throws Exception { snmpQueryConfiguration.set("action", new TextNode("")); snmpQueryConfiguration.set("oid", new TextNode(SnmpExtension.SNMP_OID)); - assertThrows( - Exception.class, - () -> snmpExtension.executeQuery(snmpConfiguration, snmpQueryConfiguration, new PrintWriter(new StringWriter())) + assertEquals( + "Failed Executing SNMP query", + snmpExtension.executeQuery(snmpConfiguration, snmpQueryConfiguration, new PrintWriter(new StringWriter())) ); } }