Search in sources :

Example 6 with AmazonCloudWatchException

use of com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException in project cloudbreak by hortonworks.

the class AwsCloudWatchService method getExistingCloudWatchAlarms.

private Stream<List<String>> getExistingCloudWatchAlarms(String regionName, AwsCredentialView credentialView, List<String> alarmNames) {
    Stream<List<String>> filteredAlarmNamesStream;
    AmazonCloudWatchClient amazonCloudWatchClient = awsClient.createCloudWatchClient(credentialView, regionName);
    LOGGER.info("Searching for cloudwatch alarms [{}]", alarmNames);
    try {
        DescribeAlarmsRequest request = new DescribeAlarmsRequest().withAlarmNames(alarmNames).withMaxRecords(maxBatchsize);
        List<String> filteredAlarmNames = amazonCloudWatchClient.describeAlarms(request).getMetricAlarms().stream().map(MetricAlarm::getAlarmName).collect(Collectors.toList());
        filteredAlarmNamesStream = Stream.of(filteredAlarmNames);
        LOGGER.debug("Checking cloudwatch alarms [{}] for existence and found [{}]", alarmNames, filteredAlarmNames);
    } catch (AmazonCloudWatchException acwe) {
        LOGGER.error("Unable to describe cloudwatch alarms falling back to delete all alarms individually [{}]: {}", alarmNames, acwe.getLocalizedMessage());
        filteredAlarmNamesStream = alarmNames.stream().map(alarmName -> List.of(alarmName));
    }
    return filteredAlarmNamesStream;
}
Also used : AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) AmazonCloudWatchException(com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) List(java.util.List)

Example 7 with AmazonCloudWatchException

use of com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException in project cloudbreak by hortonworks.

the class AwsCloudWatchService method addCloudWatchAlarmsForSystemFailures.

public void addCloudWatchAlarmsForSystemFailures(List<CloudResource> instances, String regionName, AwsCredentialView credentialView) {
    AmazonCloudWatchClient amazonCloudWatchClient = awsClient.createCloudWatchClient(credentialView, regionName);
    instances.stream().filter(instance -> {
        String instanceType = instance.getStringParameter(INSTANCE_TYPE);
        if (instanceType == null) {
            LOGGER.debug("Cannot determine if recovery is supported by instance type, attempting to set it up.");
            return true;
        }
        String family = instanceType.contains(".") ? instanceType.split("\\.")[0] : instanceType;
        return RECOVERABLE_INSTANCE_TYPES.contains(family);
    }).forEach(instance -> {
        try {
            PutMetricAlarmRequest metricAlarmRequest = new PutMetricAlarmRequest();
            String segment = credentialView.isGovernmentCloudEnabled() ? "aws-us-gov" : "aws";
            metricAlarmRequest.setAlarmActions(Arrays.asList("arn:" + segment + ":automate:" + regionName + ":ec2:recover"));
            metricAlarmRequest.setAlarmName(instance.getInstanceId() + alarmSuffix);
            metricAlarmRequest.setMetricName("StatusCheckFailed_System");
            metricAlarmRequest.setStatistic("Maximum");
            metricAlarmRequest.setNamespace("AWS/EC2");
            metricAlarmRequest.setDimensions(Arrays.asList(new Dimension().withName("InstanceId").withValue(instance.getInstanceId())));
            metricAlarmRequest.setPeriod(cloudwatchPeriod);
            metricAlarmRequest.setEvaluationPeriods(cloudwatchEvaluationPeriods);
            metricAlarmRequest.setThreshold(cloudwatchThreshhold);
            metricAlarmRequest.setComparisonOperator("GreaterThanOrEqualToThreshold");
            amazonCloudWatchClient.putMetricAlarm(metricAlarmRequest);
            LOGGER.debug("Created cloudwatch alarm for instanceId {}.", instance.getInstanceId());
        } catch (AmazonCloudWatchException acwe) {
            LOGGER.info("Unable to create cloudwatch alarm for instanceId {} (instance type: {}): {}", instance.getInstanceId(), instance.getStringParameter(INSTANCE_TYPE), acwe.getLocalizedMessage());
        }
    });
}
Also used : Arrays(java.util.Arrays) INSTANCE_TYPE(com.sequenceiq.cloudbreak.cloud.model.CloudResource.INSTANCE_TYPE) LoggerFactory(org.slf4j.LoggerFactory) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) Dimension(com.amazonaws.services.cloudwatch.model.Dimension) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) Map(java.util.Map) DeleteAlarmsRequest(com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest) Logger(org.slf4j.Logger) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) MetricAlarm(com.amazonaws.services.cloudwatch.model.MetricAlarm) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Collectors(java.util.stream.Collectors) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) PutMetricAlarmRequest(com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest) Stream(java.util.stream.Stream) AmazonCloudWatchException(com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) PutMetricAlarmRequest(com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest) AmazonCloudWatchException(com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException) Dimension(com.amazonaws.services.cloudwatch.model.Dimension)

Example 8 with AmazonCloudWatchException

use of com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException in project cloudbreak by hortonworks.

the class AwsNativeCloudWatchServiceTest method testDeleteCloudWatchAlarmsForSystemFailuresDoesNotBatchDeletionWhenDescribeAlarmPermissionIsMissing.

@Test
void testDeleteCloudWatchAlarmsForSystemFailuresDoesNotBatchDeletionWhenDescribeAlarmPermissionIsMissing() {
    List<CloudInstance> cloudInstances = new LinkedList<>();
    List<List<String>> deleteAlarmNames = new LinkedList<>();
    List<String> alarmNames1 = new LinkedList<>();
    List<String> alarmNames2 = new LinkedList<>();
    List<String> alarmNames3 = new LinkedList<>();
    for (int i = 1; i <= 100; i++) {
        String alarmName = "i-" + i + "-Status-Check-Failed-System";
        alarmNames1.add(alarmName);
        deleteAlarmNames.add(List.of(alarmName));
    }
    for (int i = 101; i <= 200; i++) {
        String alarmName = "i-" + i + "-Status-Check-Failed-System";
        alarmNames2.add(alarmName);
        deleteAlarmNames.add(List.of(alarmName));
    }
    for (int i = 201; i <= 210; i++) {
        String alarmName = "i-" + i + "-Status-Check-Failed-System";
        alarmNames3.add(alarmName);
        deleteAlarmNames.add(List.of(alarmName));
    }
    for (int i = 1; i <= 210; i++) {
        String instanceId = "i-" + i;
        CloudInstance cloudInstance = mock(CloudInstance.class);
        when(cloudInstance.getInstanceId()).thenReturn(instanceId);
        cloudInstances.add(cloudInstance);
    }
    AwsCredentialView credentialView = mock(AwsCredentialView.class);
    CloudStack stack = mock(CloudStack.class);
    Group group = mock(Group.class);
    List<Group> groups = List.of(group);
    when(stack.getGroups()).thenReturn(groups);
    when(group.getInstances()).thenReturn(cloudInstances);
    when(commonAwsClient.createCloudWatchClient(credentialView, REGION)).thenReturn(mockAmazonCloudWatchClient);
    AmazonCloudWatchException exception = new AmazonCloudWatchException("No permissions to describe cloudwatch alarms");
    when(mockAmazonCloudWatchClient.describeAlarms(any())).thenThrow(exception);
    underTest.deleteAllCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView);
    ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class);
    ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class);
    verify(mockAmazonCloudWatchClient, times(3)).describeAlarms(captorDescribe.capture());
    verify(mockAmazonCloudWatchClient, times(210)).deleteAlarms(captorDelete.capture());
    assertEquals(List.of(alarmNames1, alarmNames2, alarmNames3), captorDescribe.getAllValues().stream().map(DescribeAlarmsRequest::getAlarmNames).collect(Collectors.toList()));
    assertEquals(deleteAlarmNames, captorDelete.getAllValues().stream().map(DeleteAlarmsRequest::getAlarmNames).collect(Collectors.toList()));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) LinkedList(java.util.LinkedList) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DeleteAlarmsRequest(com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest) AmazonCloudWatchException(com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException) LinkedList(java.util.LinkedList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

AmazonCloudWatchException (com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException)8 DeleteAlarmsRequest (com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest)5 DescribeAlarmsRequest (com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest)5 List (java.util.List)5 AmazonCloudWatchClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient)4 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)3 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)3 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)3 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)3 PutMetricAlarmRequest (com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest)2 Group (com.sequenceiq.cloudbreak.cloud.model.Group)2 LinkedList (java.util.LinkedList)2 Test (org.junit.jupiter.api.Test)2 Dimension (com.amazonaws.services.cloudwatch.model.Dimension)1 MetricAlarm (com.amazonaws.services.cloudwatch.model.MetricAlarm)1 AwsCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 INSTANCE_TYPE (com.sequenceiq.cloudbreak.cloud.model.CloudResource.INSTANCE_TYPE)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1