diff --git a/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/config/AdditionalConnector.java b/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/config/AdditionalConnector.java index f6a52ef7f..c55f5e71a 100644 --- a/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/config/AdditionalConnector.java +++ b/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/config/AdditionalConnector.java @@ -2,7 +2,7 @@ /*- * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲ - * MetricsHub Engine + * MetricsHub Agent * ჻჻჻჻჻჻ * Copyright 2023 - 2024 Sentry Software * ჻჻჻჻჻჻ diff --git a/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/deserialization/AdditionalConnectorsParsingResult.java b/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/connector/AdditionalConnectorsParsingResult.java similarity index 93% rename from metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/deserialization/AdditionalConnectorsParsingResult.java rename to metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/connector/AdditionalConnectorsParsingResult.java index 94b2372f9..eb8bb047e 100644 --- a/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/deserialization/AdditionalConnectorsParsingResult.java +++ b/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/connector/AdditionalConnectorsParsingResult.java @@ -1,8 +1,8 @@ -package org.sentrysoftware.metricshub.agent.deserialization; +package org.sentrysoftware.metricshub.agent.connector; /*- * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲ - * MetricsHub Engine + * MetricsHub Agent * ჻჻჻჻჻჻ * Copyright 2023 - 2024 Sentry Software * ჻჻჻჻჻჻ diff --git a/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/helper/ConnectorVariablesLibraryParser.java b/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/connector/ConnectorVariablesLibraryParser.java similarity index 86% rename from metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/helper/ConnectorVariablesLibraryParser.java rename to metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/connector/ConnectorVariablesLibraryParser.java index 953f3a23c..5b6053f1e 100644 --- a/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/helper/ConnectorVariablesLibraryParser.java +++ b/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/connector/ConnectorVariablesLibraryParser.java @@ -1,4 +1,4 @@ -package org.sentrysoftware.metricshub.agent.helper; +package org.sentrysoftware.metricshub.agent.connector; /*- * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲ @@ -38,7 +38,6 @@ import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.sentrysoftware.metricshub.agent.config.AdditionalConnector; -import org.sentrysoftware.metricshub.agent.deserialization.AdditionalConnectorsParsingResult; import org.sentrysoftware.metricshub.engine.connector.model.Connector; import org.sentrysoftware.metricshub.engine.connector.model.identity.ConnectorDefaultVariable; import org.sentrysoftware.metricshub.engine.connector.parser.ConnectorParser; @@ -111,44 +110,15 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IO // Construct a variables map from the default connector variables. final Map defaultVariables = new HashMap<>(getDefaultConnectorVariables(connectorNode)); - // Parse the connector even if it is not configured as an additional connector. - // This ensures that the connector will function with the default variables if the user forces it. if (filteredConnectors.isEmpty()) { + // Parse the connector even if it is not configured as an additional connector. + // This ensures that the connector will function with the default variables if the user forces it. parseConnectorWithModifier(path, defaultVariables, connectorId, filename, connector -> {}); + } else { + // Parse and generate connectors that have been added and configured. + generateNewConnectors(path, filename, filteredConnectors, defaultVariables); } - // For each configuration, we create a new custom connector and a new variables map to be used in the connector update. - for (final Entry connectorConfigurationEntry : filteredConnectors.entrySet()) { - final String additionalConnectorId = connectorConfigurationEntry.getKey(); - final AdditionalConnector additionalConnectorValue = connectorConfigurationEntry.getValue(); - - // Add the connector to the host connectors set. - connectorsParsingResult - .getHostConnectors() - .add(additionalConnectorValue.isForce() ? "+" + additionalConnectorId : additionalConnectorId); - - // Retrieve and use default connector variables on this connector for this configuration. - final Map connectorVariables = new HashMap<>(defaultVariables); - - // Override the default connector variables by the connector variables that the user configured. - final Map configuredVariables = additionalConnectorValue.getVariables(); - if (configuredVariables != null) { - connectorVariables.putAll(configuredVariables); - } - // There are at least two additional connectors that use the current connector. - // This means that the compiled filename of these connectors needs to be modified. - if (filteredConnectors.size() > 1) { - parseConnectorWithModifier( - path, - connectorVariables, - additionalConnectorId, - filename, - connector -> connector.getConnectorIdentity().setCompiledFilename(additionalConnectorId) - ); - continue; - } - parseConnectorWithModifier(path, connectorVariables, connectorId, filename, connector -> {}); - } return FileVisitResult.CONTINUE; } @@ -203,12 +173,63 @@ private static Map getDefaultConnectorVariables(final JsonNode c final String variableName = entry.getKey(); final JsonNode variableValue = entry.getValue(); - connectorVariablesMap.put(variableName, variableValue.get("defaultValue").asText()); + final JsonNode defaultValue = variableValue.get("defaultValue"); + if (defaultValue != null && !defaultValue.isNull()) { + connectorVariablesMap.put(variableName, variableValue.get("defaultValue").asText()); + } }); return connectorVariablesMap; } + /** + * Generates custom connectors for each additional connector configuration. + * + *

This method iterates over the provided map of filtered connectors, generating a new {@link Connector} + * object for each. It handles the default and user-configured variables, ensuring that any forced connectors + * are correctly identified. The method can modify the compiled filename of connectors when needed.

+ * + * @param path The path to the directory containing the connector files. + * @param filename The filename of the connector file. + * @param filteredConnectors A map of filtered additional connectors, with their configurations. + * @param defaultVariables A map of default variables to be used during connector generation. + */ + private void generateNewConnectors( + final Path path, + final String filename, + final Map filteredConnectors, + final Map defaultVariables + ) { + // For each configuration, we create a new custom connector and a new variables map to be used in the connector update. + for (final Entry connectorConfigurationEntry : filteredConnectors.entrySet()) { + final String additionalConnectorId = connectorConfigurationEntry.getKey(); + final AdditionalConnector additionalConnectorValue = connectorConfigurationEntry.getValue(); + + // Add the connector to the host connectors set. + connectorsParsingResult + .getHostConnectors() + .add(additionalConnectorValue.isForce() ? "+" + additionalConnectorId : additionalConnectorId); + + // Retrieve and use default connector variables on this connector for this configuration. + final Map connectorVariables = new HashMap<>(defaultVariables); + + // Override the default connector variables by the connector variables that the user configured. + final Map configuredVariables = additionalConnectorValue.getVariables(); + if (configuredVariables != null) { + connectorVariables.putAll(configuredVariables); + } + // There are at least two additional connectors that use the current connector. + // This means that the compiled filename of these connectors needs to be modified. + parseConnectorWithModifier( + path, + connectorVariables, + additionalConnectorId, + filename, + connector -> connector.getConnectorIdentity().setCompiledFilename(additionalConnectorId) + ); + } + } + /** * Parses a connector file located at the specified path, using the provided variables and connector Id. * The parsed connector is then added to the custom connectors map, with the option to perform additional diff --git a/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/helper/ConfigHelper.java b/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/helper/ConfigHelper.java index 5f32917a6..0e597c3b5 100644 --- a/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/helper/ConfigHelper.java +++ b/metricshub-agent/src/main/java/org/sentrysoftware/metricshub/agent/helper/ConfigHelper.java @@ -66,8 +66,9 @@ import org.sentrysoftware.metricshub.agent.config.AlertingSystemConfig; import org.sentrysoftware.metricshub.agent.config.ResourceConfig; import org.sentrysoftware.metricshub.agent.config.ResourceGroupConfig; +import org.sentrysoftware.metricshub.agent.connector.AdditionalConnectorsParsingResult; +import org.sentrysoftware.metricshub.agent.connector.ConnectorVariablesLibraryParser; import org.sentrysoftware.metricshub.agent.context.MetricDefinitions; -import org.sentrysoftware.metricshub.agent.deserialization.AdditionalConnectorsParsingResult; import org.sentrysoftware.metricshub.agent.security.PasswordEncrypt; import org.sentrysoftware.metricshub.engine.common.exception.InvalidConfigurationException; import org.sentrysoftware.metricshub.engine.common.helpers.JsonHelper; diff --git a/metricshub-agent/src/test/java/org/sentrysoftware/metricshub/agent/helper/ConnectorVariablesLibraryParserTest.java b/metricshub-agent/src/test/java/org/sentrysoftware/metricshub/agent/helper/ConnectorVariablesLibraryParserTest.java index 51895ca98..c7a2cd6f5 100644 --- a/metricshub-agent/src/test/java/org/sentrysoftware/metricshub/agent/helper/ConnectorVariablesLibraryParserTest.java +++ b/metricshub-agent/src/test/java/org/sentrysoftware/metricshub/agent/helper/ConnectorVariablesLibraryParserTest.java @@ -12,7 +12,8 @@ import java.util.Set; import org.junit.jupiter.api.Test; import org.sentrysoftware.metricshub.agent.config.AdditionalConnector; -import org.sentrysoftware.metricshub.agent.deserialization.AdditionalConnectorsParsingResult; +import org.sentrysoftware.metricshub.agent.connector.AdditionalConnectorsParsingResult; +import org.sentrysoftware.metricshub.agent.connector.ConnectorVariablesLibraryParser; import org.sentrysoftware.metricshub.engine.connector.model.Connector; import org.sentrysoftware.metricshub.engine.connector.model.identity.Detection; import org.sentrysoftware.metricshub.engine.connector.model.identity.criterion.SnmpGetNextCriterion;