Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #203 : Refactor extensions code to check protocol efficiently #225

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
e79db5a
- Fixed error on compute (null table)
NassimBtk Jun 2, 2024
87ddbac
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 3, 2024
51aef13
Merge branch 'main' into feature/issue-203-Refactor-Extensions-code-t…
SafaeAJ Jun 3, 2024
31be857
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 3, 2024
d024d54
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 3, 2024
bc4fb13
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 3, 2024
0eadaee
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 4, 2024
125162c
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 4, 2024
974c845
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 4, 2024
65a46de
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 4, 2024
0681744
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 4, 2024
ad04d4d
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 4, 2024
c9f74d9
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 4, 2024
0cba537
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 4, 2024
93544ac
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 4, 2024
92a15f9
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
SafaeAJ Jun 4, 2024
05c7654
Issue #203: Refactor-Extensions-code-to-check-protocol-efficiently
NassimBtk Jun 5, 2024
9dd2034
Merge branch 'feature/issue-203-refactor-extensions-code-to-check-pro…
NassimBtk Jun 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public interface IProtocolExtension {
* telemetry manager.
*
* @param telemetryManager The telemetry manager to use for monitoring.
* @return true if the protocol check succeeds, false otherwise.
* @return Optional.of(true) if the protocol check succeeds, Optional.of(false) otherwise.
*/
boolean checkProtocol(TelemetryManager telemetryManager);
Optional<Boolean> checkProtocol(TelemetryManager telemetryManager);

/**
* Executes a source operation based on the given source and configuration within the telemetry manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

import java.util.List;
import java.util.Optional;
import lombok.Builder;
import lombok.NonNull;
import org.sentrysoftware.metricshub.engine.client.ClientsExecutor;
Expand Down Expand Up @@ -80,12 +81,21 @@ public void run() {
// Call the extensions to check the protocol health
final List<IProtocolExtension> protocolExtensions = extensionManager.findProtocolCheckExtensions(telemetryManager);
protocolExtensions.forEach(protocolExtension -> {
Optional<Boolean> protocolCheckResult = protocolExtension.checkProtocol(telemetryManager);

double metricValue;
if (!protocolCheckResult.isPresent()) {
metricValue = DOWN;
} else {
metricValue = protocolCheckResult.get() ? UP : DOWN;
}

// CHECKSTYLE:OFF
new MetricFactory()
.collectNumberMetric(
telemetryManager.getEndpointHostMonitor(),
"metricshub.host.up{protocol=\"" + protocolExtension.getIdentifier() + "\"}",
protocolExtension.checkProtocol(telemetryManager) ? UP : DOWN,
metricValue,
telemetryManager.getStrategyTime()
);
// CHECKSTYLE:OFF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.UnaryOperator;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -86,12 +87,10 @@ public Set<Class<? extends Criterion>> getSupportedCriteria() {
}

@Override
public boolean checkProtocol(TelemetryManager telemetryManager) {
public Optional<Boolean> checkProtocol(TelemetryManager telemetryManager) {
// Retrieve the hostname
final String hostname = telemetryManager.getHostConfiguration().getHostname();

log.info("Hostname {} - Performing protocol health check.", hostname);

// Create and set the HTTP result to null
String httpResult = null;

Expand All @@ -103,9 +102,10 @@ public boolean checkProtocol(TelemetryManager telemetryManager) {

// Stop the HTTP health check if there is not an HTTP configuration
if (httpConfiguration == null) {
return false;
return Optional.of(false);
}

log.info("Hostname {} - Performing protocol health check.", hostname);
log.info("Hostname {} - Checking HTTP protocol status. Sending GET request to '/'.", hostname);

// Execute HTTP test request
Expand All @@ -129,7 +129,7 @@ public boolean checkProtocol(TelemetryManager telemetryManager) {
);
}

return httpResult != null;
return Optional.of(httpResult != null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -107,10 +108,10 @@ void testCheckHttpDownHealth() {
.when(httpRequestExecutorMock)
.executeHttp(any(HttpRequest.class), anyBoolean(), any(TelemetryManager.class));

boolean result = httpExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = httpExtension.checkProtocol(telemetryManager);

// Assert the result
assertFalse(result);
assertFalse(result.get());
}

@Test
Expand All @@ -122,10 +123,10 @@ void testCheckHttpUpHealth() {
.when(httpRequestExecutorMock)
.executeHttp(any(HttpRequest.class), anyBoolean(), any(TelemetryManager.class));

boolean result = httpExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = httpExtension.checkProtocol(telemetryManager);

// Assert the result
assertTrue(result);
assertTrue(result.get());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.UnaryOperator;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -77,12 +78,10 @@ public Set<Class<? extends Criterion>> getSupportedCriteria() {
}

@Override
public boolean checkProtocol(TelemetryManager telemetryManager) {
public Optional<Boolean> checkProtocol(TelemetryManager telemetryManager) {
// Retrieve the hostname
String hostname = telemetryManager.getHostConfiguration().getHostname();

log.info("Hostname {} - Performing protocol health check.", hostname);

// Create and set the IPMI result to null
String ipmiResult = null;

Expand All @@ -94,9 +93,10 @@ public boolean checkProtocol(TelemetryManager telemetryManager) {

// Stop the IPMI health check if there is not an IPMI configuration
if (ipmiConfiguration == null) {
return false;
return Optional.empty();
}

log.info("Hostname {} - Performing protocol health check.", hostname);
log.info(
"Hostname {} - Checking IPMI protocol status. Sending a IPMI 'Get Chassis Status As String Result' request.",
hostname
Expand All @@ -122,7 +122,7 @@ public boolean checkProtocol(TelemetryManager telemetryManager) {
e
);
}
return ipmiResult != null;
return Optional.of(ipmiResult != null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.fasterxml.jackson.databind.node.TextNode;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -103,9 +104,9 @@ void testCheckIpmiUpHealth() {
.thenReturn(SUCCESS_RESPONSE);

// Start the IPMI Health Check strategy
boolean result = ipmiExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = ipmiExtension.checkProtocol(telemetryManager);

assertTrue(result);
assertTrue(result.get());
}
}

Expand All @@ -120,9 +121,9 @@ void testCheckIpmiDownHealth() {
.thenReturn(null);

// Start the IPMI Health Check strategy
boolean result = ipmiExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = ipmiExtension.checkProtocol(telemetryManager);

assertFalse(result);
assertFalse(result.get());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.UnaryOperator;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -98,15 +99,13 @@ public Set<Class<? extends Criterion>> getSupportedCriteria() {
}

@Override
public boolean checkProtocol(TelemetryManager telemetryManager) {
public Optional<Boolean> checkProtocol(TelemetryManager telemetryManager) {
// Create and set the SSH result to null
Double sshResult = UP;

// Retrieve the hostname
String hostname = telemetryManager.getHostConfiguration().getHostname();

log.info("Hostname {} - Performing protocol health check.", hostname);

// Retrieve SSH Configuration
final SshConfiguration sshConfiguration = (SshConfiguration) telemetryManager
.getHostConfiguration()
Expand All @@ -115,9 +114,10 @@ public boolean checkProtocol(TelemetryManager telemetryManager) {

// Stop the SSH health check if there is not any SSH configuration
if (sshConfiguration == null || !telemetryManager.getHostProperties().isMustCheckSshStatus()) {
return false;
return Optional.empty();
}

log.info("Hostname {} - Performing protocol health check.", hostname);
log.info("Hostname {} - Checking SSH protocol status. Sending an SSH 'echo test' command.", hostname);

// Execute Local test
Expand All @@ -129,8 +129,7 @@ public boolean checkProtocol(TelemetryManager telemetryManager) {
sshResult = remoteSshTest(hostname, sshResult, sshConfiguration);
}

//Return true if sshResult is UP, else false
return sshResult == UP;
return Optional.of(sshResult == UP);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -322,10 +323,10 @@ void testCheckSshHealthLocally() {
.thenReturn(SUCCESS_RESPONSE);

// Start the SSH Health Check strategy
boolean result = osCommandExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = osCommandExtension.checkProtocol(telemetryManager);

// Assert the result
assertTrue(result);
assertTrue(result.get());
}

try (MockedStatic<OsCommandService> staticOsCommandHelper = Mockito.mockStatic(OsCommandService.class)) {
Expand All @@ -334,10 +335,10 @@ void testCheckSshHealthLocally() {
.thenReturn(null);

// Start the SSH Health Check strategy
boolean result = osCommandExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = osCommandExtension.checkProtocol(telemetryManager);

// Assert the result
assertFalse(result);
assertFalse(result.get());
}
}

Expand All @@ -359,10 +360,10 @@ void testCheckSshUpHealthRemotely() {
.thenReturn(SUCCESS_RESPONSE);

// Start the SSH Health Check strategy
boolean result = osCommandExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = osCommandExtension.checkProtocol(telemetryManager);

// Assert the result
assertTrue(result);
assertTrue(result.get());
}

try (MockedStatic<OsCommandService> staticOsCommandHelper = Mockito.mockStatic(OsCommandService.class)) {
Expand All @@ -373,10 +374,10 @@ void testCheckSshUpHealthRemotely() {
.thenReturn(null);

// Start the SSH Health Check strategy
boolean result = osCommandExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = osCommandExtension.checkProtocol(telemetryManager);

// Assert the result
assertFalse(result);
assertFalse(result.get());
}
}

Expand All @@ -403,10 +404,10 @@ void testCheckSshUpHealthBothLocallyAndRemotely() {
.thenReturn(SUCCESS_RESPONSE);

// Start the SSH Health Check strategy
boolean result = osCommandExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = osCommandExtension.checkProtocol(telemetryManager);

// Assert the result
assertTrue(result);
assertTrue(result.get());
}

// Local commands not working
Expand All @@ -422,10 +423,10 @@ void testCheckSshUpHealthBothLocallyAndRemotely() {
.thenReturn(null);

// Start the SSH Health Check strategy
boolean result = osCommandExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = osCommandExtension.checkProtocol(telemetryManager);

// Assert the result
assertFalse(result);
assertFalse(result.get());
}
// remote command not working
try (MockedStatic<OsCommandService> staticOsCommandHelper = Mockito.mockStatic(OsCommandService.class)) {
Expand All @@ -440,10 +441,10 @@ void testCheckSshUpHealthBothLocallyAndRemotely() {
.thenReturn(SUCCESS_RESPONSE);

// Start the SSH Health Check strategy
boolean result = osCommandExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = osCommandExtension.checkProtocol(telemetryManager);

// Assert the result
assertFalse(result);
assertFalse(result.get());
}

// Both local and remote commands not working, but not throwing exceptions
Expand All @@ -458,10 +459,10 @@ void testCheckSshUpHealthBothLocallyAndRemotely() {
.when(() -> OsCommandService.runLocalCommand(anyString(), anyLong(), any()))
.thenReturn(null);

boolean result = osCommandExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = osCommandExtension.checkProtocol(telemetryManager);

// Assert the result
assertFalse(result);
assertFalse(result.get());
}
}

Expand All @@ -476,10 +477,10 @@ void testCheckSshNoHealthWhenMustCheckFalse() {
telemetryManager.getHostProperties().setOsCommandExecutesRemotely(true);

// Start the SSH Health Check strategy
boolean result = osCommandExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = osCommandExtension.checkProtocol(telemetryManager);

// Assert the result
assertFalse(result);
assertEquals(Optional.empty(), result);
}

@Test
Expand All @@ -493,10 +494,10 @@ void testCheckSshNoHealthWhenNoConfiguration() {
telemetryManager.getHostProperties().setOsCommandExecutesRemotely(true);

// Start the SSH Health Check strategy
boolean result = osCommandExtension.checkProtocol(telemetryManager);
Optional<Boolean> result = osCommandExtension.checkProtocol(telemetryManager);

// Assert the result
assertFalse(result);
assertEquals(Optional.empty(), result);
}

@Test
Expand Down
Loading