use of io.hops.hopsworks.alerting.config.dto.AlertManagerConfig in project hopsworks by logicalclocks.
the class AlertManagerConfiguration method restoreFromBackup.
public void restoreFromBackup() throws AlertManagerConfigCtrlCreateException, AlertManagerConfigReadException, IOException, AlertManagerUnreachableException, AlertManagerConfigUpdateException, AlertManagerClientCreateException {
AlertManagerConfig alertManagerConfig = read();
Optional<AlertManagerConfigEntity> optionalAlertManagerConfigEntity = alertManagerConfigFacade.getLatest();
ObjectMapper objectMapper = new ObjectMapper();
JSONObject jsonAlertManagerConfig = alertManagerConfig != null ? new JSONObject(objectMapper.writeValueAsString(alertManagerConfig)) : null;
JSONObject jsonAlertManagerConfigBackup = optionalAlertManagerConfigEntity.map(AlertManagerConfigEntity::getContent).orElse(null);
AlertManagerConfig alertManagerConfigBackup = jsonAlertManagerConfigBackup != null ? objectMapper.readValue(jsonAlertManagerConfigBackup.toString(), AlertManagerConfig.class) : null;
if (jsonAlertManagerConfig != null && jsonAlertManagerConfigBackup != null) {
if (!JsonObjectHelper.similar(jsonAlertManagerConfig, jsonAlertManagerConfigBackup)) {
boolean updated = merge(alertManagerConfig, alertManagerConfigBackup);
if (updated) {
writeAndReload(alertManagerConfig);
LOGGER.log(Level.INFO, "Fixed Alert manager config from backup.");
} else {
// Not similar but not updated then save new backup
saveToDatabase(jsonAlertManagerConfig);
LOGGER.log(Level.INFO, "Alert manager config backup saved.");
}
} else {
LOGGER.log(Level.INFO, "Alert manager config is up to date with backup.");
}
} else if (jsonAlertManagerConfig == null && jsonAlertManagerConfigBackup != null) {
writeAndReload(alertManagerConfigBackup);
LOGGER.log(Level.INFO, "Replace Alert manager config with backup.");
} else if (jsonAlertManagerConfig != null) {
saveToDatabase(jsonAlertManagerConfig);
LOGGER.log(Level.INFO, "Alert manager config backup saved.");
}
}
use of io.hops.hopsworks.alerting.config.dto.AlertManagerConfig in project hopsworks by logicalclocks.
the class TestAlertManagerConfigTimer method testRestoreBackup.
@Test
public void testRestoreBackup() throws AlertManagerUnreachableException, AlertManagerConfigCtrlCreateException, AlertManagerConfigUpdateException, IOException, AlertManagerConfigReadException, AlertManagerClientCreateException {
Mockito.doAnswer((Answer<Void>) invocation -> {
Object[] args = invocation.getArguments();
if (args != null && args.length > 0 && args[0] != null) {
AlertManagerConfigEntity alertManagerConfigEntity = (AlertManagerConfigEntity) args[0];
AlertManagerConfig alertManagerConfig = fromJson(alertManagerConfigEntity.getContent());
LOGGER.log(Level.INFO, "Update database routes={0}, receivers={1}.", new Object[] { alertManagerConfig.getRoute().getRoutes().size(), alertManagerConfig.getReceivers().size() });
assert alertManagerConfig.getRoute().getRoutes().size() == 15;
assert alertManagerConfig.getReceivers().size() == 9;
}
return null;
}).when(alertManagerConfigFacade).update(Mockito.any());
alertManagerConfiguration.restoreFromBackup();
AlertManagerConfig alertManagerConfig = alertManagerConfigController.read();
assert alertManagerConfig.getRoute().getRoutes().size() == 15;
assert alertManagerConfig.getReceivers().size() == 9;
}
use of io.hops.hopsworks.alerting.config.dto.AlertManagerConfig in project hopsworks by logicalclocks.
the class TestAlertManagerConfigTimer method testReceiverFix.
@Test
public void testReceiverFix() throws AlertManagerUnreachableException, AlertManagerConfigCtrlCreateException, AlertManagerConfigUpdateException, IOException, AlertManagerConfigReadException, AlertManagerClientCreateException {
alertManagerConfiguration.runFixConfig();
AlertManagerConfig alertManagerConfig = alertManagerConfigController.read();
assert alertManagerConfig.getRoute().getRoutes().size() == 15;
assert alertManagerConfig.getReceivers().size() == 6;
}
use of io.hops.hopsworks.alerting.config.dto.AlertManagerConfig in project hopsworks by logicalclocks.
the class TestAlertManagerConfigTimer method setUp.
@Before
public void setUp() throws ServiceDiscoveryException, AlertManagerConfigFileNotFoundException, AlertManagerConfigReadException, JsonProcessingException {
String alertmanagerConfigPath = TestAlertManagerConfigTimer.class.getResource("/alertmanager.yml").getPath();
alertManagerConfigDBPath = TestAlertManagerConfigTimer.class.getResource("/alertmanagerDB.yml").getPath();
client = Mockito.mock(AlertManagerClient.class);
alertManagerConfigController = new AlertManagerConfigController.Builder().withConfigPath(alertmanagerConfigPath).withClient(client).build();
alertManagerConfigFacade = Mockito.mock(AlertManagerConfigFacade.class);
Mockito.doAnswer((Answer<Void>) invocation -> {
Object[] args = invocation.getArguments();
LOGGER.log(Level.INFO, "Save to database: {0}.", args);
return null;
}).when(alertManagerConfigFacade).save(Mockito.any());
Mockito.doAnswer((Answer<Void>) invocation -> {
Object[] args = invocation.getArguments();
LOGGER.log(Level.INFO, "Update database: {0}.", args);
return null;
}).when(alertManagerConfigFacade).update(Mockito.any());
AlertManagerConfig alertManagerConfig = read(new File(alertManagerConfigDBPath));
AlertManagerConfigEntity alertManagerConfigEntity = new AlertManagerConfigEntity();
alertManagerConfigEntity.setId(1);
alertManagerConfigEntity.setCreated(new Date());
alertManagerConfigEntity.setContent(toJson(alertManagerConfig));
Mockito.when(alertManagerConfigFacade.getLatest()).thenReturn(Optional.of(alertManagerConfigEntity));
alertReceiverFacade = Mockito.mock(AlertReceiverFacade.class);
List<AlertReceiver> receivers = createReceivers();
Mockito.when(alertReceiverFacade.findAll()).thenReturn(receivers);
Mockito.doAnswer((Answer<Optional<AlertReceiver>>) invocation -> {
Object[] args = invocation.getArguments();
Optional<AlertReceiver> alertReceiver = receivers.stream().filter(receiver -> receiver.getName().equals(args[0])).findFirst();
return alertReceiver;
}).when(alertReceiverFacade).findByName(Mockito.any());
alertManagerConfiguration = new AlertManagerConfiguration(client, alertManagerConfigController, alertManagerConfigFacade, alertReceiverFacade);
alertManagerConfigBackup = alertManagerConfigController.read();
alertManagerConfigBackupDB = read(new File(alertManagerConfigDBPath));
}
use of io.hops.hopsworks.alerting.config.dto.AlertManagerConfig in project hopsworks by logicalclocks.
the class TestAlertManagerConfigTimer method testGlobalFromBackup.
@Test
public void testGlobalFromBackup() throws AlertManagerConfigReadException, IOException, AlertManagerUnreachableException, AlertManagerConfigCtrlCreateException, AlertManagerConfigUpdateException, AlertManagerClientCreateException {
AlertManagerConfig alertManagerConfig = read(new File(alertManagerConfigDBPath));
alertManagerConfig.getGlobal().setSlackApiUrl("https://hooks.slack.com/services/1234567/ASEDWRFDE/XXXXXXXXXXXXXX");
AlertManagerConfigEntity alertManagerConfigEntity = new AlertManagerConfigEntity();
alertManagerConfigEntity.setId(1);
alertManagerConfigEntity.setCreated(new Date());
alertManagerConfigEntity.setContent(toJson(alertManagerConfig));
Mockito.when(alertManagerConfigFacade.getLatest()).thenReturn(Optional.of(alertManagerConfigEntity));
alertManagerConfiguration.restoreFromBackup();
AlertManagerConfig alertManagerConfig1 = alertManagerConfigController.read();
assert alertManagerConfig1.getGlobal().getSlackApiUrl().equals("https://hooks.slack.com/services/1234567/ASEDWRFDE/XXXXXXXXXXXXXX");
}
Aggregations